mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-09-27 16:29:33 +02:00
Update NotConstraint to use custom allocator, with better encapsulation
This commit is contained in:
parent
05ba5bf711
commit
99a5c2f604
@ -421,17 +421,30 @@ struct MultipleOfConstraint: BasicConstraint<MultipleOfConstraint>
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents a 'not' constraint.
|
||||
* @brief Represents a 'not' constraint
|
||||
*/
|
||||
struct NotConstraint: BasicConstraint<NotConstraint>
|
||||
class NotConstraint: public BasicConstraint<NotConstraint>
|
||||
{
|
||||
NotConstraint(const Subschema *schema)
|
||||
: schema(schema) { }
|
||||
public:
|
||||
NotConstraint()
|
||||
: subschema(NULL) { }
|
||||
|
||||
NotConstraint(const NotConstraint &other)
|
||||
: schema(other.schema) { }
|
||||
NotConstraint(CustomAlloc allocFn, CustomFree freeFn)
|
||||
: BasicConstraint(allocFn, freeFn),
|
||||
subschema(NULL) { }
|
||||
|
||||
const Subschema *schema;
|
||||
const Subschema * getSubschema() const
|
||||
{
|
||||
return subschema;
|
||||
}
|
||||
|
||||
void setSubschema(const Subschema *subschema)
|
||||
{
|
||||
this->subschema = subschema;
|
||||
}
|
||||
|
||||
private:
|
||||
const Subschema *subschema;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -16,15 +16,14 @@ struct MinItemsConstraint;
|
||||
struct MinLengthConstraint;
|
||||
struct MinPropertiesConstraint;
|
||||
struct MultipleOfConstraint;
|
||||
struct NotConstraint;
|
||||
struct PatternConstraint;
|
||||
struct PropertiesConstraint;
|
||||
|
||||
|
||||
class AllOfConstraint;
|
||||
class AnyOfConstraint;
|
||||
class DependenciesConstraint;
|
||||
class LinearItemsConstraint;
|
||||
class NotConstraint;
|
||||
class OneOfConstraint;
|
||||
class RequiredConstraint;
|
||||
class SingularItemsConstraint;
|
||||
|
@ -1153,10 +1153,13 @@ private:
|
||||
typename FetchDocumentFunction<AdapterType>::Type > fetchDoc)
|
||||
{
|
||||
if (node.maybeObject()) {
|
||||
const Subschema *childSubschema = rootSchema.createSubschema();
|
||||
populateSchema<AdapterType>(rootSchema, rootNode, node,
|
||||
*childSubschema, currentScope, nodePath, fetchDoc);
|
||||
return constraints::NotConstraint(childSubschema);
|
||||
const Subschema *subschema = rootSchema.createSubschema();
|
||||
constraints::NotConstraint constraint;
|
||||
constraint.setSubschema(subschema);
|
||||
populateSchema<AdapterType>(rootSchema, rootNode, node, *subschema,
|
||||
currentScope, nodePath, fetchDoc);
|
||||
|
||||
return constraint;
|
||||
}
|
||||
|
||||
throw std::runtime_error("Expected object value for 'not' constraint.");
|
||||
|
@ -683,21 +683,32 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate against the not constraint represented by a
|
||||
* NotConstraint object.
|
||||
* @brief Validate a value against a NotConstraint
|
||||
*
|
||||
* @param constraint Constraint that the target must validate against.
|
||||
* If the subschema NotConstraint currently holds a NULL pointer, the
|
||||
* schema will be treated like the empty schema. Therefore validation
|
||||
* will always fail.
|
||||
*
|
||||
* @return true if the constraint is satisfied, false otherwise.
|
||||
* @param constraint Constraint that the target must validate against
|
||||
*
|
||||
* @return \c true if the constraint is satisfied; \c false otherwise
|
||||
*/
|
||||
virtual bool visit(const NotConstraint &constraint)
|
||||
{
|
||||
ValidationVisitor<AdapterType> v(target, context, strictTypes, NULL);
|
||||
const Subschema *subschema = constraint.getSubschema();
|
||||
if (!subschema) {
|
||||
// Treat NULL pointer like empty schema
|
||||
return false;
|
||||
}
|
||||
|
||||
if (v.validateSchema(*constraint.schema)) {
|
||||
ValidationVisitor<AdapterType> v(target, context, strictTypes, NULL);
|
||||
if (v.validateSchema(*subschema)) {
|
||||
if (results) {
|
||||
results->pushError(context, "Target should not validate against schema specified in 'not' constraint.");
|
||||
results->pushError(context,
|
||||
"Target should not validate against schema "
|
||||
"specified in 'not' constraint.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user