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

Please note that in order to calculate the length of a string, Opis JSON Schema uses the following libraries/functions, depending which one is available on your system: Opis String (recommended, add it with composer require opis/string), mb_strlen (you must enable mb_string extension for PHP), strlen (will not always return the correct length of a string with 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 u (PCRE_UTF8).

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

Currently, there can only be two values for this keyword

  • binary - any string is valid
  • base64 - the string must be a valid base64 encoded string
{
  "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.

Out of the box, Opis JSON Schema comes with the following media types

  • text/plain - any text
  • application/json - json encoded string

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