constraint.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 auto ifThenElse(detail::ParserContext<A> const & context) {
  124. return std::make_unique<constraint::ConditionalConstraint>(
  125. context.node(), context.neighbor("then").node(), context.neighbor("else").node());
  126. }
  127. static auto isInEnumuration(detail::ParserContext<A> const & context) {
  128. EXPECT(context.schema.type() == adapter::Type::Array);
  129. std::vector<std::unique_ptr<adapter::Const const>> rval;
  130. for (auto subschema : context.schema.as_array()) {
  131. rval.push_back(subschema.freeze());
  132. }
  133. return std::make_unique<constraint::EnumConstraint>(std::move(rval));
  134. }
  135. static auto isConstant(detail::ParserContext<A> const & context) {
  136. return std::make_unique<constraint::EnumConstraint>(context.schema.freeze());
  137. }
  138. static auto allOf(detail::ParserContext<A> const & context) {
  139. EXPECT(context.schema.type() == adapter::Type::Array);
  140. std::vector<schema::Node const *> rval;
  141. size_t index = 0;
  142. for (auto subschema : context.schema.as_array()) {
  143. rval.push_back(context.child(subschema, index).node());
  144. ++index;
  145. }
  146. return std::make_unique<constraint::AllOfConstraint>(rval);
  147. }
  148. static auto anyOf(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::AnyOfConstraint>(rval);
  157. }
  158. static auto oneOf(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::OneOfConstraint>(rval);
  167. }
  168. static auto isNot(detail::ParserContext<A> const & context) {
  169. return std::make_unique<constraint::NotConstraint>(context.node());
  170. }
  171. // SECTION: Numeric Constraints
  172. static auto minimum(detail::ParserContext<A> const & context) {
  173. double value = context.schema.as_number();
  174. if (context.version < schema::Version::Draft06 &&
  175. context.parent->contains("exclusiveMinimum")) {
  176. auto exclusive = (*context.parent)["exclusiveMinimum"];
  177. EXPECT(exclusive.type() == adapter::Type::Boolean);
  178. return std::make_unique<constraint::MinimumConstraint>(value, exclusive.as_boolean());
  179. }
  180. return std::make_unique<constraint::MinimumConstraint>(value, false);
  181. }
  182. static pConstraint exclusiveMinimum(detail::ParserContext<A> const & context) {
  183. double value = context.schema.as_number();
  184. return std::make_unique<constraint::MinimumConstraint>(value, true);
  185. }
  186. static auto maximum(detail::ParserContext<A> const & context) {
  187. double value = context.schema.as_number();
  188. if (context.version < schema::Version::Draft06 &&
  189. context.parent->contains("exclusiveMaximum")) {
  190. auto exclusive = (*context.parent)["exclusiveMaximum"];
  191. EXPECT(exclusive.type() == adapter::Type::Boolean);
  192. return std::make_unique<constraint::MaximumConstraint>(value, exclusive.as_boolean());
  193. }
  194. return std::make_unique<constraint::MaximumConstraint>(value, false);
  195. }
  196. static pConstraint exclusiveMaximum(detail::ParserContext<A> const & context) {
  197. double value = context.schema.as_number();
  198. return std::make_unique<constraint::MaximumConstraint>(value, true);
  199. }
  200. static auto multipleOf(detail::ParserContext<A> const & context) {
  201. int64_t value = context.schema.as_integer();
  202. return std::make_unique<constraint::MultipleOfConstraint>(value);
  203. }
  204. // SECTION: String Constraints
  205. static auto minLength(detail::ParserContext<A> const & context) {
  206. EXPECT(context.schema.type() == adapter::Type::Integer);
  207. return std::make_unique<constraint::MinLengthConstraint>(context.schema.as_integer());
  208. }
  209. static auto maxLength(detail::ParserContext<A> const & context) {
  210. EXPECT(context.schema.type() == adapter::Type::Integer);
  211. return std::make_unique<constraint::MaxLengthConstraint>(context.schema.as_integer());
  212. }
  213. static auto pattern(detail::ParserContext<A> const & context) {
  214. return std::make_unique<constraint::PatternConstraint>(context.schema.as_string());
  215. }
  216. // SECTION: Array Constraints
  217. static auto contains(detail::ParserContext<A> const & context) {
  218. if (context.version < schema::Version::Draft2019_09) {
  219. return std::make_unique<constraint::ContainsConstraint>(context.node());
  220. }
  221. std::optional<size_t> maximum;
  222. std::optional<size_t> minimum;
  223. if (context.parent->contains("maxContains")) {
  224. maximum = (*context.parent)["maxContains"].as_integer();
  225. }
  226. if (context.parent->contains("minContains")) {
  227. minimum = (*context.parent)["minContains"].as_integer();
  228. }
  229. return std::make_unique<constraint::ContainsConstraint>(context.node(), minimum, maximum);
  230. }
  231. static auto minItems(detail::ParserContext<A> const & context) {
  232. EXPECT(context.schema.type() == adapter::Type::Integer);
  233. return std::make_unique<constraint::MinItemsConstraint>(context.schema.as_integer());
  234. }
  235. static auto maxItems(detail::ParserContext<A> const & context) {
  236. EXPECT(context.schema.type() == adapter::Type::Integer);
  237. return std::make_unique<constraint::MaxItemsConstraint>(context.schema.as_integer());
  238. }
  239. static auto prefixItems(detail::ParserContext<A> const & context) {
  240. EXPECT(context.schema.type() == adapter::Type::Array);
  241. std::vector<schema::Node const *> rval;
  242. size_t index = 0;
  243. for (auto subschema : context.schema.as_array()) {
  244. rval.push_back(context.child(subschema, index).node());
  245. ++index;
  246. }
  247. return std::make_unique<constraint::TupleConstraint>(rval);
  248. }
  249. static pConstraint additionalItems(detail::ParserContext<A> const & context) {
  250. std::string const prefix =
  251. context.version >= schema::Version::Draft2020_12 ? "prefixItems" : "items";
  252. Object const & parent = *context.parent;
  253. size_t start_after = 0;
  254. if (not prefix.empty() && parent.contains(prefix)) {
  255. start_after = parent[prefix].as_integer();
  256. }
  257. using C = constraint::AdditionalItemsConstraint;
  258. if (context.version < schema::Version::Draft06 &&
  259. context.schema.type() == adapter::Type::Boolean) {
  260. return std::make_unique<C>(context.always(), start_after);
  261. }
  262. return std::make_unique<C>(context.node(), start_after);
  263. }
  264. static pConstraint itemsTupleOrVector(detail::ParserContext<A> const & context) {
  265. if (context.schema.type() == adapter::Type::Array) {
  266. return prefixItems(context);
  267. }
  268. return additionalItems(context);
  269. }
  270. static auto unevaluatedItems(detail::ParserContext<A> const & context) {
  271. return std::make_unique<constraint::UnevaluatedItemsConstraint>(context.node());
  272. }
  273. static pConstraint uniqueItems(detail::ParserContext<A> const & context) {
  274. EXPECT(context.schema.type() == adapter::Type::Boolean);
  275. if (not context.schema.as_boolean()) {
  276. return nullptr;
  277. }
  278. return std::make_unique<constraint::UniqueItemsConstraint>();
  279. }
  280. // SECTION: Object Constraints
  281. static auto required(detail::ParserContext<A> const & context) {
  282. EXPECT(context.schema.type() == adapter::Type::Array);
  283. std::unordered_set<std::string> rval;
  284. for (auto subschema : context.schema.as_array()) {
  285. EXPECT(subschema.type() == adapter::Type::String);
  286. rval.insert(subschema.as_string());
  287. }
  288. return std::make_unique<constraint::RequiredConstraint>(rval);
  289. }
  290. static auto minProperties(detail::ParserContext<A> const & context) {
  291. EXPECT(context.schema.type() == adapter::Type::Integer);
  292. return std::make_unique<constraint::MinPropertiesConstraint>(context.schema.as_integer());
  293. }
  294. static auto maxProperties(detail::ParserContext<A> const & context) {
  295. EXPECT(context.schema.type() == adapter::Type::Integer);
  296. return std::make_unique<constraint::MaxPropertiesConstraint>(context.schema.as_integer());
  297. }
  298. static auto patternProperties(detail::ParserContext<A> const & context) {
  299. EXPECT(context.schema.type() == adapter::Type::Object);
  300. std::vector<std::pair<std::string, schema::Node const *>> rval;
  301. for (auto [prop, subschema] : context.schema.as_object()) {
  302. rval.emplace_back(prop, context.child(subschema, prop).node());
  303. }
  304. return std::make_unique<constraint::PatternPropertiesConstraint>(rval);
  305. }
  306. static auto properties(detail::ParserContext<A> const & context) {
  307. EXPECT(context.schema.type() == adapter::Type::Object);
  308. std::map<std::string, schema::Node const *> rval;
  309. for (auto [prop, subschema] : context.schema.as_object()) {
  310. rval.emplace(prop, context.child(subschema, prop).node());
  311. }
  312. return std::make_unique<constraint::PropertiesConstraint>(rval);
  313. }
  314. static auto propertyNames(detail::ParserContext<A> const & context) {
  315. return std::make_unique<constraint::PropertyNamesConstraint>(context.node());
  316. }
  317. static auto unevaluatedProperties(detail::ParserContext<A> const & context) {
  318. return std::make_unique<constraint::UnevaluatedPropertiesConstraint>(context.node());
  319. }
  320. static auto additionalProperties(detail::ParserContext<A> const & context) {
  321. std::unordered_set<std::string> properties;
  322. std::vector<std::string> patterns;
  323. Object const & parent = *context.parent;
  324. if (parent.contains("properties")) {
  325. for (auto [key, _] : parent["properties"].as_object()) {
  326. properties.insert(key);
  327. }
  328. }
  329. if (parent.contains("patternProperties")) {
  330. for (auto [key, _] : parent["patternProperties"].as_object()) {
  331. patterns.push_back(key);
  332. }
  333. }
  334. using C = constraint::AdditionalPropertiesConstraint;
  335. if (context.version < schema::Version::Draft06 &&
  336. context.schema.type() == adapter::Type::Boolean) {
  337. return std::make_unique<C>(context.always(), properties, patterns);
  338. }
  339. return std::make_unique<C>(context.node(), properties, patterns);
  340. }
  341. static auto dependencies(detail::ParserContext<A> const & context) {
  342. EXPECT(context.schema.type() == adapter::Type::Object);
  343. std::map<std::string, schema::Node const *> schemas;
  344. std::map<std::string, std::unordered_set<std::string>> required;
  345. for (auto [prop, subschema] : context.schema.as_object()) {
  346. if (subschema.type() == adapter::Type::Object) {
  347. schemas.emplace(prop, context.child(subschema, prop).node());
  348. } else {
  349. for (auto key : subschema.as_array()) {
  350. EXPECT(key.type() == adapter::Type::String);
  351. required[prop].insert(key.as_string());
  352. }
  353. }
  354. }
  355. return std::make_unique<constraint::DependenciesConstraint>(schemas, required);
  356. }
  357. static auto dependentSchemas(detail::ParserContext<A> const & context) {
  358. EXPECT(context.schema.type() == adapter::Type::Object);
  359. std::map<std::string, schema::Node const *> rval;
  360. for (auto [prop, subschema] : context.schema.as_object()) {
  361. rval.emplace(prop, context.child(subschema, prop).node());
  362. }
  363. return std::make_unique<constraint::DependenciesConstraint>(rval);
  364. }
  365. static auto dependentRequired(detail::ParserContext<A> const & context) {
  366. EXPECT(context.schema.type() == adapter::Type::Object);
  367. std::map<std::string, std::unordered_set<std::string>> rval;
  368. for (auto [prop, subschema] : context.schema.as_object()) {
  369. EXPECT(subschema.type() == adapter::Type::Array);
  370. for (auto key : subschema.as_array()) {
  371. EXPECT(key.type() == adapter::Type::String);
  372. rval[prop].insert(key.as_string());
  373. }
  374. }
  375. return std::make_unique<constraint::DependenciesConstraint>(rval);
  376. }
  377. };
  378. }