Skip to content

Add parseSafe and parseStrict cases for @sinclair/typebox - #2333

Open
colinhacks wants to merge 2 commits into
moltar:masterfrom
colinhacks:typebox-parse-categories
Open

Add parseSafe and parseStrict cases for @sinclair/typebox#2333
colinhacks wants to merge 2 commits into
moltar:masterfrom
colinhacks:typebox-parse-categories

Conversation

@colinhacks

Copy link
Copy Markdown
Contributor

The @sinclair/typebox cases currently register only the assert categories. This adds parseSafe and parseStrict to the dynamic and just-in-time variants.

For parseSafe, TypeCompiler only compiles the check, so unknown-key removal goes through the dynamic Value module: Check first, then Value.Clean on a Value.Clone so the input is not mutated and the case returns a new object. parseStrict checks against the Strict schema and returns the input, matching the typia case.

The ahead-of-time variant is left as-is — its generated modules contain only check functions, and a parse case there would measure the same dynamic Clean.

Copilot AI lite review requested due to automatic review settings August 30, 2026 23:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@colinhacks
colinhacks force-pushed the typebox-parse-categories branch from 85a480f to ef55d5f Compare August 30, 2026 23:51
Comment thread cases/sinclair-typebox-dynamic.ts Outdated
if (!Value.Check(Loose, data)) {
throw new Error('validation failure');
}
return Value.Clean(Loose, Value.Clone(data)) as typeof data;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cheating! Cannot accept it. 😁

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair! Reworked it to use Value.Parse — TypeBox's first-class parse entry point, which clones, strips unknown keys, and asserts in one call and returns a typed result. The just-in-time variant drops parseSafe entirely, since TypeCompiler only compiles checks and has no parse pipeline.

One thing worth knowing: the default Value.Parse pipeline includes Convert, so it coerces e.g. string numbers the way yup's non-strict validateSync does. The category tests pass either way.

Comment thread cases/sinclair-typebox-just-in-time.ts Outdated
if (!CheckLoose.Check(data)) {
throw new Error('validation failure');
}
return Value.Clean(Loose, Value.Clone(data)) as typeof data;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants