-
-
Notifications
You must be signed in to change notification settings - Fork 70
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 compile
command: add schemas before compiling them to allow refs between schemas
#43
Comments
Forgot to attach the samples: schemas.tar.gz |
@jthomerson thank you, that’s very detailed. I agree with the change. To-do:
PR is welcome. |
ajv compile
command: add schemas before compiling them to allow refs between schemas
This seems to still be happening. Only difference is that when I add the -r command as @jthomerson commented, I get a ton of "already exist" errors and schemas are now invalid. |
@joaomcarlos Did you figure it out? |
@lengors I dont recall exactly, but here is what I use to verify schemas # Validate the JSON schemas
validate-schemas:
stage: validate
image: node:16-alpine
variables:
IFS: ""
before_script:
- npm install -g ajv-cli@5.0.0
script: # Validate all JSON files in the repository
- ajv compile --strict-types=false --validate-formats=false -s **/*.json |
Thanks, Ill see if that works out for me. Or if I'll have to fork the project as it seems inactive 😅 |
If you use
ajv-cli
to compile multiple schemas, it appears to process them in alphabetical order. If one of the earlier schemas references a later schema, the compilation will fail because the later schema has not yet been validated. You can work around that (appropriately) by adding the-r '**/*.json'
flag so that all the referenced schemas are added to AJV first.This seems non-intuitive (at least it tripped me up for an hour) because you can have some folders of schemas that work just fine, and then just by adding a reference to a schema that comes later in the alphabetical order, the schema resolution seems to stop working.
My suggestion would be that if a glob is passed to the compile step, that all schemas in the glob are first added to AJV, and then they are compiled after that, so that the order of them appearing in the glob does not matter, and users do not have to reference them again using the
-r
flag.For example:
ajv compile -s '**/*.json'
.auth-auth/token-with-nonce.json
failed because it couldn't resolve a schema it depended on (that came later in the glob).mv auth-auth zzzz
ajv compile -s '**/*.json'
again. Everything worked this time because the dependencies happened to be in the right order.Here's the output of that exercise:
And if I had specified the glob twice, it would've worked (with the original folder structure):
If you don't agree with the proposed solution of always doing the
addSchema
step on all schemas before doing thecompile
step, perhaps at least the documentation could emphasize the need for the-r
flag, which may be missed by newcomers (at least I missed it, and didn't realize it until I read the AJV code and made my own test case using AJV directly).Thanks!
The text was updated successfully, but these errors were encountered: