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

Feature: Strict Validation #64

Closed
theory opened this issue Jun 6, 2022 · 6 comments
Closed

Feature: Strict Validation #64

theory opened this issue Jun 6, 2022 · 6 comments

Comments

@theory
Copy link

theory commented Jun 6, 2022

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.

@handrews
Copy link

handrews commented Jun 15, 2022

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.

@handrews
Copy link

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

@theory
Copy link
Author

theory commented Jun 15, 2022

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?

@theory
Copy link
Author

theory commented Jun 15, 2022

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" }
    ]
}

@handrews
Copy link

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

@theory
Copy link
Author

theory commented Jun 24, 2022

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

@theory theory closed this as completed Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants