-
Notifications
You must be signed in to change notification settings - Fork 117
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
feat: add createAjvValidatorAsync #522
base: main
Are you sure you want to change the base?
Conversation
Thanks Stefan, I'll review your PR as soon as I can. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Stefan, your PR looks good! I like the way you've refactored both the code and made sure the tests are not duplicated. It makes sense to create a separate function for the async variant since it returnse a Promise, that would be a breaking change and not something you want to deal with in the synchronous case.
I made a few inline comments, can you have a look at those?
describe('createAjvValidator', () => { | ||
test('should create a validate function', () => { | ||
const validate = createAjvValidator({ schema, schemaDefinitions }) | ||
function sharedAssertions(factoryFn: typeof createAjvValidator | typeof createAjvValidatorAsync) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you replace the solution with a sharedAssertions
function with using describe.each
of vitest? Docs: https://vitest.dev/api/#describe-each
Something like:
describe.each([
{ name: 'createAjvValidator', create: createAjvValidator },
{ name: 'createAjvValidatorAsync', create: createAjvValidatorAsync }
])('$name', ({ create }) => {
// ... the tests ...
})
* } | ||
* }) | ||
*/ | ||
export async function createAjvValidatorAsync(options: AjvValidatorOptions): Promise<Validator> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you describe the new function createAjvValidatorAsync
in the README.md, in the sections "validator" and "Utility functions"?
* | ||
* @default ValidationSeverity.warning | ||
*/ | ||
errorSeverity?: ValidationSeverity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition 👌
This PR adds a new function,
createAjvValidatorAsync
to the Ajv plugin.It behaves like
createAjvValidator
, but usesajv.compileAsync
on the schema which allows for resolution of remote schemas through theloadSchema(uri)
hook of Ajv'soptions
.Due to the async nature of
compileAsync
, the return format changes, which would be a breaking change. So I opted for a second factory function in order to not break existing consumers.The PR also adds
errorSeverity
as an option.As you can see, I have refactored the code a bit s.t. as little code as possible is duplicated. It had a bit of an unfortunate impact on the testing code, where I also tried to not duplicate all the existing tests just be cause there now is a second factory function.
Thanks for your work & please let me know if you want anything changed :)