Default value

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.

default keyword support is enabled by default, to disable it use Opis\JsonSchema\Validator::defaultSupport(false).

{
  "type": "object",
  "properties": {
    "prop1": {
      "type": "string",
      "default": "test"
    }
  }
}

{"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
    }
  }
}

{"prop1": "string"} - valid

{} - invalid (default value is not a string)

{"prop1": null} - invalid (null is not a string)