Use format comparison keywords like formatMinimum and formatMaximum
masterWhen ajv-formats is used without options or with { keywords: true }, it adds comparison keywords to your Ajv instance. These allow you to define minimum/maximum constraints for formats that support ordering (like date, time, and date-time).
Supported Keywords
formatMinimum/formatMaximumformatExclusiveMinimum/formatExclusiveMaximum
These keywords apply only to strings. If the data is not a string, validation succeeds. The value for these keywords must be a string or a $data reference representing the allowed boundary.
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const ajv = new Ajv()
addFormats(ajv)
const schema = {
type: "string",
format: "date",
formatMinimum: "2016-02-06",
formatExclusiveMaximum: "2016-12-27",
}
const validate = ajv.compile(schema)
console.log(validate("2016-02-06")) // true
console.log(validate("2016-12-26")) // true
console.log(validate("2016-02-05")) // false
console.log(validate("2016-12-27")) // false
console.log(validate("abc")) // true (not a string)