From 1ea6c9b1850a114a14ed2534acbfacca8c498f8d Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Thu, 2 Apr 2015 06:32:06 +1100 Subject: [PATCH] Implement draft 3 divisibleBy constraint based on draft 4 multipleOf constraint --- include/valijson/schema_parser.hpp | 16 ++++++++++++++-- include/valijson/validation_visitor.hpp | 8 ++++++-- tests/test_validator.cpp | 5 +++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/valijson/schema_parser.hpp b/include/valijson/schema_parser.hpp index d017b29..f1abea9 100644 --- a/include/valijson/schema_parser.hpp +++ b/include/valijson/schema_parser.hpp @@ -102,6 +102,14 @@ public: schema.addConstraint(makeDependenciesConstraint(itr->second, deref)); } + if ((itr = object.find("divisibleBy")) != object.end()) { + if (version == kDraft3) { + schema.addConstraint(makeMultipleOfConstraint(itr->second)); + } else { + throw std::runtime_error("'divisibleBy' constraint not valid after draft 3"); + } + } + if ((itr = object.find("enum")) != object.end()) { schema.addConstraint(makeEnumConstraint(itr->second)); } @@ -168,7 +176,11 @@ public: } if ((itr = object.find("multipleOf")) != object.end()) { - schema.addConstraint(makeMultipleOfConstraint(itr->second)); + if (version == kDraft3) { + throw std::runtime_error("'multipleOf' constraint not available in draft 3"); + } else { + schema.addConstraint(makeMultipleOfConstraint(itr->second)); + } } if ((itr = object.find("not")) != object.end()) { @@ -785,7 +797,7 @@ private: /** * @brief Make a new MultipleOfConstraint object. * - * @param node JSON node containing an integer value that a value must + * @param node JSON node containing an numeric value that a value must * be divisible by. * * @return pointer to a new MultipleOfConstraint that belongs to the diff --git a/include/valijson/validation_visitor.hpp b/include/valijson/validation_visitor.hpp index 6cdba3f..446b34b 100644 --- a/include/valijson/validation_visitor.hpp +++ b/include/valijson/validation_visitor.hpp @@ -634,7 +634,9 @@ public: if (!target.asDouble(d)) { if (results) { results->pushError(context, "Value could not be converted " - "to a number for multipleOf check"); + "to a number to check if it is a multiple of " + + boost::lexical_cast( + constraint.multipleOf)); } return false; } @@ -643,7 +645,9 @@ public: if (!target.asInteger(i)) { if (results) { results->pushError(context, "Value could not be converted " - "to a number for multipleOf check"); + "to a number to check if it is a multiple of " + + boost::lexical_cast( + constraint.multipleOf)); } return false; } diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index 8678fcc..427b9f6 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -139,6 +139,11 @@ TEST_F(TestValidator, Draft3_Dependencies) processDraft3TestFile(TEST_SUITE_DIR "draft3/dependencies.json"); } +TEST_F(TestValidator, Draft3_DivisibleBy) +{ + processDraft3TestFile(TEST_SUITE_DIR "draft3/divisibleBy.json"); +} + TEST_F(TestValidator, Draft3_Enum) { processDraft3TestFile(TEST_SUITE_DIR "draft3/enum.json");