The string type is used for validating strings/texts containing Unicode characters.

{
  "type": "string"
}
Input Status
"some text" valid
"" valid - empty string
12 invalid - is integer/number
null invalid - is null

Validation keywords

The following keywords are supported by the string type, and evaluated in the presented order. All keywords are optional.

minLength

A string is valid against this keyword if its length is greater then, or equal to, the value of this keyword. Value of this keyword must be a non-negative integer.

{
  "type": "string",
  "minLength": 3
}
Input Status
"abc" valid - length = 3
"abcd" valid - length > 3
"ab" invalid - length < 3

maxLength

A string is valid against this keyword if its length is lower then, or equal to, the value of this keyword. Value of this keyword must be a non-negative integer.

{
  "type": "string",
  "maxLength": 3
}
Input Status
"ab" valid - length < 3
"" valid - length < 3
"abc" valid - length = 3
"abcd" invalid - length > 3

pattern

A string is valid against this keyword if it matches the regular expression specified by the value of this keyword. Value of this keyword must be a string representing a valid regular expression.

Please note that the delimiter used by Opis JSON Schema is \x07 (bell) and the modifier is uD (PCRE_UTF8 & PCRE_DOLLAR_ENDONLY).

{
  "type": "string",
  "pattern": "^opis/[a-z-]+$"
}
Input Status
"opis/json-schema" valid
"opis/--" valid
"opis" invalid
"opis/Json-Schema" invalid

For more information about PHP regular expressions, you can read about

contentEncoding

A string is valid against this keyword if it is encoded using the method indicated by the value of this keyword. Value of this keyword must be a string.

* You should enable this keyword for your draft by setting the decodeContent option.

Currently, there can only be three values for this keyword

  • binary - any string is valid
  • base64 - the string must be a valid base64 encoded string
  • quoted-printable - string must be a valid quoted printable text

If you want to add new content encodings, please read about Content Encodings.

{
  "type": "string",
  "contentEncoding": "base64"
}
Input Status
"b3Bpcy9qc29uLXNjaGVtYQ==" valid - decodes to "opis/json-schema"
"opis/json-schema" invalid - the - character is not in the base64 alphabet

contentMediaType

A string is valid against this keyword if its content has the media type (MIME type) indicated by the value of this keyword. If the contentEncoding keyword is also specified, then the decoded content must have the indicated media type. Value of this keyword must be a string.

* You should enable this keyword for your draft by setting the decodeContent option.

Out of the box, Opis JSON Schema knows how to detect application/json and for other media types it uses finfo class as a detector.

If you want to add new media types (MIME types), please read about Media Types.

{
  "type": "string",
  "contentMediaType": "application/json"
}
Input Status
"{\"a\": 1}" valid - JSON object
"[\"a\", \"b\", 2]" valid - JSON array
"\"text\"" valid - JSON string
"null" valid - JSON null
"1-2-3" invalid
"{a: 1}" invalid
"a = 23" invalid
{
  "type": "string",
  "contentEncoding": "base64",
  "contentMediaType": "application/json"
}
Input Status
"eyJhIjogMX0=" valid - decodes to "{\"a\": 1}" which is a JSON object
"bnVsbA==" valid - decodes to "null" which is JSON null
"1-2-3" invalid - not a base64 encoded string
"e2E6IDF9" invalid - decodes to "{a: 1}" which is not a JSON object

contentSchema

A string is valid against this keyword if its content has the application/json media type and the decoded content validates against the schema indicated by this keyword. If the contentEncoding keyword is also specified, then the decoded content is validated against this keyword. Value of this keyword must be a valid json schema (object or boolean).

* You should enable this keyword for your draft by setting the decodeContent option.

{
  "type": "string",
  "contentSchema": {
    "type": "object",
    "required": ["name", "age"]
  }
}
Input Status
"{\"name\": \"Opis\", \"age\": 50}" valid
"1-2-3" invalid - invalid json
"[]" invalid - not an object
"{\"name\": \"Opis\"}" invalid - age property is missing