From d5091b2dbb58a8b26e305f5422adb99f6dd57f29 Mon Sep 17 00:00:00 2001 From: Samuel <92.reader@gmail.com> Date: Wed, 7 Oct 2020 20:16:30 +0900 Subject: [PATCH] added feature : now users can get a detailed error message when if-then-else conditional constraints are not met --- include/valijson/validation_visitor.hpp | 26 +++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/include/valijson/validation_visitor.hpp b/include/valijson/validation_visitor.hpp index 610e026..9e17df9 100644 --- a/include/valijson/validation_visitor.hpp +++ b/include/valijson/validation_visitor.hpp @@ -178,17 +178,35 @@ public: */ bool visit(const ConditionalConstraint &constraint) override { + ValidationResults newResults; + ValidationResults* conditionalResults = (m_results) ? &newResults : nullptr; + // Create a validator to evaluate the conditional ValidationVisitor ifValidator(m_target, m_context, m_strictTypes, nullptr, m_regexesCache); - ValidationVisitor thenElseValidator(m_target, m_context, m_strictTypes, nullptr, m_regexesCache); + ValidationVisitor thenElseValidator(m_target, m_context, m_strictTypes, conditionalResults, m_regexesCache); + bool validated = false; if (ifValidator.validateSchema(*constraint.getIfSubschema())) { const Subschema *thenSubschema = constraint.getThenSubschema(); - return thenSubschema == nullptr || thenElseValidator.validateSchema(*thenSubschema); + validated = thenSubschema == nullptr || thenElseValidator.validateSchema(*thenSubschema); + } + else + { + const Subschema *elseSubschema = constraint.getElseSubschema(); + validated = elseSubschema == nullptr || thenElseValidator.validateSchema(*elseSubschema); } - const Subschema *elseSubschema = constraint.getElseSubschema(); - return elseSubschema == nullptr || thenElseValidator.validateSchema(*elseSubschema); + if (!validated && m_results) + { + ValidationResults::Error conditionalError; + while (conditionalResults->popError(conditionalError)) + { + m_results->pushError(conditionalError.context, conditionalError.description); + } + m_results->pushError(m_context, "Failed to validate against a conditional schema set by if-then-else constraints."); + } + + return validated; } /**