Formats

The format keyword performs a semantic validation on data. The value of this keyword must be a string, representig a format. The keyword behavior depends on the data type, meaning that the same format name for a string behaves differently on a number, or is missing, because not all data types must implement a format and usually different data types have different formats.

{
    "format": "date"
}

"1970-01-01" - valid

510 - valid (not a string)

"test" - invalid (not a date)

Provided formats

Opis Json Schema provides all the formats for string type defined in json schema specifications.

Please note that formats starting with idn- or iri require PHP intl extension to work correctly.

  1. data
  2. time
  3. date-time
  4. regex
  5. email
  6. idn-email
  7. hostname
  8. idn-hostname
  9. ipv4
  10. ipv6
  11. json-pointer
  12. relative-json-pointer
  13. uri
  14. uri-reference
  15. uri-template
  16. iri
  17. iri-reference

date

A string is valid against this format if it represents a date in the following format: YYYY-MM-DD.

{
    "type": "string",
    "format": "date"
}

"1970-01-01" - valid

"Jan. 1st, 1970" - invalid

time

A string is valid against this format if it represents a time in the following format: hh:mm:ss.sTZD.

{
    "type": "string",
    "format": "time"
}

"10:05:08" - valid

"10:05:08.5" - valid

"10:05:08+01:00" - valid

"10:05:08-02:30" - valid

"10:05:08Z" - valid

"45:60:62" - invalid

"10:05" - invalid

"1 p.m. - invalid

date-time

A string is valid against this format if it represents a date-time in the following format: YYYY:MM::DDThh:mm:ss.sTZD.

{
    "type": "string",
    "format": "date-time"
}

"1970-01-01T10:05:08" - valid

"1970-01-01T10:05:08.10" - valid

"1970-01-01T10:05:08+01:00" - valid

"Jan. 1st, 1970 at 1 p.m." - invalid

regex

A string is valid against this format if it represents a valid regular expression.

{
    "type": "string",
    "format": "regex"
}

"^[a-z]+$" - valid

"a/b" - invalid (slash is not escaped)

"(a" - invalid (incomplete group)

email

A string is valid against this format if it represents a valid e-mail address format.

{
    "type": "string",
    "format": "email"
}

"john@example.com" - valid

"john(at)example.com" - invalid

idn-email

A string is valid against this format if it represents a valid idn e-mail address format.

{
    "type": "string",
    "format": "idn-email"
}

"실례@실례.테스트" - valid

"john@example.com" - valid

"1234" - invalid

hostname

A string is valid against this format if it represents a valid hostname.

{
    "type": "string",
    "format": "hostname"
}

"www.example.com" - valid

"xn--4gbwdl.xn--wgbh1c" - valid

"not_a_valid_host_name" - invalid

idn-hostname

A string is valid against this format if it represents a valid IDN hostname.

{
    "type": "string",
    "format": "idn-hostname"
}

"실례.테스트" - valid

"〮실례.테스트" - invalid

ipv4

A string is valid against this format if it represents a valid IPv4 address.

{
    "type": "string",
    "format": "ipv4"
}

"192.168.0.1" - valid

"192.168.1.1.1" - invalid

ipv6

A string is valid against this format if it represents a valid IPv6 address.

{
    "type": "string",
    "format": "ipv6"
}

"::1" - valid

"12345::" - invalid

json-pointer

A string is valid against this format if it represents a valid (absolute) json pointer.

{
    "type": "string",
    "format": "json-pointer"
}

"/a/b/c" - valid

"/a/~" - invalid

relative-json-pointer

A string is valid against this format if it represents a valid relative json pointer.

{
    "type": "string",
    "format": "relative-json-pointer"
}

"0/a/b" - valid

"5/a/b#" - valid

"2#" - valid

"/a/b" - invalid

uri

A string is valid against this format if it represents a valid uri.

{
    "type": "string",
    "format": "uri"
}

"http://example.com/path?qs=v&qs2[1]=3#fragment" - valid

"http://a_example.com" - invalid

"aaa/bbb.html" - invalid

uri-reference

A string is valid against this format if it represents a valid uri or uri reference.

{
    "type": "string",
    "format": "uri-reference"
}

"aaa/bbb.html" - valid

"?a=b" - valid

"#fragment" - valid

"http://example.com" - valid

"http://a_example.com" - invalid

uri-template

A string is valid against this format if it represents a valid uri template or uri-reference.

{
    "type": "string",
    "format": "uri-template"
}

"/{+file}.html" - valid

"http://example.com/dictionary/{term:1}/{term}" - valid

"{?q,lang}" - valid

"http://a_example.com/file.php{?q,r}" - invalid

iri

A string is valid against this format if it represents a valid IRI.

{
    "type": "string",
    "format": "iri"
}

"http://ƒøø.ßår/?∂éœ=πîx#πîüx" - valid

"http://ƒøø.com/blah_(wîkïpédiå)_blah#ßité-1" - valid

"http:// ƒøø.com" - invalid

iri-reference

A string is valid against this format if it represents a valid IRI reference.

{
    "type": "string",
    "format": "iri-reference"
}

"//ƒøø.ßår/?∂éœ=πîx#πîüx" - valid

"#ƒrägmênt" - valid

"http://ƒøø.com/blah_(wîkïpédiå)_blah#ßité-1" - valid

"\\\\WINDOWS\\filëßåré - invalid