Skip to content
Advertisement

Validate UTC date/date-time with ajv but allow empty strings

I need to validate string schema format: "date" or format: "date-time" to accept only ISO 8601 but also allow empty string “” (the “” requirement should be separately checked using the required schema).

JavaScript

However, the native ajv format parser does not allow empty string “” to pass. How do I make it allow empty string, and still validate ISO 8601?

Do I really have to write my own format checker using ajv.addFormat()? I also have the same issue for "format": "email", where I also need “” to be valid.

Note: I can’t modify the schema, so I can’t add defaults.

Advertisement

Answer

Lets try to use oneOf

JavaScript

in the first object, it will allows empty string. In the second object, it will check input based on your format

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement