Default value

Default value keyword

You can specify a default value for an item using the default keyword. When a data doesn’t have a corresponding value, the value of this keyword will be used instead to do the validation checks. This keyword is not mandatory and the value of this keyword can be anything.

The default keyword support is enabled by default. To disable it, just pass false to the Opis\JsonSchema\Validator::defaultSupport static method.

{
    "type": "object",
    "properties": {
        "prop1": {
            "type": "string",
            "default": "test"
        }
    }
}
Input Status
{"prop1": "string"} valid
{} valid - the default value for prop1 is used to validate the property
{"prop1": 5} invalid - not a string

Please pay attention when using default! The value of the keyword must pass the validations!

{
  "type": "object",
  "properties": {
    "prop1": {
      "type": "string",
      "default": 5
    }
  }
}
Input Status
{"prop1": "string"} valid
{} invalid - the default value is not a string
{"prop1": null} invalid - null is not a string