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

feat: add createAjvValidatorAsync #522

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iStefo
Copy link

@iStefo iStefo commented Feb 17, 2025

This PR adds a new function, createAjvValidatorAsync to the Ajv plugin.

It behaves like createAjvValidator, but uses ajv.compileAsync on the schema which allows for resolution of remote schemas through the loadSchema(uri) hook of Ajv's options.

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 :)

@josdejong
Copy link
Owner

Thanks Stefan, I'll review your PR as soon as I can.

Copy link
Owner

@josdejong josdejong left a 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) {
Copy link
Owner

@josdejong josdejong Feb 28, 2025

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> {
Copy link
Owner

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
Copy link
Owner

Choose a reason for hiding this comment

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

Nice addition 👌

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.

2 participants