Tweak behaviour of format constraint validation

This commit is contained in:
Tristan Penman 2023-08-24 09:37:44 +10:00
parent 92b9514bde
commit 138c3785ef

View File

@ -356,6 +356,25 @@ public:
*/
bool visit(const FormatConstraint &constraint) override
{
//
// Don't attempt to cast validate the format constraint unless the
// target value is known to be a string. Drafts 4-7 of the spec
// suggest that 'format' should be treated as an annotation rather
// than an assertion, however this is not guaranteed. Given that we
// have been treating it as an assertion here, failing quietly on
// non-string values seems like the right thing to do, to avoid
// this throwing an exception.
//
// Schemas that need tighter validation around 'format' constaints
// should generally pair it with a 'type' constraint.
//
// Reference:
// https://json-schema.org/understanding-json-schema/reference/string.html#format
//
if (!m_target.maybeString()) {
return true;
}
const std::string s = m_target.asString();
const std::string format = constraint.getFormat();
if (format == "date") {