mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-04 07:27:24 +01:00
Improve encapsulation in MaximumConstraint class
This commit is contained in:
parent
38c540a5d0
commit
65e2c662cc
@ -378,16 +378,43 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'maximum' constraint.
|
||||
* @brief Represents 'maximum' and 'exclusiveMaximum' constraints
|
||||
*/
|
||||
struct MaximumConstraint: BasicConstraint<MaximumConstraint>
|
||||
class MaximumConstraint: public BasicConstraint<MaximumConstraint>
|
||||
{
|
||||
MaximumConstraint(double maximum, bool exclusiveMaximum)
|
||||
: maximum(maximum),
|
||||
exclusiveMaximum(exclusiveMaximum) { }
|
||||
public:
|
||||
MaximumConstraint()
|
||||
: maximum(std::numeric_limits<double>::infinity()),
|
||||
exclusiveMaximum(false) { }
|
||||
|
||||
const double maximum;
|
||||
const bool exclusiveMaximum;
|
||||
MaximumConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
maximum(std::numeric_limits<double>::infinity()),
|
||||
exclusiveMaximum(false) { }
|
||||
|
||||
bool getExclusiveMaximum() const
|
||||
{
|
||||
return exclusiveMaximum;
|
||||
}
|
||||
|
||||
void setExclusiveMaximum(bool newExclusiveMaximum)
|
||||
{
|
||||
exclusiveMaximum = newExclusiveMaximum;
|
||||
}
|
||||
|
||||
double getMaximum() const
|
||||
{
|
||||
return maximum;
|
||||
}
|
||||
|
||||
void setMaximum(double newMaximum)
|
||||
{
|
||||
maximum = newMaximum;
|
||||
}
|
||||
|
||||
private:
|
||||
double maximum;
|
||||
bool exclusiveMaximum;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -5,9 +5,7 @@
|
||||
namespace valijson {
|
||||
namespace constraints {
|
||||
|
||||
|
||||
struct FormatConstraint;
|
||||
struct MaximumConstraint;
|
||||
struct MaxItemsConstraint;
|
||||
struct MaxLengthConstraint;
|
||||
struct MaxPropertiesConstraint;
|
||||
@ -16,15 +14,16 @@ struct MinItemsConstraint;
|
||||
struct MinLengthConstraint;
|
||||
struct MinPropertiesConstraint;
|
||||
struct MultipleOfConstraint;
|
||||
struct PatternConstraint;
|
||||
|
||||
class AllOfConstraint;
|
||||
class AnyOfConstraint;
|
||||
class DependenciesConstraint;
|
||||
class EnumConstraint;
|
||||
class LinearItemsConstraint;
|
||||
class MaximumConstraint;
|
||||
class NotConstraint;
|
||||
class OneOfConstraint;
|
||||
class PatternConstraint;
|
||||
class PropertiesConstraint;
|
||||
class RequiredConstraint;
|
||||
class SingularItemsConstraint;
|
||||
|
@ -900,25 +900,25 @@ private:
|
||||
const AdapterType &node,
|
||||
const AdapterType *exclusiveMaximum)
|
||||
{
|
||||
bool exclusiveMaximumValue = false;
|
||||
if (!node.maybeDouble()) {
|
||||
throw std::runtime_error(
|
||||
"Expected numeric value for maximum constraint.");
|
||||
}
|
||||
|
||||
constraints::MaximumConstraint constraint;
|
||||
constraint.setMaximum(node.asDouble());
|
||||
|
||||
if (exclusiveMaximum) {
|
||||
if (exclusiveMaximum->maybeBool()) {
|
||||
exclusiveMaximumValue = exclusiveMaximum->asBool();
|
||||
} else {
|
||||
if (!exclusiveMaximum->maybeBool()) {
|
||||
throw std::runtime_error(
|
||||
"Expected boolean value for exclusiveMaximum "
|
||||
"constraint.");
|
||||
}
|
||||
|
||||
constraint.setExclusiveMaximum(exclusiveMaximum->asBool());
|
||||
}
|
||||
|
||||
if (node.maybeDouble()) {
|
||||
double maximumValue = node.asDouble();
|
||||
return constraints::MaximumConstraint(maximumValue,
|
||||
exclusiveMaximumValue);
|
||||
}
|
||||
|
||||
throw std::runtime_error(
|
||||
"Expected numeric value for maximum constraint.");
|
||||
return constraint;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -345,12 +345,11 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate against the maximum and exclusiveMaximum constraints
|
||||
* represented by a MaximumConstraint object.
|
||||
* @brief Validate a value against a MaximumConstraint object
|
||||
*
|
||||
* @param constraint Constraint that the target must validate against.
|
||||
* @param constraint Constraint that the target must validate against
|
||||
*
|
||||
* @return true if constraints are satisfied, false otherwise.
|
||||
* @return \c true if constraints are satisfied; \c false otherwise
|
||||
*/
|
||||
virtual bool visit(const MaximumConstraint &constraint)
|
||||
{
|
||||
@ -359,23 +358,26 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (constraint.exclusiveMaximum) {
|
||||
if (target.asDouble() >= constraint.maximum) {
|
||||
const double maximum = constraint.getMaximum();
|
||||
|
||||
if (constraint.getExclusiveMaximum()) {
|
||||
if (target.asDouble() >= maximum) {
|
||||
if (results) {
|
||||
results->pushError(context, "Expected number less than " +
|
||||
boost::lexical_cast<std::string>(constraint.maximum));
|
||||
boost::lexical_cast<std::string>(maximum));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (target.asDouble() > constraint.maximum) {
|
||||
if (results) {
|
||||
results->pushError(context,
|
||||
"Expected number less than or equal to" +
|
||||
boost::lexical_cast<std::string>(constraint.maximum));
|
||||
}
|
||||
return false;
|
||||
|
||||
} else if (target.asDouble() > maximum) {
|
||||
if (results) {
|
||||
results->pushError(context,
|
||||
"Expected number less than or equal to" +
|
||||
boost::lexical_cast<std::string>(maximum));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user