mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-09-27 16:29:33 +02:00
Improve encapsulation in MinItemsConstraint and MaxItemsConstraint classes
This commit is contained in:
parent
4d0d38566c
commit
31c0fce2b3
@ -418,14 +418,30 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'maxItems' constraint.
|
||||
* @brief Represents a 'maxItems' constraint
|
||||
*/
|
||||
struct MaxItemsConstraint: BasicConstraint<MaxItemsConstraint>
|
||||
class MaxItemsConstraint: public BasicConstraint<MaxItemsConstraint>
|
||||
{
|
||||
MaxItemsConstraint(int64_t maxItems)
|
||||
: maxItems(maxItems) { }
|
||||
public:
|
||||
MaxItemsConstraint()
|
||||
: maxItems(std::numeric_limits<int64_t>::max()) { }
|
||||
|
||||
const int64_t maxItems;
|
||||
MaxItemsConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
maxItems(std::numeric_limits<int64_t>::max()) { }
|
||||
|
||||
int64_t getMaxItems() const
|
||||
{
|
||||
return maxItems;
|
||||
}
|
||||
|
||||
void setMaxItems(int64_t newMaxItems)
|
||||
{
|
||||
maxItems = newMaxItems;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t maxItems;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -491,14 +507,35 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'minItems' constraint.
|
||||
* @brief Represents a 'minItems' constraint
|
||||
*/
|
||||
struct MinItemsConstraint: BasicConstraint<MinItemsConstraint>
|
||||
class MinItemsConstraint: public BasicConstraint<MinItemsConstraint>
|
||||
{
|
||||
MinItemsConstraint(int64_t minItems)
|
||||
: minItems(minItems) { }
|
||||
public:
|
||||
MinItemsConstraint()
|
||||
: minItems(0) { }
|
||||
|
||||
const int64_t minItems;
|
||||
MinItemsConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
minItems(0) { }
|
||||
|
||||
int64_t getMinItems() const
|
||||
{
|
||||
return minItems;
|
||||
}
|
||||
|
||||
void setMinItems(int64_t newMinItems)
|
||||
{
|
||||
if (newMinItems < 0) {
|
||||
throw std::runtime_error(
|
||||
"Minimum number of items must be a non-negative integer");
|
||||
}
|
||||
|
||||
minItems = newMinItems;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t minItems;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -6,10 +6,8 @@ namespace valijson {
|
||||
namespace constraints {
|
||||
|
||||
struct FormatConstraint;
|
||||
struct MaxItemsConstraint;
|
||||
struct MaxLengthConstraint;
|
||||
struct MaxPropertiesConstraint;
|
||||
struct MinItemsConstraint;
|
||||
struct MinLengthConstraint;
|
||||
struct MinPropertiesConstraint;
|
||||
struct MultipleOfConstraint;
|
||||
@ -19,7 +17,9 @@ class AnyOfConstraint;
|
||||
class DependenciesConstraint;
|
||||
class EnumConstraint;
|
||||
class LinearItemsConstraint;
|
||||
class MaxItemsConstraint;
|
||||
class MaximumConstraint;
|
||||
class MinItemsConstraint;
|
||||
class MinimumConstraint;
|
||||
class NotConstraint;
|
||||
class OneOfConstraint;
|
||||
|
@ -934,9 +934,11 @@ private:
|
||||
const AdapterType &node)
|
||||
{
|
||||
if (node.maybeInteger()) {
|
||||
int64_t value = node.asInteger();
|
||||
const int64_t value = node.asInteger();
|
||||
if (value >= 0) {
|
||||
return constraints::MaxItemsConstraint(value);
|
||||
constraints::MaxItemsConstraint constraint;
|
||||
constraint.setMaxItems(value);
|
||||
return constraint;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1043,9 +1045,11 @@ private:
|
||||
const AdapterType &node)
|
||||
{
|
||||
if (node.maybeInteger()) {
|
||||
int64_t value = node.asInteger();
|
||||
const int64_t value = node.asInteger();
|
||||
if (value >= 0) {
|
||||
return constraints::MinItemsConstraint(value);
|
||||
constraints::MinItemsConstraint constraint;
|
||||
constraint.setMinItems(value);
|
||||
return constraint;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -384,12 +384,11 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate against the maxItems constraint represented by a
|
||||
* MaxItemsConstraint object.
|
||||
* @brief Validate a value against a MaxItemsConstraint
|
||||
*
|
||||
* @param constraint Constraint that the target must validate against.
|
||||
* @param constraint Constraint that the target must validate against
|
||||
*
|
||||
* @return true if constraint is satisfied, false otherwise.
|
||||
* @return \c true if constraint is satisfied; \c false otherwise
|
||||
*/
|
||||
virtual bool visit(const MaxItemsConstraint &constraint)
|
||||
{
|
||||
@ -397,14 +396,14 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (target.asArray().size() <= constraint.maxItems) {
|
||||
const uint64_t maxItems = constraint.getMaxItems();
|
||||
if (target.asArray().size() <= maxItems) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (results) {
|
||||
results->pushError(context, "Array should contain no more than " +
|
||||
boost::lexical_cast<std::string>(constraint.maxItems) +
|
||||
" elements.");
|
||||
boost::lexical_cast<std::string>(maxItems) + " elements.");
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -506,12 +505,11 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate against the minItems constraint represented by a
|
||||
* MinItemsConstraint object.
|
||||
* @brief Validate a value against a MinItemsConstraint
|
||||
*
|
||||
* @param constraint Constraint that the target must validate against.
|
||||
* @param constraint Constraint that the target must validate against
|
||||
*
|
||||
* @return true if the constraint is satisfied, false otherwise.
|
||||
* @return \c true if the constraint is satisfied; \c false otherwise
|
||||
*/
|
||||
virtual bool visit(const MinItemsConstraint &constraint)
|
||||
{
|
||||
@ -519,14 +517,14 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (target.asArray().size() >= constraint.minItems) {
|
||||
const int64_t minItems = constraint.getMinItems();
|
||||
if (target.asArray().size() >= minItems) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (results) {
|
||||
results->pushError(context, "Array should contain no fewer than " +
|
||||
boost::lexical_cast<std::string>(constraint.minItems) +
|
||||
" elements.");
|
||||
boost::lexical_cast<std::string>(minItems) + " elements.");
|
||||
}
|
||||
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user