Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat missing fields as undefined #61

Merged
merged 1 commit into from Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/index.spec.ts
Expand Up @@ -87,6 +87,7 @@ const runtypes = {
InstanceOfSomeClass: InstanceOf(SomeClass),
InstanceOfSomeOtherClass: InstanceOf(SomeOtherClass),
DictionaryOfArraysOfSomeClass: Dictionary(Array(InstanceOf(SomeClass))),
OptionalKey: Record({ foo: String, bar: Union(Number, Undefined) }),
};

type RuntypeName = keyof typeof runtypes;
Expand All @@ -111,7 +112,7 @@ const testValues: { value: always; passes: RuntypeName[] }[] = [
{ value: { Boolean: true, Number: 3 }, passes: ['record1', 'union1', 'Partial'] },
{ value: { Boolean: true }, passes: ['Partial'] },
{ value: { Boolean: true, foo: undefined }, passes: ['Partial'] },
{ value: { Boolean: true, foo: 'hello' }, passes: ['Partial'] },
{ value: { Boolean: true, foo: 'hello' }, passes: ['Partial', 'OptionalKey'] },
{ value: { Boolean: true, foo: 5 }, passes: [] },
{ value: (x: number, y: string) => x + y.length, passes: ['Function'] },
{ value: { name: undefined, likes: [] }, passes: [] },
Expand All @@ -130,6 +131,8 @@ const testValues: { value: always; passes: RuntypeName[] }[] = [
{ value: [1, 2, 3, 4], passes: ['ArrayNumber', 'CustomArray', 'CustomArrayWithMessage'] },
{ value: new SomeClass(42), passes: ['InstanceOfSomeClass'] },
{ value: { xxx: [new SomeClass(55)] }, passes: ['DictionaryOfArraysOfSomeClass'] },
{ value: { foo: 'hello' }, passes: ['OptionalKey', 'Dictionary'] },
{ value: { foo: 'hello', bar: undefined }, passes: ['OptionalKey'] },
];

for (const { value, passes } of testValues) {
Expand Down Expand Up @@ -297,6 +300,18 @@ describe('check errors', () => {
);
});

it('record missing keys', () => {
assertThrows(
{ name: 'Jack' },
Record({
name: String,
age: Number,
}),
'Expected number, but was undefined',
'age',
);
});

it('record complex', () => {
assertThrows(
{ name: 'Jack', age: 10, likes: [{ title: false }] },
Expand Down
12 changes: 5 additions & 7 deletions src/types/record.ts
Expand Up @@ -21,13 +21,11 @@ export function Record<O extends { [_: string]: Runtype }>(fields: O) {

// tslint:disable-next-line:forin
for (const key in fields) {
if (hasKey(key, x)) {
try {
fields[key].check(x[key]);
} catch ({ key: nestedKey, message }) {
throw validationError(message, nestedKey ? `${key}.${nestedKey}` : key);
}
} else throw validationError(`Expected ${key} to be ${show(fields[key].reflect)}`, key);
try {
fields[key].check(hasKey(key, x) ? x[key] : undefined);
} catch ({ key: nestedKey, message }) {
throw validationError(message, nestedKey ? `${key}.${nestedKey}` : key);
}
}

return x as O;
Expand Down