prantlf / jsonlint

JSON/CJSON/JSON5 parser, syntax & schema validator and pretty-printer with a command-line client, written in pure JavaScript.
http://prantlf.github.io/jsonlint/
MIT License
38 stars 9 forks source link

Schema validation with remote specs #16

Open utrack opened 1 year ago

utrack commented 1 year ago

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 commented 1 year ago

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:

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.