ajv-validator / ajv-validator/ajv-cli

`ajv compile` command: add schemas before compiling them to allow refs between schemas

Đang mở
#43 6 bình luận 2 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
TypeScript
Star
316
Fork
77
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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:

1. Take the attached folder of schemas and untar it on your workstation.
2. From the untarred schemas folder, run `ajv compile -s '**/*.json'`.
3. You'll see that `auth-auth/token-with-nonce.json` failed because it couldn't resolve a schema it depended on (that came later in the glob).
4. `mv auth-auth zzzz`
5. Run `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:

```
jrthomer@work:/tmp/foo/schemas$ ajv compile -s '**/*.json'
schema auth-auth/token-with-nonce.json is invalid
error: can't resolve reference ../core/jwt.json from id https://example.com/schemas/auth-auth/token-with-nonce.json#
schema core/jwt.json is valid
schema core/uuid.json is valid

jrthomer@work:/tmp/foo/schemas$ mv auth-auth zzzz

jrthomer@work:/tmp/foo/schemas$ ajv compile -s '**/*.json'
schema core/jwt.json is valid
schema core/uuid.json is valid
schema zzzz/token-with-nonce.json is valid
```

And if I had specified the glob twice, it would've worked (with the original folder structure):

```
jrthomer@work:/tmp/foo/schemas$ ajv compile -r '**/*.json' -s '**/*.json'
schema auth-auth/token-with-nonce.json is valid
schema core/jwt.json is valid
schema core/uuid.json is valid
```

If you don't agree with the proposed solution of always doing the `addSchema` step on all schemas before doing the `compile` 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!

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.