CheckMySchema

Structured data explained

What Does JSON Schema const Mean? Test It With a Version Field

Try two JSON Schema branches keyed to exact version values. See why a quoted, missing or unsupported version fails the local fixture.

    Plain colored version tabs sit in a folder beside a keyboard.
    Give each expected format an identifiable tag.
    CheckMySchemaCheckMySchema

    At a glance

    Use a constant to accept the exact tag a branch expects. In the example, version 1 carries label and version 2 carries title. A quoted tag and a missing tag are separate test cases.

    Check the complete schema against fixtures

    In JSON Schema, a property with a const of 2 accepts the JSON number 2 there. It rejects the string “2”. You can use that exact test on a version tag to route data to a corresponding shape. Use required when every object has to carry the version explicitly.

    Pair each version with the fields it names

    Imagine a report file with two versions. Version 1 carries a string called label. Version 2 uses a string called title. Put version in the object’s required list. Put the version 1 constant and the label requirement in one branch. Put the version 2 constant and title requirement in a second branch. The top-level oneOf accepts records matching exactly one branch.

    This little design gives you a clear question for each branch. Did the source send that exact version, and does the record have the fields that go with it? It also gives you a way to tell whether a report had the right shape even if its author decided to keep the same word for the displayed title.

    Version 1 requires a string label, while version 2 requires a string title.
    Require the version on the object and give each branch its own exact tag.

    Check what each rule does

    const

    Checks the exact version value where the subschema applies.

    required

    Checks that the named property is present on the object.

    For how the schemas combine, see Draft 2020-12 core specification.

    Sources: Draft 2020-12 validation vocabulary · Draft 2020-12 core specification

    Try a missing version and a quoted version

    The local Draft 2020-12 fixture accepts version 1 with a string label and version 2 with a string title. A version 2 number with just a label fails because that branch requires title. The quoted “2” fails the exact comparison. A payload with only title also fails because the outer object requires version. Keep those failures in the test suite before switching any data producer.

    The const keyword compares a value where its schema applies. In an object’s properties map that property-specific schema only applies when the property is present. Without an explicit presence rule elsewhere, that check can be skipped for a missing property. It also cannot turn the string “2” into the number 2, or fill in a version that the producer omitted.

    Actual outcomes from the local Ajv Draft 2020-12 fixture.
    Candidate JSONValid?Why
    {"version":1,"label":"Summary"}YesVersion 1 has its label
    {"version":2,"title":"Summary"}YesVersion 2 has its title
    {"version":"2","title":"Summary"}NoString does not equal numeric 2
    {"version":2,"label":"Summary"}NoVersion 2 requires title
    {"title":"Summary"}NoVersion is required
    {"version":3,"title":"Summary"}NoNo supported branch matches
    Four rows show the accepted numeric version plus failures for a quoted value, missing title and missing version.
    The source of the error depends on which part of the contract the value violates.

    For const equality and property presence requirements, see Draft 2020-12 validation vocabulary.

    Sources: Draft 2020-12 validation vocabulary · Draft 2020-12 core specification

    Keep the schema rule next to the producer’s contract

    Write down who sets the version and when a new tag is introduced. If a producer needs to accept old files, test samples of both versions before changing a name. Also test how the application handles a missing or unsupported version so an error points to the real problem.

    This small schema allows additional fields. That can be useful when the versions can carry extra metadata. If your reader requires a closed shape, decide how that should be expressed and test it separately. Keep the exact-value, presence and extra-field checks visible so later readers can tell which requirement each test covers.

    Three rows explain the distinct roles of required, const and oneOf.
    Keep a failing fixture for each layer of the contract.
    Two blank index tabs are laid out on a blue work surface.
    Keep each supported version documented with its required fields.

    Test the boundary before changing the producer

    • Declare the Draft 2020-12 dialect.
    • List the exact version tags the consumer accepts.
    • Require the tag and the needed fields for each branch.
    • Test quoted, unknown and missing tags.

    For default annotations in standard validation, see Schema annotations and default.

    For the related task, see Compare boolean, enum and const.

    Sources: Schema annotations and default · Draft 2020-12 validation vocabulary

    Frequently asked questions

    Can const set a missing property automatically?

    It checks a value when its schema is applied. The value must be present for the properties map to check it.

    Will oneOf accept two passing branches?

    It requires exactly one passing branch.

    What if I want to accept three permitted values in the same shape?

    Use an enum when the identical rule has several accepted literals. Use branches when the shapes differ.

    Can I include extra fields?

    The shown fixture allows extra fields. Add and test a distinct rule if the producer needs a closed shape.

    CheckMySchemaCheckMySchema

    Validate the schema and test its instances

    Use the correct dialect for the schema and then run actual examples.

    Check the complete schema against fixtures

    Continue with a related guide

    References