Giter Club home page Giter Club logo

Comments (6)

handrews avatar handrews commented on May 15, 2024

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.

from jsonschema.

handrews avatar handrews commented on May 15, 2024

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

from jsonschema.

theory avatar theory commented on May 15, 2024

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?

from jsonschema.

theory avatar theory commented on May 15, 2024

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

from jsonschema.

handrews avatar handrews commented on May 15, 2024

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

from jsonschema.

theory avatar theory commented on May 15, 2024

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

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.