CheckMySchema

Structured data explained

Disallow Extra JSON Properties After allOf: A Tested Draft 2020-12 Example

Compare additionalProperties in one allOf branch with unevaluatedProperties at the composed object. Test known, extra, missing and invalid fields.

    Two translucent sheets inside a single frame
    The illustration shows the shared outer boundary of two branches.
    CheckMySchemaCheckMySchema

    At a glance

    For fields composed with allOf in Draft 2020-12, keep the branches open and put unevaluatedProperties false on the outer object. A false additionalProperties inside just one branch can reject a field declared only in another branch. Run one valid and several invalid examples to check the difference.

    Read the additionalProperties scope guide

    To disallow extras in a single JSON Schema object, put additionalProperties false beside the allowed properties. In an object assembled with allOf, work out which evaluations can see each property before closing it. This checked Draft 2020-12 example uses two branches.

    Check the scope of the first closure rule

    Branch A defines a string ticket and requires it. Branch B declares stage with two allowed values, open and closed, and requires it. The outside schema has type object and an allOf array containing A and B. When additionalProperties is false in branch A, its local property list does not contain stage. The instance with ticket T17 and stage open fails even though stage meets branch B.

    Branch A allows ticket and closes locally. Branch B declares stage. The first branch evaluates stage as extra.
    A branch’s additionalProperties rule looks at that branch’s own declarations.

    Read the schema project’s extending-closed-schemas example Object reference: extending closed schemas.

    Sources: Object reference: extending closed schemas · Draft 2020-12 Core, unevaluatedProperties

    Move closure to the composed object

    Remove the branch A closure and set unevaluatedProperties false at the same level as allOf. The first instance now passes. Both present properties were evaluated by successful branches. A debug field that neither branch covers still fails under the outer closure. Keep the same required arrays in both variants so you can see that the new closure does not make stage optional.

    Two open branches sit inside a shared object with an unevaluatedProperties closure.
    A valid ticket and stage pair is evaluated across the successful branches.
    Two translucent sheets above an empty outline frame
    Close the object at the level that can see the successful evaluations.

    Choose the closure rule by object shape.

    One declaration scope

    Use additionalProperties false when the allowed names or patterns are all declared alongside it.

    Two open allOf branches

    Use unevaluatedProperties false on the outer schema in Draft 2020-12 if you want to close after those successful evaluations.

    Check the Draft 2020-12 definition Draft 2020-12 Core, unevaluatedProperties.

    Sources: Draft 2020-12 Core, unevaluatedProperties · Object reference: extending closed schemas

    Run both valid and invalid cases

    The local Ajv 8.20.0 fixture used the Draft 2020-12 validator with all errors reported and strictTypes disabled for the branch objects. The known pair passed only the outer schema. The extra debug property, missing stage and invalid value next failed in both variants for the reasons shown in the table. Use the same record for both schemas so a changed instance does not hide a change in scope. If a valid input fails, record the keyword and schema path so you can see which branch rejected it.

    Actual local Ajv 8.20.0 results for the two Draft 2020-12 schemas.
    InstanceClosed branchOuter closure
    ticket T17 and stage openFails: stage is extra in branch APasses
    Both known fields plus debug trueFailsFails: debug is unevaluated
    ticket present, stage missingFails: requiredFails: required
    ticket T17, stage nextFailsFails: stage value
    The expected known pair passes while extra, missing and invalid cases all fail.
    The result depends on the complete rules and the actual draft in use.

    Reproduce the matrix

    • Select the Draft 2020-12 validator.
    • Close only branch A and test both named properties.
    • Open branch A and close the outer object instead.
    • Check a valid pair, unknown property, missing field and invalid value.

    Check which Ajv validator supports the dialect JSON Schema support in Ajv.

    For the related task, see Test overlapping property rules.

    Sources: Ajv · JSON Schema

    Frequently asked questions

    Does allOf merge properties into one declaration?

    It evaluates the instance against each branch. Do not depend on sibling declarations becoming local to a closed branch.

    Can I leave additionalProperties false inside the base and add outer closure?

    The base can still fail independently. Move the closure out if the design calls for extension through that branch.

    Does unevaluatedProperties require required as well?

    Use required to assert that a field exists. Unevaluated closure restricts properties according to evaluation and answers a different question.

    Can the same fixture be run as Draft 7?

    This fixture’s outer closure uses Draft 2020-12. Choose the matching dialect before evaluating it. A Draft 7 schema can close a single scope with additionalProperties instead.

    CheckMySchemaCheckMySchema

    Read the additionalProperties scope

    Check how a single declaration scope handles extra fields.

    Read the additionalProperties scope guide

    Continue with a related guide

    References