| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- #pragma once
- #include <functional>
- #include <map>
- #include <memory>
- #include <set>
- #include <string_view>
- #include <unordered_map>
- #include <unordered_set>
- #include <jvalidate/constraint/array_constraint.h>
- #include <jvalidate/constraint/general_constraint.h>
- #include <jvalidate/constraint/number_constraint.h>
- #include <jvalidate/constraint/object_constraint.h>
- #include <jvalidate/constraint/string_constraint.h>
- #include <jvalidate/detail/expect.h>
- #include <jvalidate/detail/parser_context.h>
- #include <jvalidate/detail/vocabulary.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/forward.h>
- namespace jvalidate {
- template <Adapter A> class ConstraintFactory {
- public:
- using pConstraint = std::unique_ptr<constraint::Constraint>;
- using MakeConstraint = typename detail::Vocabulary<A>::MakeConstraint;
- using Object = decltype(std::declval<A>().as_object());
- enum KeywordType { Keyword, Removed };
- struct Make {
- Make(KeywordType t) : is_keyword(t == Keyword) {}
- template <typename F> Make(F make) : make(make), is_keyword(true) {}
- explicit operator bool() const { return make || is_keyword; }
- operator MakeConstraint() const { return make; }
- MakeConstraint make = nullptr;
- bool is_keyword = false;
- };
- struct Versioned {
- template <typename M> Versioned(M make) : data{{schema::Version::Earliest, make}} {}
- template <typename M> Versioned(schema::Version version, M make) : data{{version, make}} {}
- Versioned(std::initializer_list<std::pair<schema::Version const, Make>> init) : data(init) {}
- std::map<schema::Version, Make, std::greater<>> data;
- };
- using Store = std::unordered_map<std::string_view, Versioned>;
- private:
- using Self = ConstraintFactory<A>;
- private:
- std::unordered_map<std::string_view, Versioned> constraints_{
- {"$defs", {schema::Version::Draft2019_09, Keyword}},
- {"additionalItems",
- {{schema::Version::Earliest, &Self::additionalItems},
- {schema::Version::Draft2020_12, Removed}}},
- {"additionalProperties", &Self::additionalProperties},
- {"allOf", {schema::Version::Draft04, &Self::allOf}},
- {"anyOf", {schema::Version::Draft04, &Self::anyOf}},
- {"const", {schema::Version::Draft06, &Self::isConstant}},
- {"contains", {schema::Version::Draft06, &Self::contains}},
- {"definitions", Keyword},
- {"dependencies", &Self::dependencies},
- {"dependentRequired", {schema::Version::Draft2019_09, &Self::dependentRequired}},
- {"dependentSchemas", {schema::Version::Draft2019_09, &Self::dependentSchemas}},
- {"disallow",
- {{schema::Version::Earliest, &Self::disallowDraft3}, {schema::Version::Draft04, Removed}}},
- {"divisibleBy",
- {{schema::Version::Earliest, &Self::multipleOf}, {schema::Version::Draft04, Removed}}},
- {"else", {{schema::Version::Draft07, Keyword}}},
- {"enum", &Self::isInEnumuration},
- {"exclusiveMaximum", {schema::Version::Draft06, &Self::exclusiveMaximum}},
- {"exclusiveMinimum", {schema::Version::Draft06, &Self::exclusiveMinimum}},
- {"extends",
- {{schema::Version::Earliest, &Self::extendsDraft3}, {schema::Version::Draft04, Removed}}},
- {"format", &Self::format},
- {"if", {schema::Version::Draft07, &Self::ifThenElse}},
- {"items",
- {{schema::Version::Earliest, &Self::itemsTupleOrVector},
- {schema::Version::Draft2020_12, &Self::additionalItems}}},
- {"maxItems", &Self::maxItems},
- {"maxLength", &Self::maxLength},
- {"maxProperties", {schema::Version::Draft04, &Self::maxProperties}},
- {"maximum", &Self::maximum},
- {"minItems", &Self::minItems},
- {"minLength", &Self::minLength},
- {"minProperties", {schema::Version::Draft04, &Self::minProperties}},
- {"minimum", &Self::minimum},
- {"multipleOf", {schema::Version::Draft04, &Self::multipleOf}},
- {"not", {schema::Version::Draft04, &Self::isNot}},
- {"oneOf", {schema::Version::Draft04, &Self::oneOf}},
- {"pattern", &Self::pattern},
- {"patternProperties", &Self::patternProperties},
- {"prefixItems", {schema::Version::Draft2020_12, &Self::prefixItems}},
- {"properties",
- {{schema::Version::Earliest, &Self::propertiesDraft3},
- {schema::Version::Draft04, &Self::properties}}},
- {"propertyNames", {schema::Version::Draft06, &Self::propertyNames}},
- {"required", {schema::Version::Draft04, &Self::required}},
- {"then", {schema::Version::Draft07, Keyword}},
- {"type",
- {{schema::Version::Earliest, &Self::typeDraft3}, {schema::Version::Draft04, &Self::type}}},
- {"unevaluatedItems", {schema::Version::Draft2019_09, &Self::unevaluatedItems}},
- {"unevaluatedProperties", {schema::Version::Draft2019_09, &Self::unevaluatedProperties}},
- {"uniqueItems", &Self::uniqueItems},
- };
- public:
- ConstraintFactory() = default;
- ConstraintFactory(std::initializer_list<std::pair<std::string_view, Versioned>> init) {
- constraints_.insert(init.begin(), init.end());
- }
- ConstraintFactory<A> && with_user_keyword(std::string_view word, Versioned make) && {
- constraints_.insert(word, std::move(make));
- return *this;
- }
- ConstraintFactory<A> && override_keyword(std::string_view word, Versioned make) && {
- constraints_[word] = std::move(make);
- return *this;
- }
- detail::Vocabulary<A> keywords(schema::Version version) const {
- std::unordered_map<std::string_view, MakeConstraint> rval;
- for (auto const & [key, versions] : constraints_) {
- if (auto it = versions.data.lower_bound(version); it != versions.data.end() && it->second) {
- rval.emplace(key, it->second);
- }
- }
- return detail::Vocabulary<A>(version, std::move(rval));
- }
- // SECTION: Untyped Constraints
- static auto type(detail::ParserContext<A> const & context) {
- static std::unordered_map<std::string_view, adapter::Type> const s_type_names{
- {"null", adapter::Type::Null}, {"boolean", adapter::Type::Boolean},
- {"integer", adapter::Type::Integer}, {"number", adapter::Type::Number},
- {"string", adapter::Type::String}, {"array", adapter::Type::Array},
- {"object", adapter::Type::Object},
- };
- auto to_type = [](std::string_view type) {
- EXPECT_M(s_type_names.contains(type), "Unknown type " << type);
- return s_type_names.at(type);
- };
- adapter::Type const type = context.schema.type();
- if (type == adapter::Type::String) {
- return std::make_unique<constraint::TypeConstraint>(to_type(context.schema.as_string()));
- }
- EXPECT(type == adapter::Type::Array);
- std::set<adapter::Type> types;
- for (auto subschema : context.schema.as_array()) {
- types.insert(to_type(subschema.as_string()));
- }
- return std::make_unique<constraint::TypeConstraint>(types);
- }
- static pConstraint typeDraft3(detail::ParserContext<A> const & context) {
- static std::unordered_map<std::string_view, adapter::Type> const s_type_names{
- {"null", adapter::Type::Null}, {"boolean", adapter::Type::Boolean},
- {"integer", adapter::Type::Integer}, {"number", adapter::Type::Number},
- {"string", adapter::Type::String}, {"array", adapter::Type::Array},
- {"object", adapter::Type::Object},
- };
- auto to_type = [](std::string_view type) {
- EXPECT_M(s_type_names.contains(type), "Unknown type " << type);
- return s_type_names.at(type);
- };
- adapter::Type const type = context.schema.type();
- if (type == adapter::Type::String) {
- if (context.schema.as_string() == "any") {
- return nullptr;
- }
- return std::make_unique<constraint::TypeConstraint>(to_type(context.schema.as_string()));
- }
- EXPECT(type == adapter::Type::Array);
- std::vector<schema::Node const *> children;
- std::set<adapter::Type> types;
- size_t index = 0;
- for (auto subschema : context.schema.as_array()) {
- if (subschema.type() != adapter::Type::String) {
- children.push_back(context.child(subschema, index).node());
- } else if (subschema.as_string() == "any") {
- return nullptr;
- } else {
- types.insert(to_type(subschema.as_string()));
- }
- ++index;
- }
- return constraint::PolyConstraint::AnyOf(
- std::make_unique<constraint::TypeConstraint>(types),
- std::make_unique<constraint::AnyOfConstraint>(children));
- }
- static pConstraint disallowDraft3(detail::ParserContext<A> const & context) {
- return constraint::PolyConstraint::Not(typeDraft3(context));
- }
- static pConstraint extendsDraft3(detail::ParserContext<A> const & context) {
- std::vector<schema::Node const *> children;
- switch (context.schema.type()) {
- case adapter::Type::Object:
- children.push_back(context.node());
- break;
- case adapter::Type::Array: {
- size_t index = 0;
- for (auto const & subschema : context.schema.as_array()) {
- children.push_back(context.child(subschema, index).node());
- ++index;
- }
- break;
- }
- default:
- JVALIDATE_THROW(std::runtime_error, "extends must be a schema of array-of-schemas");
- }
- return std::make_unique<constraint::AllOfConstraint>(children);
- }
- static pConstraint ifThenElse(detail::ParserContext<A> const & context) {
- schema::Node const * then_ = context.fixed_schema(true);
- if (context.parent->contains("then")) {
- then_ = context.neighbor("then").node();
- }
- schema::Node const * else_ = context.fixed_schema(true);
- if (context.parent->contains("else")) {
- else_ = context.neighbor("else").node();
- }
- return std::make_unique<constraint::ConditionalConstraint>(context.node(), then_, else_);
- }
- static auto isInEnumuration(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::vector<std::unique_ptr<adapter::Const const>> rval;
- for (auto subschema : context.schema.as_array()) {
- rval.push_back(subschema.freeze());
- }
- return std::make_unique<constraint::EnumConstraint>(std::move(rval));
- }
- static auto isConstant(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::EnumConstraint>(context.schema.freeze());
- }
- static auto allOf(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::vector<schema::Node const *> rval;
- size_t index = 0;
- for (auto subschema : context.schema.as_array()) {
- rval.push_back(context.child(subschema, index).node());
- ++index;
- }
- return std::make_unique<constraint::AllOfConstraint>(rval);
- }
- static auto anyOf(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::vector<schema::Node const *> rval;
- size_t index = 0;
- for (auto subschema : context.schema.as_array()) {
- rval.push_back(context.child(subschema, index).node());
- ++index;
- }
- return std::make_unique<constraint::AnyOfConstraint>(rval);
- }
- static auto oneOf(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::vector<schema::Node const *> rval;
- size_t index = 0;
- for (auto subschema : context.schema.as_array()) {
- rval.push_back(context.child(subschema, index).node());
- ++index;
- }
- return std::make_unique<constraint::OneOfConstraint>(rval);
- }
- static auto isNot(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::NotConstraint>(context.node());
- }
- // SECTION: Numeric Constraints
- static auto minimum(detail::ParserContext<A> const & context) {
- double value = context.schema.as_number();
- if (context.vocab->version() < schema::Version::Draft06 &&
- context.parent->contains("exclusiveMinimum")) {
- auto exclusive = (*context.parent)["exclusiveMinimum"];
- EXPECT(exclusive.type() == adapter::Type::Boolean);
- return std::make_unique<constraint::MinimumConstraint>(value, exclusive.as_boolean());
- }
- return std::make_unique<constraint::MinimumConstraint>(value, false);
- }
- static pConstraint exclusiveMinimum(detail::ParserContext<A> const & context) {
- double value = context.schema.as_number();
- return std::make_unique<constraint::MinimumConstraint>(value, true);
- }
- static auto maximum(detail::ParserContext<A> const & context) {
- double value = context.schema.as_number();
- if (context.vocab->version() < schema::Version::Draft06 &&
- context.parent->contains("exclusiveMaximum")) {
- auto exclusive = (*context.parent)["exclusiveMaximum"];
- EXPECT(exclusive.type() == adapter::Type::Boolean);
- return std::make_unique<constraint::MaximumConstraint>(value, exclusive.as_boolean());
- }
- return std::make_unique<constraint::MaximumConstraint>(value, false);
- }
- static pConstraint exclusiveMaximum(detail::ParserContext<A> const & context) {
- double value = context.schema.as_number();
- return std::make_unique<constraint::MaximumConstraint>(value, true);
- }
- static auto multipleOf(detail::ParserContext<A> const & context) {
- double value = context.schema.as_number();
- return std::make_unique<constraint::MultipleOfConstraint>(value);
- }
- // SECTION: String Constraints
- static auto minLength(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MinLengthConstraint>(context.schema.as_integer());
- }
- static auto maxLength(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MaxLengthConstraint>(context.schema.as_integer());
- }
- static auto pattern(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::PatternConstraint>(context.schema.as_string());
- }
- static auto format(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::FormatConstraint>(context.schema.as_string());
- }
- // SECTION: Array Constraints
- static auto contains(detail::ParserContext<A> const & context) {
- if (context.vocab->version() < schema::Version::Draft2019_09) {
- return std::make_unique<constraint::ContainsConstraint>(context.node());
- }
- std::optional<size_t> maximum;
- std::optional<size_t> minimum;
- if (context.parent->contains("maxContains")) {
- maximum = (*context.parent)["maxContains"].as_integer();
- }
- if (context.parent->contains("minContains")) {
- minimum = (*context.parent)["minContains"].as_integer();
- }
- return std::make_unique<constraint::ContainsConstraint>(context.node(), minimum, maximum);
- }
- static auto minItems(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MinItemsConstraint>(context.schema.as_integer());
- }
- static auto maxItems(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MaxItemsConstraint>(context.schema.as_integer());
- }
- static auto prefixItems(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::vector<schema::Node const *> rval;
- size_t index = 0;
- for (auto subschema : context.schema.as_array()) {
- rval.push_back(context.child(subschema, index).node());
- ++index;
- }
- return std::make_unique<constraint::TupleConstraint>(rval);
- }
- static auto additionalItemsAfter(detail::ParserContext<A> const & context, size_t n) {
- using C = constraint::AdditionalItemsConstraint;
- if (context.vocab->version() < schema::Version::Draft06 &&
- context.schema.type() == adapter::Type::Boolean) {
- return std::make_unique<C>(context.always(), n);
- }
- return std::make_unique<C>(context.node(), n);
- }
- static pConstraint additionalItems(detail::ParserContext<A> const & context) {
- std::string const prefix =
- context.vocab->version() >= schema::Version::Draft2020_12 ? "prefixItems" : "items";
- Object const & parent = *context.parent;
- // Before Draft 2020-12, the "items" could be either a subschema or a tuple.
- // When not provided, we therefore treat it as an "accept-all" schema, and
- // thus will never have additionalItems to process. Similarly - if it is an
- // Object, then it must act on all items.
- if (context.vocab->version() < schema::Version::Draft2020_12 &&
- (not parent.contains(prefix) || parent[prefix].type() == adapter::Type::Object)) {
- return nullptr;
- }
- return additionalItemsAfter(context, parent[prefix].array_size());
- }
- static pConstraint itemsTupleOrVector(detail::ParserContext<A> const & context) {
- if (context.schema.type() == adapter::Type::Array) {
- return prefixItems(context);
- }
- return additionalItemsAfter(context, 0);
- }
- static auto unevaluatedItems(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::UnevaluatedItemsConstraint>(context.node());
- }
- static pConstraint uniqueItems(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Boolean);
- if (not context.schema.as_boolean()) {
- return nullptr;
- }
- return std::make_unique<constraint::UniqueItemsConstraint>();
- }
- // SECTION: Object Constraints
- static auto required(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Array);
- std::unordered_set<std::string> rval;
- for (auto subschema : context.schema.as_array()) {
- EXPECT(subschema.type() == adapter::Type::String);
- rval.insert(subschema.as_string());
- }
- return std::make_unique<constraint::RequiredConstraint>(rval);
- }
- static auto minProperties(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MinPropertiesConstraint>(context.schema.as_integer());
- }
- static auto maxProperties(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Integer ||
- context.schema.type() == adapter::Type::Number);
- return std::make_unique<constraint::MaxPropertiesConstraint>(context.schema.as_integer());
- }
- static auto patternProperties(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::vector<std::pair<std::string, schema::Node const *>> rval;
- for (auto [prop, subschema] : context.schema.as_object()) {
- rval.emplace_back(prop, context.child(subschema, prop).node());
- }
- return std::make_unique<constraint::PatternPropertiesConstraint>(rval);
- }
- static auto properties(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::map<std::string, schema::Node const *> rval;
- for (auto [prop, subschema] : context.schema.as_object()) {
- rval.emplace(prop, context.child(subschema, prop).node());
- }
- return std::make_unique<constraint::PropertiesConstraint>(rval);
- }
- static pConstraint propertiesDraft3(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::unordered_set<std::string> required;
- for (auto [prop, subschema] : context.schema.as_object()) {
- EXPECT(subschema.type() == adapter::Type::Object);
- if (auto sub = subschema.as_object();
- sub.contains("required") && sub["required"].as_boolean()) {
- required.insert(prop);
- }
- }
- if (required.empty()) {
- return properties(context);
- }
- return constraint::PolyConstraint::AllOf(
- properties(context), std::make_unique<constraint::RequiredConstraint>(std::move(required)));
- }
- static auto propertyNames(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::PropertyNamesConstraint>(context.node());
- }
- static auto unevaluatedProperties(detail::ParserContext<A> const & context) {
- return std::make_unique<constraint::UnevaluatedPropertiesConstraint>(context.node());
- }
- static auto additionalProperties(detail::ParserContext<A> const & context) {
- std::unordered_set<std::string> properties;
- std::vector<std::string> patterns;
- Object const & parent = *context.parent;
- if (parent.contains("properties")) {
- for (auto [key, _] : parent["properties"].as_object()) {
- properties.insert(key);
- }
- }
- if (parent.contains("patternProperties")) {
- for (auto [key, _] : parent["patternProperties"].as_object()) {
- patterns.push_back(key);
- }
- }
- using C = constraint::AdditionalPropertiesConstraint;
- if (context.vocab->version() < schema::Version::Draft06 &&
- context.schema.type() == adapter::Type::Boolean) {
- return std::make_unique<C>(context.always(), properties, patterns);
- }
- return std::make_unique<C>(context.node(), properties, patterns);
- }
- static auto dependencies(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::map<std::string, schema::Node const *> schemas;
- std::map<std::string, std::unordered_set<std::string>> required;
- for (auto [prop, subschema] : context.schema.as_object()) {
- if (subschema.type() == adapter::Type::Array) {
- for (auto key : subschema.as_array()) {
- EXPECT(key.type() == adapter::Type::String);
- required[prop].insert(key.as_string());
- }
- } else if (context.vocab->version() <= schema::Version::Draft03 &&
- subschema.type() == adapter::Type::String) {
- required[prop].insert(subschema.as_string());
- } else {
- schemas.emplace(prop, context.child(subschema, prop).node());
- }
- }
- return std::make_unique<constraint::DependenciesConstraint>(schemas, required);
- }
- static auto dependentSchemas(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::map<std::string, schema::Node const *> rval;
- for (auto [prop, subschema] : context.schema.as_object()) {
- rval.emplace(prop, context.child(subschema, prop).node());
- }
- return std::make_unique<constraint::DependenciesConstraint>(rval);
- }
- static auto dependentRequired(detail::ParserContext<A> const & context) {
- EXPECT(context.schema.type() == adapter::Type::Object);
- std::map<std::string, std::unordered_set<std::string>> rval;
- for (auto [prop, subschema] : context.schema.as_object()) {
- EXPECT(subschema.type() == adapter::Type::Array);
- for (auto key : subschema.as_array()) {
- EXPECT(key.type() == adapter::Type::String);
- rval[prop].insert(key.as_string());
- }
- }
- return std::make_unique<constraint::DependenciesConstraint>(rval);
- }
- };
- }
|