CheckMySchema

Structured data explained

How to Specify a JSON Schema and Apply It to a Data File

Declare the draft in the schema file, then connect the schema to an instance through editor settings or your validator. Verify with two small examples.

    Two separate blue and white wooden boxes rest on a bright desk.
    Keep the data and the rules in separate files, then bind them with an explicit setting.
    CheckMySchemaCheckMySchema

    At a glance

    Put the draft URI in the schema document. Tell the editor or validator separately that the data file should use those rules. A two-input test shows whether the intended association is working.

    Write the schema’s first constraints

    A JSON Schema normally lives in its own file. Declare its dialect there. To check a JSON data file, tell your editor or runtime validator which schema to apply. Seeing a schema next to the file in the same folder does not establish that the file has been checked.

    Declare the dialect in the schema document

    Use the official 2020-12 URI as the value of the schema document’s $schema property when you work in that draft. A tiny schema can look like {"$schema":"DRAFT_URI","type":"object","properties":{"days":{"type":"integer","minimum":1}},"required":["days"],"additionalProperties":false}. Replace DRAFT_URI with the full address linked as the Draft 2020-12 meta-schema below. Save it as schemas/report.schema.json and keep a separate data file called report.json.

    The data can be as short as {"days":3}. It contains the value and none of the keywords defining the rules. Put the $schema declaration at the root of the schema document where the validator reads the dialect.

    The schema file declares Draft 2020-12 and the instance file provides a days value.
    Both files use JSON but serve different purposes.

    Two things you have to name

    Schema dialect

    The schema’s $schema URI tells the validator which edition of the vocabulary is in use.

    Instance association

    The editor or validator needs to know which rules to apply to this specific data file.

    Copy the full address for the example from Draft 2020-12 meta-schema.

    For what the dialect URI controls, read Dialect and vocabulary declaration.

    Sources: JSON Schema · Ajv maintainers

    Tell the editor which instance the rules apply to

    In Visual Studio Code, a workspace settings entry in json.schemas can associate the files. For example, use {"fileMatch":["/report.json"],"url":"./schemas/report.schema.json"} as an item in that setting for a workspace with the files at those paths. This association is an editor setting.

    VS Code also supports a special $schema value in a JSON data file for editor association. That convention adds a field to the instance. With the strict property list above, the regular validator treats it as an unexpected property. An association in editor settings keeps the contract of the data file clean.

    An arrow shows a settings association from report.json to a schema file.
    A workspace editor association is separate from runtime validation.
    Two plain blue and white folders lie beside a pen.
    The association tells an editor which set of rules to use for the instance.

    For editor association details, see Editing JSON with Visual Studio Code.

    Sources: Microsoft · JSON Schema

    Run the same fixture through your validator

    Use a validator with 2020-12 support and explicitly pass it the schema and instance. In the fixture, {"days":3} passes and {"days":"3"} fails with type coercion off. An extra label also fails. Check the error path and keyword so you can distinguish an invalid instance from a schema that never loaded.

    Keep a passing and failing example beside the runtime command or test. Matching editor feedback is convenient; the runtime check is the evidence that the rule is applied in your actual workflow.

    Local Draft 2020-12 fixture readback
    InstanceExpectedWhy
    {"days":3}PassThe required integer meets the minimum.
    {"days":"3"}FailA string is not an integer with type coercion off.
    {"days":3,"label":"sample"}FailThe strict property list excludes label.
    The numeric case passes, the string case fails type and an extra property also fails.
    Use the actual validation keyword to trace the problem.

    Check your own association

    • Declare the intended dialect in the schema.
    • Bind it to the target file by a documented route.
    • Run a known-good instance and a known-bad one.
    • Compare the result in the editor and the runtime validator.

    For the implementation’s 2020-12 support, see JSON Schema versions supported by Ajv.

    For the related task, see See how extra properties are rejected.

    Sources: Ajv maintainers · JSON Schema

    Frequently asked questions

    Is $schema on an instance a JSON Schema requirement?

    No. Some editors, including VS Code, use it as an association convention. A strict schema can reject it as an unexpected property.

    Is $id the dialect declaration?

    No. $id identifies a schema resource. $schema in a schema declares its dialect.

    Will editor validation replace runtime validation?

    An editor can flag a mistake while you type. Keep validation at the runtime or CI boundary that actually consumes the data.

    What if my validator cannot find the 2020-12 meta-schema?

    Use the implementation and class that support that draft, and check how the implementation makes meta-schemas available.

    CheckMySchemaCheckMySchema

    Write the schema rules first

    Define the fields, types and required keys before testing.

    Write the schema’s first constraints

    Continue with a related guide

    References