Skip to content

feat: implement standard schema spec#3080

Merged
Marsup merged 4 commits into
hapijs:masterfrom
xballoy:xba/standard-schema
Jul 31, 2025
Merged

feat: implement standard schema spec#3080
Marsup merged 4 commits into
hapijs:masterfrom
xballoy:xba/standard-schema

Conversation

@xballoy

@xballoy xballoy commented May 29, 2025

Copy link
Copy Markdown
Contributor

Implements standard-schema.

Fixes #3078

Comment thread test/helper.js
@Marsup

Marsup commented Jun 18, 2025

Copy link
Copy Markdown
Collaborator

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

xballoy commented Jun 18, 2025

Copy link
Copy Markdown
Contributor Author

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

Marsup commented Jun 18, 2025

Copy link
Copy Markdown
Collaborator

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

xballoy commented Jun 18, 2025

Copy link
Copy Markdown
Contributor Author

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 commented Jun 18, 2025

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread lib/validator.js Outdated
@Marsup

Marsup commented Jul 30, 2025

Copy link
Copy Markdown
Collaborator

Sorry, took me a while to find some time. Is #3084 a good fit to help you with this PR?

@xballoy

xballoy commented Jul 30, 2025

Copy link
Copy Markdown
Contributor Author

Sorry, took me a while to find some time. Is #3084 a good fit to help you with this PR?

@Marsup yes that would help, I'll be able to pass a flag isAsync to Validator.standard(value, this) to validate only once.

Comment thread lib/base.js
@xballoy

xballoy commented Jul 31, 2025

Copy link
Copy Markdown
Contributor Author

@Marsup I rebased on master and everything works now 🎉

@Marsup Marsup added this to the 18.0.0 milestone Jul 31, 2025
@Marsup Marsup added the feature New functionality or improvement label Jul 31, 2025
Comment thread lib/base.js Outdated
@Marsup

Marsup commented Jul 31, 2025

Copy link
Copy Markdown
Collaborator

Thanks a lot for your work, I'll try to release the next major soon!

@Marsup Marsup merged commit d746c03 into hapijs:master Jul 31, 2025
9 checks passed
@Marsup Marsup self-assigned this Aug 1, 2025
@Marsup Marsup mentioned this pull request May 14, 2025
@xballoy xballoy deleted the xba/standard-schema branch August 7, 2025 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New functionality or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Standard Schema

2 participants