am1988
am1988

Reputation: 1

Validation fails for string property with pattern

I have a JSON schema

{
"type": "object",
"required": ["version"],
"additionalProperties": false,
"properties": {
    "version": {
        "type": "string",
        "format": "date-time",
        "examples": ["2020-08-20T13:57:33.034Z"],
        "pattern": "^.{27}$"
    }
}}

When I validate the data

{"version": "2025-01-28T07:24:28.090090Z"}

it fails with an error

Found 1 error(s) Message: String '2025-01-28T07:24:28.09009Z' does not match regex pattern '^.{27}$'. Schema path: #/properties/version/pattern

enter image description here

Upvotes: 0

Views: 45

Answers (1)

Jeremy Fiel
Jeremy Fiel

Reputation: 3307

Your string is not 27 characters.

try {26} and it will validate

format keyword is not validated by default per the JSON Schema Specification. It's what they call an "annotation", in other words, informational only.

Depending on the implementation you are using, there may exist a flag or options to enable format validation.

EDIT:

more clarification on the simplified ISO8601 date time format:

24 or 27 characters long YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ

  • YYYY is the year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year.
  • MM is the month, with two digits (01 to 12). Defaults to 01.
  • DD is the day of the month, with two digits (01 to 31). Defaults to 01.
  • T is a literal character, which indicates the beginning of the time part of the string. The T is required when specifying the time part.
  • HH is the hour, with two digits (00 to 23). As a special case, 24:00:00 is allowed, and is interpreted as midnight at the beginning of the next day. Defaults to 00.
  • mm is the minute, with two digits (00 to 59). Defaults to 00.
  • ss is the second, with two digits (00 to 59). Defaults to 00.
  • sss is the millisecond, with three digits (000 to 999). Defaults to 000.
  • Z is the timezone offset, which can either be the literal character Z (indicating UTC), or + or - followed by HH:mm, the offset in hours and minutes from UTC.

src: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format


Your example data does not follow the ISO format. second ss and millisecond sss are only 2 and 3 digits, respectively. Your example has 4 digits, without the optional separator. I believe the validator is dropping the trailing zero because of that reason

Upvotes: 0

Related Questions