Add ConstraintBuilder, allowing the user to add constraints to the json schema language

This commit is contained in:
James Jensen 2016-04-07 11:37:19 -04:00
parent f3eb9edb7b
commit acffebd74e

View File

@ -4,6 +4,8 @@
#include <stdexcept>
#include <iostream>
#include <vector>
#include <memory>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
@ -24,6 +26,12 @@
namespace valijson {
class ConstraintBuilder {
public:
virtual ~ConstraintBuilder() {}
virtual std::unique_ptr<constraints::Constraint> make(adapters::Adapter &node) = 0;
};
/**
* @brief Parser for populating a Schema based on a JSON Schema document.
*
@ -37,6 +45,10 @@ class SchemaParser
{
public:
void addConstraintBuilder(const std::string &key, ConstraintBuilder &builder) {
regConstraints.emplace_back(key, &builder);
}
/// Supported versions of JSON Schema
enum Version {
kDraft3, ///< @deprecated JSON Schema v3 has been superseded by v4
@ -509,6 +521,9 @@ private:
schemaCache, schemaCacheKeysToCreate);
}
std::vector<std::pair<std::string, ConstraintBuilder *>> regConstraints;
/**
* @brief Populate a Schema object from JSON Schema document
*
@ -770,6 +785,13 @@ private:
makePatternConstraint(itr->second), &subschema);
}
for (auto &builder : regConstraints) {
if ((itr = object.find(builder.first)) != object.end()) {
auto pcons = builder.second->make(itr->second);
rootSchema.addConstraintToSubschema(*pcons, &subschema);
}
}
{
// Check for schema keywords that require the creation of a
// PropertiesConstraint instance.