How to specify defaults conditionally with JSON Schema

10,542

Unfortunatly, this is not possible using pure JSON Schema.

JSON Schema validation does not modify the instance data.

The default key word in JSON Schema is an annotation key word. Annotation key words are used to denote information, however they have no validation requirements.

Draft-7 (which is current) says this:

There are no restrictions placed on the value of this keyword. When multiple occurrences of this keyword are applicable to a single sub-instance, implementations SHOULD remove duplicates.

This keyword can be used to supply a default JSON value associated with a particular schema. It is RECOMMENDED that a default value be valid against the associated schema.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-10.2

There is no defined behaviour associated with annotation key words.

Remember, the primary use case for JSON Schema is definition, validation, and annotation.

HOWEVER...

If you are not concerned about the portabliltiy of your schemas, the ajv implementation allows you to use the default value to set the key during validation, but this behaviour is not defined by JSON Schema.

Share:
10,542
nwkeeley
Author by

nwkeeley

Updated on June 07, 2022

Comments

  • nwkeeley
    nwkeeley almost 2 years

    I have a field status.

    If the user is setting the job as draft status I don't want to require the description field - but I DO want to to have a default of empty string.

    If the user is publishing the job than I want the description to be required.

    What I cannot figure out is how in the "oneOf - draft" array to set a default for description.

    Here is my schema

    {
      "schema": "http://json-schema.org/draft-04/schema#",
      "$id": "http://company.com/schemas/job-update.json#",
      "title": "Job",
      "description": "Update job",
      "type": "object",
      "properties": {
        "title": { 
          "type": "string",
          "minLength": 2
        },
        "description": { 
          "type": "string"
         // Can't set default here - as it will apply for the publish status.
        },    
        "status": { 
          "enum": ["draft", "published", "onhold"],
          "default": "draft"
        }
      },
      "oneOf": [
            {
              "description": "Draft jobs do not require any validation",
              "properties": {
                "status": { "enum": ["draft"]}
              },
              "required": [] 
              // SOME WHERE HERE SET DESCRIPTION.default: ""         
            },
            {
              "description": "Published jobs require validation on required fields",
              "properties": {
                "status": { "enum": ["published"]}
              },
              "required": [
                "description"
              ], 
            }        
      ],
      "additionalProperties": false
    }
    
  • nwkeeley
    nwkeeley over 5 years
    Hi @Relequestual - I am actually using ajv and as such I've used defaults in other places apologies - I should have mentioned that - would this be a question then for the ajv node.js repo or might you know how to do this with ajv
  • Relequestual
    Relequestual over 5 years
    OK, great! Sounds like this is just a JSON Schema question then, and not specifically to do with ajv. Can you update your question with a sample JSON instance where you would want the default to be applied please? =]
  • syam
    syam about 3 years
    Some validators (eg. github.com/pboettch/json-schema-validator) return a patch that you can apply to your document if you want default values. Respects the spec and can be made to work as commonly intended. :)