ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.47k stars 864 forks source link

compileAsync a schema with discriminator and $ref, fixes #2427 #2433

Closed jasoniangreen closed 2 weeks ago

jasoniangreen commented 2 weeks ago

Originally by @yonran - I had to start a fresh PR and cherry-pick due to some unknown coveralls error on his PR. Description taken from original.

What issue does this pull request resolve?

2427

What changes did you make? Make the discriminator code generation throw MissingRefError when the $ref does not synchronously resolve so that compileAsync can loadSchema and retry.

Is there anything that requires more attention while reviewing? The existing tests create separate test Ajv instances by calling withStandalone(getAjvInstances(AjvClass, …)) (https://github.com/ajv-validator/ajv/blob/v8.13.0/spec/discriminator.spec.ts#L22). I could not directly reuse those Ajv instances because I need to add the loadSchema option and I can’t call compileAsync on an Ajv after withStandalone. For now I only called new _Ajv. To what extent should I try creating separate ajv, ajv2019, standalone instances in the test?