2

I would like to know if it is possbile to specify in a json schema that a string is of format date-time and is restricted to a specific interval.

In my case, I need to validate that the year of the submitted date is within the interval 2006 to 2012.

sherle
  • 23
  • 1
  • 5

1 Answers1

4

The next version of the standard (v5) has the proposed keywords formatMaximum/formatMinimum which would do exactly what you wanted, in an elegant way: they combine with format to provide format-specific min/max.

Currently (with v4), your only option is to create a hack using pattern (e.g. ^20(0[6-9]|1[0-2])). However, this is completely horrible, and best avoided if possible.

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • Hi, do you know when will be available to use formatMaximum/formatMinimum? I've tried some examples (here https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#formatmaximum--formatminimum-and-exclusiveformatmaximum--exclusiveformatminimum-v5-proposal), but output is always as valid json, even if date is out of range. Also I've enabled strictProperties flag. Do you know if I miss something? – Zilev av Mar 03 '16 at 18:55
  • 1
    Can you update your answer please? The link is dead and the standard is at v7. Is this now implemented? – Vampire Jun 30 '20 at 08:14
  • Maybe use regexp keyword - https://stackoverflow.com/questions/45888524/empty-values-validation-in-json-schema-using-ajv/62971172#62971172 – rofrol Jan 11 '21 at 20:32