Implement draft 3 divisibleBy constraint based on draft 4 multipleOf constraint

This commit is contained in:
Tristan Penman 2015-04-02 06:32:06 +11:00
parent 04cebebcfd
commit 1ea6c9b185
3 changed files with 25 additions and 4 deletions

View File

@ -102,6 +102,14 @@ public:
schema.addConstraint(makeDependenciesConstraint(itr->second, deref)); 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()) { if ((itr = object.find("enum")) != object.end()) {
schema.addConstraint(makeEnumConstraint(itr->second)); schema.addConstraint(makeEnumConstraint(itr->second));
} }
@ -168,7 +176,11 @@ public:
} }
if ((itr = object.find("multipleOf")) != object.end()) { 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()) { if ((itr = object.find("not")) != object.end()) {
@ -785,7 +797,7 @@ private:
/** /**
* @brief Make a new MultipleOfConstraint object. * @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. * be divisible by.
* *
* @return pointer to a new MultipleOfConstraint that belongs to the * @return pointer to a new MultipleOfConstraint that belongs to the

View File

@ -634,7 +634,9 @@ public:
if (!target.asDouble(d)) { if (!target.asDouble(d)) {
if (results) { if (results) {
results->pushError(context, "Value could not be converted " 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<std::string>(
constraint.multipleOf));
} }
return false; return false;
} }
@ -643,7 +645,9 @@ public:
if (!target.asInteger(i)) { if (!target.asInteger(i)) {
if (results) { if (results) {
results->pushError(context, "Value could not be converted " 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<std::string>(
constraint.multipleOf));
} }
return false; return false;
} }

View File

@ -139,6 +139,11 @@ TEST_F(TestValidator, Draft3_Dependencies)
processDraft3TestFile(TEST_SUITE_DIR "draft3/dependencies.json"); processDraft3TestFile(TEST_SUITE_DIR "draft3/dependencies.json");
} }
TEST_F(TestValidator, Draft3_DivisibleBy)
{
processDraft3TestFile(TEST_SUITE_DIR "draft3/divisibleBy.json");
}
TEST_F(TestValidator, Draft3_Enum) TEST_F(TestValidator, Draft3_Enum)
{ {
processDraft3TestFile(TEST_SUITE_DIR "draft3/enum.json"); processDraft3TestFile(TEST_SUITE_DIR "draft3/enum.json");