String type

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

{
  "type": "string"
}

"some text" - valid

"" - valid (empty string)

12 - invalid (is integer/number)

null - invalid (is null)

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).

Validation keywords

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

  1. minLength
  2. maxLength
  3. pattern
  4. contentEncoding
  5. contentMediaType

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
}

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

"ab" - valid (length < 3)

"" - valid (length < 3)

"abcd" - invalid (length > 3)

"abc" - valid (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-]+$"
}

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

"b3Bpcy9qc29uLXNjaGVtYQ==" - valid (decodes to "opis/json-schema")

"opis/json-schema" - invalid (- 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"
}

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

"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)