constraint.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #pragma once
  2. #include <functional>
  3. #include <map>
  4. #include <memory>
  5. #include <set>
  6. #include <string_view>
  7. #include <unordered_map>
  8. #include <unordered_set>
  9. #include <jvalidate/constraint/array_constraint.h>
  10. #include <jvalidate/constraint/general_constraint.h>
  11. #include <jvalidate/constraint/number_constraint.h>
  12. #include <jvalidate/constraint/object_constraint.h>
  13. #include <jvalidate/constraint/string_constraint.h>
  14. #include <jvalidate/detail/expect.h>
  15. #include <jvalidate/detail/parser_context.h>
  16. #include <jvalidate/detail/vocabulary.h>
  17. #include <jvalidate/enum.h>
  18. #include <jvalidate/forward.h>
  19. namespace jvalidate {
  20. template <Adapter A> class ConstraintFactory {
  21. public:
  22. using pConstraint = std::unique_ptr<constraint::Constraint>;
  23. using MakeConstraint = typename detail::Vocabulary<A>::MakeConstraint;
  24. using Object = decltype(std::declval<A>().as_object());
  25. enum KeywordType { Keyword, Removed };
  26. struct Make {
  27. Make(KeywordType t) : is_keyword(t == Keyword) {}
  28. template <typename F> Make(F make) : make(make), is_keyword(true) {}
  29. explicit operator bool() const { return make || is_keyword; }
  30. operator MakeConstraint() const { return make; }
  31. MakeConstraint make = nullptr;
  32. bool is_keyword = false;
  33. };
  34. struct Versioned {
  35. template <typename M> Versioned(M make) : data{{schema::Version::Earliest, make}} {}
  36. template <typename M> Versioned(schema::Version version, M make) : data{{version, make}} {}
  37. Versioned(std::initializer_list<std::pair<schema::Version const, Make>> init) : data(init) {}
  38. std::map<schema::Version, Make, std::greater<>> data;
  39. };
  40. using Store = std::unordered_map<std::string_view, Versioned>;
  41. private:
  42. using Self = ConstraintFactory<A>;
  43. private:
  44. std::unordered_map<std::string_view, Versioned> constraints_{
  45. {"$defs", {schema::Version::Draft2019_09, Keyword}},
  46. {"additionalItems",
  47. {{schema::Version::Earliest, &Self::additionalItems},
  48. {schema::Version::Draft2020_12, Removed}}},
  49. {"additionalProperties", &Self::additionalProperties},
  50. {"allOf", {schema::Version::Draft04, &Self::allOf}},
  51. {"anyOf", {schema::Version::Draft04, &Self::anyOf}},
  52. {"const", {schema::Version::Draft06, &Self::isConstant}},
  53. {"contains", {schema::Version::Draft06, &Self::contains}},
  54. {"definitions", Keyword},
  55. {"dependencies", &Self::dependencies},
  56. {"dependentRequired", {schema::Version::Draft2019_09, &Self::dependentRequired}},
  57. {"dependentSchemas", {schema::Version::Draft2019_09, &Self::dependentSchemas}},
  58. {"divisibleBy",
  59. {{schema::Version::Earliest, &Self::multipleOf}, {schema::Version::Draft04, Removed}}},
  60. {"else", {{schema::Version::Draft07, Keyword}}},
  61. {"enum", &Self::isInEnumuration},
  62. {"exclusiveMaximum", {schema::Version::Draft06, &Self::exclusiveMaximum}},
  63. {"exclusiveMinimum", {schema::Version::Draft06, &Self::exclusiveMinimum}},
  64. {"format", &Self::format},
  65. {"if", {schema::Version::Draft07, &Self::ifThenElse}},
  66. {"items",
  67. {{schema::Version::Earliest, &Self::itemsTupleOrVector},
  68. {schema::Version::Draft2020_12, &Self::additionalItems}}},
  69. {"maxItems", &Self::maxItems},
  70. {"maxLength", &Self::maxLength},
  71. {"maxProperties", {schema::Version::Draft04, &Self::maxProperties}},
  72. {"maximum", &Self::maximum},
  73. {"minItems", &Self::minItems},
  74. {"minLength", &Self::minLength},
  75. {"minProperties", {schema::Version::Draft04, &Self::minProperties}},
  76. {"minimum", &Self::minimum},
  77. {"multipleOf", {schema::Version::Draft04, &Self::multipleOf}},
  78. {"not", {schema::Version::Draft04, &Self::isNot}},
  79. {"oneOf", {schema::Version::Draft04, &Self::oneOf}},
  80. {"pattern", &Self::pattern},
  81. {"patternProperties", &Self::patternProperties},
  82. {"prefixItems", {schema::Version::Draft2020_12, &Self::prefixItems}},
  83. {"properties", &Self::properties},
  84. {"propertyNames", {schema::Version::Draft06, &Self::propertyNames}},
  85. {"required", {schema::Version::Draft04, &Self::required}},
  86. {"then", {schema::Version::Draft07, Keyword}},
  87. {"type", &Self::type},
  88. {"unevaluatedItems", {schema::Version::Draft2019_09, &Self::unevaluatedItems}},
  89. {"unevaluatedProperties", {schema::Version::Draft2019_09, &Self::unevaluatedProperties}},
  90. {"uniqueItems", &Self::uniqueItems},
  91. };
  92. public:
  93. ConstraintFactory() = default;
  94. ConstraintFactory(std::initializer_list<std::pair<std::string_view, Versioned>> init) {
  95. constraints_.insert(init.begin(), init.end());
  96. }
  97. ConstraintFactory<A> && with_user_keyword(std::string_view word, Versioned make) && {
  98. constraints_.insert(word, std::move(make));
  99. return *this;
  100. }
  101. ConstraintFactory<A> && override_keyword(std::string_view word, Versioned make) && {
  102. constraints_[word] = std::move(make);
  103. return *this;
  104. }
  105. detail::Vocabulary<A> keywords(schema::Version version) const {
  106. std::unordered_map<std::string_view, MakeConstraint> rval;
  107. for (auto const & [key, versions] : constraints_) {
  108. if (auto it = versions.data.lower_bound(version); it != versions.data.end() && it->second) {
  109. rval.emplace(key, it->second);
  110. }
  111. }
  112. return detail::Vocabulary<A>(version, std::move(rval));
  113. }
  114. // SECTION: Untyped Constraints
  115. static auto type(detail::ParserContext<A> const & context) {
  116. static std::unordered_map<std::string_view, adapter::Type> const s_type_names{
  117. {"null", adapter::Type::Null}, {"boolean", adapter::Type::Boolean},
  118. {"integer", adapter::Type::Integer}, {"number", adapter::Type::Number},
  119. {"string", adapter::Type::String}, {"array", adapter::Type::Array},
  120. {"object", adapter::Type::Object},
  121. };
  122. auto to_type = [](std::string_view type) {
  123. EXPECT_M(s_type_names.contains(type), "Unknown type " << type);
  124. return s_type_names.at(type);
  125. };
  126. adapter::Type const type = context.schema.type();
  127. if (type == adapter::Type::String) {
  128. return std::make_unique<constraint::TypeConstraint>(to_type(context.schema.as_string()));
  129. }
  130. EXPECT(type == adapter::Type::Array);
  131. std::set<adapter::Type> types;
  132. for (auto subschema : context.schema.as_array()) {
  133. types.insert(to_type(subschema.as_string()));
  134. }
  135. return std::make_unique<constraint::TypeConstraint>(types);
  136. }
  137. static pConstraint ifThenElse(detail::ParserContext<A> const & context) {
  138. schema::Node const * then_ = context.fixed_schema(true);
  139. if (context.parent->contains("then")) {
  140. then_ = context.neighbor("then").node();
  141. }
  142. schema::Node const * else_ = context.fixed_schema(true);
  143. if (context.parent->contains("else")) {
  144. else_ = context.neighbor("else").node();
  145. }
  146. return std::make_unique<constraint::ConditionalConstraint>(context.node(), then_, else_);
  147. }
  148. static auto isInEnumuration(detail::ParserContext<A> const & context) {
  149. EXPECT(context.schema.type() == adapter::Type::Array);
  150. std::vector<std::unique_ptr<adapter::Const const>> rval;
  151. for (auto subschema : context.schema.as_array()) {
  152. rval.push_back(subschema.freeze());
  153. }
  154. return std::make_unique<constraint::EnumConstraint>(std::move(rval));
  155. }
  156. static auto isConstant(detail::ParserContext<A> const & context) {
  157. return std::make_unique<constraint::EnumConstraint>(context.schema.freeze());
  158. }
  159. static auto allOf(detail::ParserContext<A> const & context) {
  160. EXPECT(context.schema.type() == adapter::Type::Array);
  161. std::vector<schema::Node const *> rval;
  162. size_t index = 0;
  163. for (auto subschema : context.schema.as_array()) {
  164. rval.push_back(context.child(subschema, index).node());
  165. ++index;
  166. }
  167. return std::make_unique<constraint::AllOfConstraint>(rval);
  168. }
  169. static auto anyOf(detail::ParserContext<A> const & context) {
  170. EXPECT(context.schema.type() == adapter::Type::Array);
  171. std::vector<schema::Node const *> rval;
  172. size_t index = 0;
  173. for (auto subschema : context.schema.as_array()) {
  174. rval.push_back(context.child(subschema, index).node());
  175. ++index;
  176. }
  177. return std::make_unique<constraint::AnyOfConstraint>(rval);
  178. }
  179. static auto oneOf(detail::ParserContext<A> const & context) {
  180. EXPECT(context.schema.type() == adapter::Type::Array);
  181. std::vector<schema::Node const *> rval;
  182. size_t index = 0;
  183. for (auto subschema : context.schema.as_array()) {
  184. rval.push_back(context.child(subschema, index).node());
  185. ++index;
  186. }
  187. return std::make_unique<constraint::OneOfConstraint>(rval);
  188. }
  189. static auto isNot(detail::ParserContext<A> const & context) {
  190. return std::make_unique<constraint::NotConstraint>(context.node());
  191. }
  192. // SECTION: Numeric Constraints
  193. static auto minimum(detail::ParserContext<A> const & context) {
  194. double value = context.schema.as_number();
  195. if (context.vocab->version() < schema::Version::Draft06 &&
  196. context.parent->contains("exclusiveMinimum")) {
  197. auto exclusive = (*context.parent)["exclusiveMinimum"];
  198. EXPECT(exclusive.type() == adapter::Type::Boolean);
  199. return std::make_unique<constraint::MinimumConstraint>(value, exclusive.as_boolean());
  200. }
  201. return std::make_unique<constraint::MinimumConstraint>(value, false);
  202. }
  203. static pConstraint exclusiveMinimum(detail::ParserContext<A> const & context) {
  204. double value = context.schema.as_number();
  205. return std::make_unique<constraint::MinimumConstraint>(value, true);
  206. }
  207. static auto maximum(detail::ParserContext<A> const & context) {
  208. double value = context.schema.as_number();
  209. if (context.vocab->version() < schema::Version::Draft06 &&
  210. context.parent->contains("exclusiveMaximum")) {
  211. auto exclusive = (*context.parent)["exclusiveMaximum"];
  212. EXPECT(exclusive.type() == adapter::Type::Boolean);
  213. return std::make_unique<constraint::MaximumConstraint>(value, exclusive.as_boolean());
  214. }
  215. return std::make_unique<constraint::MaximumConstraint>(value, false);
  216. }
  217. static pConstraint exclusiveMaximum(detail::ParserContext<A> const & context) {
  218. double value = context.schema.as_number();
  219. return std::make_unique<constraint::MaximumConstraint>(value, true);
  220. }
  221. static auto multipleOf(detail::ParserContext<A> const & context) {
  222. double value = context.schema.as_number();
  223. return std::make_unique<constraint::MultipleOfConstraint>(value);
  224. }
  225. // SECTION: String Constraints
  226. static auto minLength(detail::ParserContext<A> const & context) {
  227. EXPECT(context.schema.type() == adapter::Type::Integer ||
  228. context.schema.type() == adapter::Type::Number);
  229. return std::make_unique<constraint::MinLengthConstraint>(context.schema.as_integer());
  230. }
  231. static auto maxLength(detail::ParserContext<A> const & context) {
  232. EXPECT(context.schema.type() == adapter::Type::Integer ||
  233. context.schema.type() == adapter::Type::Number);
  234. return std::make_unique<constraint::MaxLengthConstraint>(context.schema.as_integer());
  235. }
  236. static auto pattern(detail::ParserContext<A> const & context) {
  237. return std::make_unique<constraint::PatternConstraint>(context.schema.as_string());
  238. }
  239. static auto format(detail::ParserContext<A> const & context) {
  240. return std::make_unique<constraint::FormatConstraint>(context.schema.as_string());
  241. }
  242. // SECTION: Array Constraints
  243. static auto contains(detail::ParserContext<A> const & context) {
  244. if (context.vocab->version() < schema::Version::Draft2019_09) {
  245. return std::make_unique<constraint::ContainsConstraint>(context.node());
  246. }
  247. std::optional<size_t> maximum;
  248. std::optional<size_t> minimum;
  249. if (context.parent->contains("maxContains")) {
  250. maximum = (*context.parent)["maxContains"].as_integer();
  251. }
  252. if (context.parent->contains("minContains")) {
  253. minimum = (*context.parent)["minContains"].as_integer();
  254. }
  255. return std::make_unique<constraint::ContainsConstraint>(context.node(), minimum, maximum);
  256. }
  257. static auto minItems(detail::ParserContext<A> const & context) {
  258. EXPECT(context.schema.type() == adapter::Type::Integer ||
  259. context.schema.type() == adapter::Type::Number);
  260. return std::make_unique<constraint::MinItemsConstraint>(context.schema.as_integer());
  261. }
  262. static auto maxItems(detail::ParserContext<A> const & context) {
  263. EXPECT(context.schema.type() == adapter::Type::Integer ||
  264. context.schema.type() == adapter::Type::Number);
  265. return std::make_unique<constraint::MaxItemsConstraint>(context.schema.as_integer());
  266. }
  267. static auto prefixItems(detail::ParserContext<A> const & context) {
  268. EXPECT(context.schema.type() == adapter::Type::Array);
  269. std::vector<schema::Node const *> rval;
  270. size_t index = 0;
  271. for (auto subschema : context.schema.as_array()) {
  272. rval.push_back(context.child(subschema, index).node());
  273. ++index;
  274. }
  275. return std::make_unique<constraint::TupleConstraint>(rval);
  276. }
  277. static auto additionalItemsAfter(detail::ParserContext<A> const & context, size_t n) {
  278. using C = constraint::AdditionalItemsConstraint;
  279. if (context.vocab->version() < schema::Version::Draft06 &&
  280. context.schema.type() == adapter::Type::Boolean) {
  281. return std::make_unique<C>(context.always(), n);
  282. }
  283. return std::make_unique<C>(context.node(), n);
  284. }
  285. static pConstraint additionalItems(detail::ParserContext<A> const & context) {
  286. std::string const prefix =
  287. context.vocab->version() >= schema::Version::Draft2020_12 ? "prefixItems" : "items";
  288. Object const & parent = *context.parent;
  289. // Before Draft 2020-12, the "items" could be either a subschema or a tuple.
  290. // When not provided, we therefore treat it as an "accept-all" schema, and
  291. // thus will never have additionalItems to process. Similarly - if it is an
  292. // Object, then it must act on all items.
  293. if (context.vocab->version() < schema::Version::Draft2020_12 &&
  294. (not parent.contains(prefix) || parent[prefix].type() == adapter::Type::Object)) {
  295. return nullptr;
  296. }
  297. return additionalItemsAfter(context, parent[prefix].array_size());
  298. }
  299. static pConstraint itemsTupleOrVector(detail::ParserContext<A> const & context) {
  300. if (context.schema.type() == adapter::Type::Array) {
  301. return prefixItems(context);
  302. }
  303. return additionalItemsAfter(context, 0);
  304. }
  305. static auto unevaluatedItems(detail::ParserContext<A> const & context) {
  306. return std::make_unique<constraint::UnevaluatedItemsConstraint>(context.node());
  307. }
  308. static pConstraint uniqueItems(detail::ParserContext<A> const & context) {
  309. EXPECT(context.schema.type() == adapter::Type::Boolean);
  310. if (not context.schema.as_boolean()) {
  311. return nullptr;
  312. }
  313. return std::make_unique<constraint::UniqueItemsConstraint>();
  314. }
  315. // SECTION: Object Constraints
  316. static auto required(detail::ParserContext<A> const & context) {
  317. EXPECT(context.schema.type() == adapter::Type::Array);
  318. std::unordered_set<std::string> rval;
  319. for (auto subschema : context.schema.as_array()) {
  320. EXPECT(subschema.type() == adapter::Type::String);
  321. rval.insert(subschema.as_string());
  322. }
  323. return std::make_unique<constraint::RequiredConstraint>(rval);
  324. }
  325. static auto minProperties(detail::ParserContext<A> const & context) {
  326. EXPECT(context.schema.type() == adapter::Type::Integer ||
  327. context.schema.type() == adapter::Type::Number);
  328. return std::make_unique<constraint::MinPropertiesConstraint>(context.schema.as_integer());
  329. }
  330. static auto maxProperties(detail::ParserContext<A> const & context) {
  331. EXPECT(context.schema.type() == adapter::Type::Integer ||
  332. context.schema.type() == adapter::Type::Number);
  333. return std::make_unique<constraint::MaxPropertiesConstraint>(context.schema.as_integer());
  334. }
  335. static auto patternProperties(detail::ParserContext<A> const & context) {
  336. EXPECT(context.schema.type() == adapter::Type::Object);
  337. std::vector<std::pair<std::string, schema::Node const *>> rval;
  338. for (auto [prop, subschema] : context.schema.as_object()) {
  339. rval.emplace_back(prop, context.child(subschema, prop).node());
  340. }
  341. return std::make_unique<constraint::PatternPropertiesConstraint>(rval);
  342. }
  343. static auto properties(detail::ParserContext<A> const & context) {
  344. EXPECT(context.schema.type() == adapter::Type::Object);
  345. std::map<std::string, schema::Node const *> rval;
  346. for (auto [prop, subschema] : context.schema.as_object()) {
  347. rval.emplace(prop, context.child(subschema, prop).node());
  348. }
  349. return std::make_unique<constraint::PropertiesConstraint>(rval);
  350. }
  351. static auto propertyNames(detail::ParserContext<A> const & context) {
  352. return std::make_unique<constraint::PropertyNamesConstraint>(context.node());
  353. }
  354. static auto unevaluatedProperties(detail::ParserContext<A> const & context) {
  355. return std::make_unique<constraint::UnevaluatedPropertiesConstraint>(context.node());
  356. }
  357. static auto additionalProperties(detail::ParserContext<A> const & context) {
  358. std::unordered_set<std::string> properties;
  359. std::vector<std::string> patterns;
  360. Object const & parent = *context.parent;
  361. if (parent.contains("properties")) {
  362. for (auto [key, _] : parent["properties"].as_object()) {
  363. properties.insert(key);
  364. }
  365. }
  366. if (parent.contains("patternProperties")) {
  367. for (auto [key, _] : parent["patternProperties"].as_object()) {
  368. patterns.push_back(key);
  369. }
  370. }
  371. using C = constraint::AdditionalPropertiesConstraint;
  372. if (context.vocab->version() < schema::Version::Draft06 &&
  373. context.schema.type() == adapter::Type::Boolean) {
  374. return std::make_unique<C>(context.always(), properties, patterns);
  375. }
  376. return std::make_unique<C>(context.node(), properties, patterns);
  377. }
  378. static auto dependencies(detail::ParserContext<A> const & context) {
  379. EXPECT(context.schema.type() == adapter::Type::Object);
  380. std::map<std::string, schema::Node const *> schemas;
  381. std::map<std::string, std::unordered_set<std::string>> required;
  382. for (auto [prop, subschema] : context.schema.as_object()) {
  383. if (subschema.type() == adapter::Type::Array) {
  384. for (auto key : subschema.as_array()) {
  385. EXPECT(key.type() == adapter::Type::String);
  386. required[prop].insert(key.as_string());
  387. }
  388. } else {
  389. schemas.emplace(prop, context.child(subschema, prop).node());
  390. }
  391. }
  392. return std::make_unique<constraint::DependenciesConstraint>(schemas, required);
  393. }
  394. static auto dependentSchemas(detail::ParserContext<A> const & context) {
  395. EXPECT(context.schema.type() == adapter::Type::Object);
  396. std::map<std::string, schema::Node const *> rval;
  397. for (auto [prop, subschema] : context.schema.as_object()) {
  398. rval.emplace(prop, context.child(subschema, prop).node());
  399. }
  400. return std::make_unique<constraint::DependenciesConstraint>(rval);
  401. }
  402. static auto dependentRequired(detail::ParserContext<A> const & context) {
  403. EXPECT(context.schema.type() == adapter::Type::Object);
  404. std::map<std::string, std::unordered_set<std::string>> rval;
  405. for (auto [prop, subschema] : context.schema.as_object()) {
  406. EXPECT(subschema.type() == adapter::Type::Array);
  407. for (auto key : subschema.as_array()) {
  408. EXPECT(key.type() == adapter::Type::String);
  409. rval[prop].insert(key.as_string());
  410. }
  411. }
  412. return std::make_unique<constraint::DependenciesConstraint>(rval);
  413. }
  414. };
  415. }