Skip to content
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

AJV Test Case #683

Open
sinclairzx81 opened this issue Jul 6, 2021 · 4 comments
Open

AJV Test Case #683

sinclairzx81 opened this issue Jul 6, 2021 · 4 comments
Labels
new package Adds a new validation package.

Comments

@sinclairzx81
Copy link
Contributor

Hi, great project!

Would it be possible to include AJV to this test suite? I think a case exists for testing AJV performance as it's possible for TypeScript to inference types from JSON schema directly via conditional type mapping. Would certainly be curious to see how the defacto JSON schema validator holds up against these libraries.

Many Thanks!

@moltar
Copy link
Owner

moltar commented Jul 7, 2021

Hey @sinclairzx81!

Does ajv fit into the criteria outlined here? https://github.com/moltar/typescript-runtime-type-benchmarks#criteria

Also, I think some packages do use ajv internally. You can see which ones by doing npm ls ajv in the repo.

@sinclairzx81
Copy link
Contributor Author

sinclairzx81 commented Jul 7, 2021

Hey @moltar, thanks for the quick response.

Does ajv fit into the criteria outlined here? https://github.com/moltar/typescript-runtime-type-benchmarks#criteria

Ah I didn't see the criteria section. Thanks for linking that.

  • These packages are capable of validating the data for type correctness (Yes)
  • It has a validator function or method that returns a valid type casted value or throws. (Not directly)

I guess on the second point, Ajv doesn't quite meet the criteria, but it is very close. The following 4 line function would be enough for Ajv to fit the criteria.

// The following deserializes the given string and returns T if matching the given schema, otherwise throw.
function deserialize<T = unknown>(schema: any, data: string): T {
    const ajv = new Ajv();
    const obj = JSON.parse(data)
    if(!ajv.validate(schema, obj)) throw 
    return obj
}

Just for a bit of context. I actually manage this project https://github.com/sinclairzx81/typebox that implements the JSON schema specification but doesn't actually implement any runtime validation logic. Conceptually, the library is similar to io-ts, zod and runtypes (where it implements static inference) but whose runtime performance is measured by that of the underlying JSON schema validator.

TypeBox doesn't currently meet either criteria for benchmarking as it offers no validation function, and requires a JSON schema validator of the users choosing to runtime type check. It's an outlier because it's conceptually serving the same role as io-ts, zod and runtypes but whose performance is tied to that of the JSON schema validator. So the only way to benchmark TypeBox would be to benchmark JSON schema validators. I expect this would be true for other test cases dependent on Ajv.

It's an interesting one. In all, I do think a pure Ajv test would give a good indication of how standard JSON validation holds up against libraries who chose to implement internal custom validation logic.

Open to your thoughts!
Cheers
S

@moltar
Copy link
Owner

moltar commented Jul 7, 2021

Just for a bit of context. I actually manage this project https://github.com/sinclairzx81/typebox that implements the JSON schema specification but doesn't actually implement any runtime validation logic.

Yup yup, I am a fan of your work! :)

TypeBox doesn't currently meet either criteria for benchmarking as it offers no validation function

Yup, I tried it in #97 😁

It's an interesting one. In all, I do think a pure Ajv test would give a good indication of how standard JSON validation holds up against libraries who chose to implement internal custom validation logic.

I kinda agree and maybe we'll relax the criteria a bit after we finish #497 which opens up the possibility for different groups of tests.

@sinclairzx81
Copy link
Contributor Author

@moltar Hey, awesome, that would be great ! :)

Will keep an eye out for #497, I'm very curious to see how the AJV validation stacks up against specialized TS type validation.

Many Many Thanks!
S

rehanvdm pushed a commit to rehanvdm/typescript-runtime-type-benchmarks that referenced this issue Jan 1, 2022
@rehanvdm rehanvdm mentioned this issue Jan 1, 2022
rehanvdm pushed a commit to rehanvdm/typescript-runtime-type-benchmarks that referenced this issue Jan 3, 2022
moltar pushed a commit that referenced this issue Jan 3, 2022
@moltar moltar added the new package Adds a new validation package. label Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new package Adds a new validation package.
Projects
None yet
Development

No branches or pull requests

2 participants