Skip to content

Commit

Permalink
Merge pull request #24 from ty-ras/issue/17-fix-state-optionality
Browse files Browse the repository at this point in the history
#17 Forgot crucial 'optional' call.
  • Loading branch information
stazz authored Aug 9, 2023
2 parents f159d1a + 542990b commit c90ccc5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion state/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ty-ras/state-zod",
"version": "2.0.0",
"version": "2.0.1",
"author": {
"name": "Stanislav Muhametsin",
"email": "[email protected]",
Expand Down
16 changes: 16 additions & 0 deletions state/src/__test__/state-validator-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ test("Verify that giving wrong input to state validator factory throws an error"
});
});

test("Verify that optional authenticated property validation behaves as expected", (c) => {
c.plan(1);

const stateValidatorFactory = createStateValidatorFactory(
{ userId: t.string() },
{},
);

const optional = stateValidatorFactory({ userId: false });
c.deepEqual(optional.validator({}), {
// Important that this it NOT protocol-error
error: "none",
data: {},
});
});

const createStateValidatorFactory = <
TAuthenticated extends Record<string, data.AnyDecoder>,
TOther extends Record<string, data.AnyDecoder>,
Expand Down
5 changes: 4 additions & 1 deletion state/src/state-validator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export const createStateValidatorFactory = <
...Object.fromEntries(
optional.map(
(optionalProp) =>
[optionalProp, validation[optionalProp].validation] as const,
[
optionalProp,
validation[optionalProp].validation.optional(),
] as const,
),
),
});
Expand Down

0 comments on commit c90ccc5

Please sign in to comment.