Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,12 @@ export function type<Schema extends ObjectSchema>(
*entries(value) {
if (isObject(value)) {
for (const k of keys) {
if (
ExactOptionalStruct.isExactOptional(schema[k]) &&
!Object.prototype.hasOwnProperty.call(value, k)
) {
continue;
}
yield [k, value[k], schema[k] as Struct<any>];
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/typings/exactOptional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
string,
number,
object,
type,
enums,
never,
record,
Expand Down Expand Up @@ -60,3 +61,17 @@ test<{
);
return value;
});

test<{
a?: number;
b: string;
}>((value) => {
assert(
value,
type({
a: exactOptional(number()),
b: string(),
}),
);
return value;
});
21 changes: 21 additions & 0 deletions test/validation/type/invalid-exact-optional-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type, exactOptional, string, number } from '../../../src';

export const Struct = type({
name: string(),
age: exactOptional(number()),
});

export const data = {
name: 'john',
age: undefined,
};

export const failures = [
{
value: undefined,
type: 'number',
refinement: undefined,
path: ['age'],
branch: [data, data.age],
},
Comment thread
hmalik88 marked this conversation as resolved.
];
12 changes: 12 additions & 0 deletions test/validation/type/valid-exact-optional-absent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type, exactOptional, string, number } from '../../../src';

export const Struct = type({
name: string(),
age: exactOptional(number()),
});

export const data = {
name: 'john',
};

export const output = data;
13 changes: 13 additions & 0 deletions test/validation/type/valid-exact-optional-present.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type, exactOptional, string, number } from '../../../src';

export const Struct = type({
name: string(),
age: exactOptional(number()),
});

export const data = {
name: 'john',
age: 42,
};

export const output = data;
Loading