gristlabs/ts-interface-checker

Array of custom interface only allows 1 entry

woulamme opened this issue · 2 comments

I might be doing something wrong, but I defined two interfaces like these:

export interface ToggleRequest {
  works: string[];
  doesNotWork: MyType[];
}

export interface MyType {
  one: string;
  two: string;
}

Now strict validation on the below json fails with "error": "value.doesNotWork[1] is extraneous":

{
	"works": ["here", "more", "than", "one", "works"],
	"doesNotWork": [
		{
			"one": "but",
			"two": "here
		}, {
			"one": "it",
			"two": "doesn't"
		}]
}

Is there a problem with strict validation of arrays of custom types?

Are you sure the problem isn't something else? (E.g. something's up with unclosed quote marks in your example.) Maybe provide a full example to reproduce the problem. The following works for me:

import {createCheckers} from '.';
import {ToggleRequest} from './bug20';
import typeSuite from './bug20-ti';

export const checkers = createCheckers(typeSuite);

const data: ToggleRequest = {
	"works": ["here", "more", "than", "one", "works"],
	"doesNotWork": [
		{
			"one": "but",
			"two": "here",
		}, {
			"one": "it",
			"two": "doesn't"
		}]
};

checkers.ToggleRequest.check(data);

The situation you describe could happen if you declare the type of doesNotWork as [MyType] rather than MyType[], or use the interface descriptor of t.tuple('MyType') instead of t.array('MyType') (but then it's correctly checking that doesNotWork is a tuple of one element).