constraint.h 18 KB

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