santhosh-tekuri/jsonschema

Feature: Strict Validation

Closed this issue · 6 comments

I'm switching from Ajv, which has a number of strict options that I will miss. Any chance support for some of these could be added? Will help prevent me from making typos like descripxtion.

Ajv's strict mode being on by default has caused a lot of problems and misunderstandings because it violates quite a few JSON Schema requirements. Problems caused by strict mode show up on stackoverflow a lot, I hear.

Most of what "strict mode" does should be handled by a linter rather than integrated into schema evaluation (although a one-step lint-and-evaluate that was opt-in could work well).

You can easily detect typos like descripxtion with the following meta-schema:

{
    "$id": "https://example.com/strict-meta-schema",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$vocabulary": {
        "https://json-schema.org/draft/2020-12/vocab/core": true, 
        "https://json-schema.org/draft/2020-12/vocab/applicator": true, 
        "https://json-schema.org/draft/2020-12/vocab/unevaluated": true, 
        "https://json-schema.org/draft/2020-12/vocab/validation": true, 
        "https://json-schema.org/draft/2020-12/vocab/meta-data": true, 
        "https://json-schema.org/draft/2020-12/vocab/format-annotation": true, 
        "https://json-schema.org/draft/2020-12/vocab/content": true
    },    
    "$dynamicAnchor": "meta",

    "$ref": "https://json-schema.org/draft/2020-12/schema",
    "unevaluatedProperties": false
}

The $vocabulary and $dynamicAnchor part is boilerplate. It's just the $ref and unevaluatedProperties that do the work here.

Validating against such as meta-schema as a build step (even if your schema doesn't declare this meta-schema as it's proper meta-schema) means you don't incur the cost of extra checks at runtime.

UGH 🤦 that should have been "unevaluatedProperties": false — I corrected it but adding a comment for email notifications.

Thanks! I can confirm that works because it choked on my extension 😂. In order to get it to validate schemas with my extension, I guess I need to add it to the metaschema somehow, yes?

I think I got it:

{
    "$id": "https://example.com/strict-meta-schema",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$vocabulary": {
        "https://json-schema.org/draft/2020-12/vocab/core": true, 
        "https://json-schema.org/draft/2020-12/vocab/applicator": true, 
        "https://json-schema.org/draft/2020-12/vocab/unevaluated": true, 
        "https://json-schema.org/draft/2020-12/vocab/validation": true, 
        "https://json-schema.org/draft/2020-12/vocab/meta-data": true, 
        "https://json-schema.org/draft/2020-12/vocab/format-annotation": true, 
        "https://json-schema.org/draft/2020-12/vocab/content": true
    },    
    "$dynamicAnchor": "meta",
    "unevaluatedProperties": false,
    "allOf": [
       {  "$ref": "https://json-schema.org/draft/2020-12/schema" },
       { "$ref": "file://absolute/path/to/my-extension.schema.json" }
    ]
}

@theory yup, that looks good! Although you should use something other than example.com for your $id 😜

LOL, yeah, that was just for public consumption :-)