2

Would like to allow null on an optional date property where the date format is validated with a regex expression. Is this even possible?

"dateOfRetirement": {
  "description": "Optional. Format: yyyy-MM-dd.",
  "type": ["string", "null"],
  "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
} 
0

2 Answers 2

3

To get the regular syntax for that you have to add a condition to your regex.

Your regex will get (assuming your regex syntax has no error!):

^(\\d{4}-\\d{2}-\\d{2}|null)$

Steps done:

  • incapsulate the normal regex with brackets (())
  • add an or-operator to the regex (|)
  • add the second validation for null to the regex after the or-operator

In the end the regex will allow a-valid-date-format or null as text.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, thanks! I updated the JSON as well to change: "dateOfRetirement" : "" to: "dateOfRetirement" : null,
that is wrong. The schema in the question is ok, if the value is null the pattern keyword will be ignored.
0

I don't think that will work when "column": null.

It will only account for "column": "null" in regex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.