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

Schema validation with remote specs #16

Open
utrack opened this issue Jan 17, 2023 · 1 comment
Open

Schema validation with remote specs #16

utrack opened this issue Jan 17, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@utrack
Copy link

utrack commented Jan 17, 2023

Hi Ferdinand, thanks for maintaining this fork! Looks p awesome :)

Can you clarify how do I actually run the schema validation for me, please?

What I've got:

data JSON (example):

{
    "$schema": "./../json.schema/form.schema.json",
    "form": [
        {
            "type": "string"
        }
    ]
}

../json.schema/form.schema.json JSON:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "form.schema.json",
  "title": "Form",
  "description": "A form description",
  "type": "array",
  "minItems": 1,
  "properties": {
    "form": {
      "items": {
        "oneOf": [
          {
            "$ref": "string.schema.json"
          },
          {
            "$ref": "number.schema.json"
          },
          {
            "$ref": "phone.schema.json"
          }
        ]
      }
    }
  }
}

Expected:

jsonlint ./example.json -V
($schema is read automatically, error about form[0] not having "name" property)

Actual:

jsonlint ./example.json -V ../json.schema/form.schema.json                                                                                                                                                                                 
File: form.json
Loading the JSON schema failed: "../json.schema/form.schema.json".
Compiling the JSON schema failed.
no schema with key or ref "https://json-schema.org/draft/2020-12/schema"

I've tried setting the json-schema ref to https://json-schema.org/draft-07/schema but the error for that was the same.

@prantlf
Copy link
Owner

prantlf commented Mar 5, 2023

Thanks for the kind words, @utrack! :-)

I myself used only definitions inside a single schema file. (A hand-written Swagger API description) I looked at it and had to fix two problems:

  • It wasn't possible to specify multiple schema files. I made the validate option a multi-value option.
  • The JSON Schema draft 2020-12 wasn't supported by the integrated version of AJV. The newer version of AJV dropped support of JSON Schema draft 04. I integrated the newer AJV version in addition to support the newer JSON Schema drafts.

Once you upgrade to 14.0.0, you'll be able to validate your data by specifying all schemas on the command line:

jsonlint ./example.json -V ../json.schema/form.schema.json \
   -V ../json.schema/string.schema.json -V ../json.schema/number.schema.json \
   -V ../json.schema/phone.schema.json

The schemas can be passed by a single -V too, if delimited by commas.

Sub-schemas aren't loaded automatically, because they are passed to the validator as schema content. Not as paths to files with the schemas. There's no concept of a base and relative URLs in AJV, because the original paths are lost.

However, the command-line script knows, what were the paths of schema files. It could load the schemas and if it detects a $ref reference, which hasn't been loaded explicitly and which has its URL based on the same URL as the parent schema ($id), it could try loading it automatically. And the same for the main schema referred from the data file ($schema). Let me try it.

prantlf referenced this issue Mar 7, 2023
* A schema can be a string|object or an array pf them.
* The main schema, if there is such, should be passed as the first one
  to be immediately compiled.
* Fix multivalue argument parsing to accept both comma-delimited items
  and multiple arguments. Unfortunately, data arguments have to be
  separated by "--".
* Accept schemas either as strings or as already parsed objects.
* Update the readme with the extended JSON Schema support.
prantlf referenced this issue Mar 7, 2023
Support JSON Schema draft 04 by a more modern package.
prantlf referenced this issue Mar 7, 2023
…finition

Upgrade AJV to the latest version and retain the previous AJV@6
to be able to suport JSON Schema draft 04.

BREAKING CHANGE: The default environment recognises only JSON Schema drafts 06 and 07 automatically. Not 04 any more. The environment for JSON Schema drafts 04 has to be selected explicitly. Also, JSON Schema drafts 06 and 07 are handled by AJV@8 instead of AJV@6. It shouldn't make any difference, but the implementation is new and could perform a stricter validation.
@prantlf prantlf added the enhancement New feature or request label Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants