8000 feat: implement standard schema spec by xballoy · Pull Request #3080 · hapijs/joi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: implement standard schema spec #3080

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xballoy
Copy link
@xballoy xballoy commented May 29, 2025

Implements standard-schema.

Fixes #3078

@Marsup
Copy link
Collaborator
Marsup commented Jun 18, 2025

Hi, thanks for that great PR, this seems properly implemented, I just have some doubts about async validation. It seems they advise against it, but only because of zod constraints if I got it right? I guess that'll lead to a few surprises for users.

@xballoy
Copy link
Author
xballoy commented Jun 18, 2025

Hi, thanks for that great PR, this seems properly implemented, I just have some doubts about async validation. It seems they advise against it, but only because of zod constraints if I got it right? I guess that'll lead to a few surprises for users.

@Marsup from the type definition '~standard'.validate returns Result<Output> | Promise<Result<Output>> so in theory it would be possible to handle async validation. However in '~standard'.validate how should I decide to call validate() or validateAsync()? 🤔

Would it make sense to check if there are external async and do an async validation only in that case?

@Marsup
Copy link
Collaborator
Marsup commented Jun 18, 2025

That's usually up to the schema creator to decide, but an error will be thrown if an async schema is called with the sync method. Since the advent of async/await, I've honestly been thinking of sunsetting the sync api, as it's now so easy to use promises, but it seems to go against their recommendations. I guess joi could have an api that surfaces externals if that helps.

@xballoy
Copy link
Author
xballoy commented Jun 18, 2025

zod seems to support async validation with the standard schema: https://github.com/colinhacks/zod/pull/3850/files#diff-f025de6d5c2512a8f3d95968437eb61a2bb34c012ca364ee2ed65adcef828b86R53

We could always use the async validation, that would:

  • simplify the implem
  • allow you to deprecate the sync validation

Or if we have an easy way to check if the schema contains an external with an async method we could call the async validation only in that case.

What do you think?

@xballoy xballoy force-pushed the xba/standard-schema branch from b0d69b9 to 19e7d0d Compare June 18, 2025 21:34
@xballoy xballoy force-pushed the xba/standard-schema branch from 19e7d0d to a91efc3 Compare June 18, 2025 21:35
@xballoy
Copy link
Author
xballoy commented Jun 18, 2025

@Marsup I updated the logic + added tests to support externals and async validation. Tell me what you think but I think it covers all cases.

const settings = Common.defaults;

const result = internals.entry(value, schema, settings);
if (result.mainstay.externals.length) {
Copy link
Author

Choose a reason for hiding this comment

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

Not sure this is the best way to detect if there are externals 🤔

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.

Implement Standard Schema
2 participants
0