From acffebd74e7f6fbeb4ea40810ef32d9c37f7c261 Mon Sep 17 00:00:00 2001 From: James Jensen Date: Thu, 7 Apr 2016 11:37:19 -0400 Subject: [PATCH] Add ConstraintBuilder, allowing the user to add constraints to the json schema language --- include/valijson/schema_parser.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/valijson/schema_parser.hpp b/include/valijson/schema_parser.hpp index a3d70c8..cf4549b 100644 --- a/include/valijson/schema_parser.hpp +++ b/include/valijson/schema_parser.hpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include @@ -24,6 +26,12 @@ namespace valijson { +class ConstraintBuilder { + public: + virtual ~ConstraintBuilder() {} + virtual std::unique_ptr 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> 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.