mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-09-27 16:29:33 +02:00
Improve encapsulation in MinPropertiesConstraint and MaxPropertiesConstraint classes
This commit is contained in:
parent
8f4dbc63c9
commit
2bcaaac344
@ -472,14 +472,35 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'maxProperties' constraint.
|
||||
* @brief Represents a 'maxProperties' constraint
|
||||
*/
|
||||
struct MaxPropertiesConstraint: BasicConstraint<MaxPropertiesConstraint>
|
||||
class MaxPropertiesConstraint: public BasicConstraint<MaxPropertiesConstraint>
|
||||
{
|
||||
MaxPropertiesConstraint(int64_t maxProperties)
|
||||
: maxProperties(maxProperties) { }
|
||||
public:
|
||||
MaxPropertiesConstraint()
|
||||
: maxProperties(std::numeric_limits<int64_t>::max()) { }
|
||||
|
||||
const int64_t maxProperties;
|
||||
MaxPropertiesConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
maxProperties(std::numeric_limits<int64_t>::max()) { }
|
||||
|
||||
int64_t getMaxProperties() const
|
||||
{
|
||||
return maxProperties;
|
||||
}
|
||||
|
||||
void setMaxProperties(int64_t newMaxProperties)
|
||||
{
|
||||
if (newMaxProperties < 0) {
|
||||
throw std::runtime_error("Maximum number of properties must be a "
|
||||
"non-negative integer");
|
||||
}
|
||||
|
||||
maxProperties = newMaxProperties;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t maxProperties;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -587,14 +608,35 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'minProperties' constraint.
|
||||
* @brief Represents a 'minProperties' constraint
|
||||
*/
|
||||
struct MinPropertiesConstraint: BasicConstraint<MinPropertiesConstraint>
|
||||
class MinPropertiesConstraint: public BasicConstraint<MinPropertiesConstraint>
|
||||
{
|
||||
MinPropertiesConstraint(int64_t minProperties)
|
||||
: minProperties(minProperties) { }
|
||||
public:
|
||||
MinPropertiesConstraint()
|
||||
: minProperties(0) { }
|
||||
|
||||
const int64_t minProperties;
|
||||
MinPropertiesConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
minProperties(0) { }
|
||||
|
||||
int64_t getMinProperties() const
|
||||
{
|
||||
return minProperties;
|
||||
}
|
||||
|
||||
void setMinProperties(int64_t newMinProperties)
|
||||
{
|
||||
if (newMinProperties < 0) {
|
||||
throw std::runtime_error("Minimum number of properties must be a "
|
||||
"non-negative integer");
|
||||
}
|
||||
|
||||
minProperties = newMinProperties;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t minProperties;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -6,8 +6,6 @@ namespace valijson {
|
||||
namespace constraints {
|
||||
|
||||
struct FormatConstraint;
|
||||
struct MaxPropertiesConstraint;
|
||||
struct MinPropertiesConstraint;
|
||||
|
||||
class AllOfConstraint;
|
||||
class AnyOfConstraint;
|
||||
@ -17,9 +15,11 @@ class LinearItemsConstraint;
|
||||
class MaxItemsConstraint;
|
||||
class MaximumConstraint;
|
||||
class MaxLengthConstraint;
|
||||
class MaxPropertiesConstraint;
|
||||
class MinItemsConstraint;
|
||||
class MinimumConstraint;
|
||||
class MinLengthConstraint;
|
||||
class MinPropertiesConstraint;
|
||||
class MultipleOfDoubleConstraint;
|
||||
class MultipleOfIntConstraint;
|
||||
class NotConstraint;
|
||||
|
@ -1008,7 +1008,9 @@ private:
|
||||
if (node.maybeInteger()) {
|
||||
int64_t value = node.asInteger();
|
||||
if (value >= 0) {
|
||||
return constraints::MaxPropertiesConstraint(value);
|
||||
constraints::MaxPropertiesConstraint constraint;
|
||||
constraint.setMaxProperties(value);
|
||||
return constraint;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1123,7 +1125,9 @@ private:
|
||||
if (node.maybeInteger()) {
|
||||
int64_t value = node.asInteger();
|
||||
if (value >= 0) {
|
||||
return constraints::MinPropertiesConstraint(value);
|
||||
constraints::MinPropertiesConstraint constraint;
|
||||
constraint.setMinProperties(value);
|
||||
return constraint;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -451,14 +451,16 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (target.asObject().size() <= constraint.maxProperties) {
|
||||
const int64_t maxProperties = constraint.getMaxProperties();
|
||||
|
||||
if (target.asObject().size() <= maxProperties) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (results) {
|
||||
results->pushError(context, "Object should have no more than" +
|
||||
boost::lexical_cast<std::string>(constraint.maxProperties) +
|
||||
" properties.");
|
||||
boost::lexical_cast<std::string>(maxProperties) +
|
||||
" properties.");
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -568,19 +570,20 @@ public:
|
||||
*/
|
||||
virtual bool visit(const MinPropertiesConstraint &constraint)
|
||||
{
|
||||
|
||||
if ((strictTypes && !target.isObject()) || !target.maybeObject()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (target.asObject().size() >= constraint.minProperties) {
|
||||
const int64_t minProperties = constraint.getMinProperties();
|
||||
|
||||
if (target.asObject().size() >= minProperties) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (results) {
|
||||
results->pushError(context, "Object should have no fewer than" +
|
||||
boost::lexical_cast<std::string>(constraint.minProperties) +
|
||||
" properties.");
|
||||
boost::lexical_cast<std::string>(minProperties) +
|
||||
" properties.");
|
||||
}
|
||||
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user