constraint.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. {"disallow",
  59. {{schema::Version::Earliest, &Self::disallowDraft3}, {schema::Version::Draft04, Removed}}},
  60. {"divisibleBy",
  61. {{schema::Version::Earliest, &Self::multipleOf}, {schema::Version::Draft04, Removed}}},
  62. {"else", {{schema::Version::Draft07, Keyword}}},
  63. {"enum", &Self::isInEnumuration},
  64. {"exclusiveMaximum", {schema::Version::Draft06, &Self::exclusiveMaximum}},
  65. {"exclusiveMinimum", {schema::Version::Draft06, &Self::exclusiveMinimum}},
  66. {"extends",
  67. {{schema::Version::Earliest, &Self::extendsDraft3}, {schema::Version::Draft04, Removed}}},
  68. {"format", &Self::format},
  69. {"if", {schema::Version::Draft07, &Self::ifThenElse}},
  70. {"items",
  71. {{schema::Version::Earliest, &Self::itemsTupleOrVector},
  72. {schema::Version::Draft2020_12, &Self::additionalItems}}},
  73. {"maxItems", &Self::maxItems},
  74. {"maxLength", &Self::maxLength},
  75. {"maxProperties", {schema::Version::Draft04, &Self::maxProperties}},
  76. {"maximum", &Self::maximum},
  77. {"minItems", &Self::minItems},
  78. {"minLength", &Self::minLength},
  79. {"minProperties", {schema::Version::Draft04, &Self::minProperties}},
  80. {"minimum", &Self::minimum},
  81. {"multipleOf", {schema::Version::Draft04, &Self::multipleOf}},
  82. {"not", {schema::Version::Draft04, &Self::isNot}},
  83. {"oneOf", {schema::Version::Draft04, &Self::oneOf}},
  84. {"pattern", &Self::pattern},
  85. {"patternProperties", &Self::patternProperties},
  86. {"prefixItems", {schema::Version::Draft2020_12, &Self::prefixItems}},
  87. {"properties",
  88. {{schema::Version::Earliest, &Self::propertiesDraft3},
  89. {schema::Version::Draft04, &Self::properties}}},
  90. {"propertyNames", {schema::Version::Draft06, &Self::propertyNames}},
  91. {"required", {schema::Version::Draft04, &Self::required}},
  92. {"then", {schema::Version::Draft07, Keyword}},
  93. {"type",
  94. {{schema::Version::Earliest, &Self::typeDraft3}, {schema::Version::Draft04, &Self::type}}},
  95. {"unevaluatedItems", {schema::Version::Draft2019_09, &Self::unevaluatedItems}},
  96. {"unevaluatedProperties", {schema::Version::Draft2019_09, &Self::unevaluatedProperties}},
  97. {"uniqueItems", &Self::uniqueItems},
  98. };
  99. public:
  100. ConstraintFactory() = default;
  101. ConstraintFactory(std::initializer_list<std::pair<std::string_view, Versioned>> init) {
  102. constraints_.insert(init.begin(), init.end());
  103. }
  104. ConstraintFactory<A> && with_user_keyword(std::string_view word, Versioned make) && {
  105. constraints_.insert(word, std::move(make));
  106. return *this;
  107. }
  108. ConstraintFactory<A> && override_keyword(std::string_view word, Versioned make) && {
  109. constraints_[word] = std::move(make);
  110. return *this;
  111. }
  112. detail::Vocabulary<A> keywords(schema::Version version) const {
  113. std::unordered_map<std::string_view, MakeConstraint> rval;
  114. for (auto const & [key, versions] : constraints_) {
  115. if (auto it = versions.data.lower_bound(version); it != versions.data.end() && it->second) {
  116. rval.emplace(key, it->second);
  117. }
  118. }
  119. return detail::Vocabulary<A>(version, std::move(rval));
  120. }
  121. // SECTION: Untyped Constraints
  122. static auto type(detail::ParserContext<A> const & context) {
  123. static std::unordered_map<std::string_view, adapter::Type> const s_type_names{
  124. {"null", adapter::Type::Null}, {"boolean", adapter::Type::Boolean},
  125. {"integer", adapter::Type::Integer}, {"number", adapter::Type::Number},
  126. {"string", adapter::Type::String}, {"array", adapter::Type::Array},
  127. {"object", adapter::Type::Object},
  128. };
  129. auto to_type = [](std::string_view type) {
  130. EXPECT_M(s_type_names.contains(type), "Unknown type " << type);
  131. return s_type_names.at(type);
  132. };
  133. adapter::Type const type = context.schema.type();
  134. if (type == adapter::Type::String) {
  135. return std::make_unique<constraint::TypeConstraint>(to_type(context.schema.as_string()));
  136. }
  137. EXPECT(type == adapter::Type::Array);
  138. std::set<adapter::Type> types;
  139. for (auto subschema : context.schema.as_array()) {
  140. types.insert(to_type(subschema.as_string()));
  141. }
  142. return std::make_unique<constraint::TypeConstraint>(types);
  143. }
  144. static pConstraint typeDraft3(detail::ParserContext<A> const & context) {
  145. static std::unordered_map<std::string_view, adapter::Type> const s_type_names{
  146. {"null", adapter::Type::Null}, {"boolean", adapter::Type::Boolean},
  147. {"integer", adapter::Type::Integer}, {"number", adapter::Type::Number},
  148. {"string", adapter::Type::String}, {"array", adapter::Type::Array},
  149. {"object", adapter::Type::Object},
  150. };
  151. auto to_type = [](std::string_view type) {
  152. EXPECT_M(s_type_names.contains(type), "Unknown type " << type);
  153. return s_type_names.at(type);
  154. };
  155. adapter::Type const type = context.schema.type();
  156. if (type == adapter::Type::String) {
  157. if (context.schema.as_string() == "any") {
  158. return nullptr;
  159. }
  160. return std::make_unique<constraint::TypeConstraint>(to_type(context.schema.as_string()));
  161. }
  162. EXPECT(type == adapter::Type::Array);
  163. std::vector<schema::Node const *> children;
  164. std::set<adapter::Type> types;
  165. size_t index = 0;
  166. for (auto subschema : context.schema.as_array()) {
  167. if (subschema.type() != adapter::Type::String) {
  168. children.push_back(context.child(subschema, index).node());
  169. } else if (subschema.as_string() == "any") {
  170. return nullptr;
  171. } else {
  172. types.insert(to_type(subschema.as_string()));
  173. }
  174. ++index;
  175. }
  176. auto rval = std::make_unique<constraint::AnyOfConstraint>(children);
  177. rval->children.push_back(std::make_unique<constraint::TypeConstraint>(types));
  178. return rval;
  179. }
  180. static pConstraint disallowDraft3(detail::ParserContext<A> const & context) {
  181. return std::make_unique<constraint::NotConstraint>(typeDraft3(context));
  182. }
  183. static pConstraint extendsDraft3(detail::ParserContext<A> const & context) {
  184. std::vector<schema::Node const *> children;
  185. switch (context.schema.type()) {
  186. case adapter::Type::Object:
  187. children.push_back(context.node());
  188. break;
  189. case adapter::Type::Array: {
  190. size_t index = 0;
  191. for (auto const & subschema : context.schema.as_array()) {
  192. children.push_back(context.child(subschema, index).node());
  193. ++index;
  194. }
  195. break;
  196. }
  197. default:
  198. JVALIDATE_THROW(std::runtime_error, "extends must be a schema of array-of-schemas");
  199. }
  200. return std::make_unique<constraint::AllOfConstraint>(children);
  201. }
  202. static pConstraint ifThenElse(detail::ParserContext<A> const & context) {
  203. schema::Node const * then_ = context.fixed_schema(true);
  204. if (context.parent->contains("then")) {
  205. then_ = context.neighbor("then").node();
  206. }
  207. schema::Node const * else_ = context.fixed_schema(true);
  208. if (context.parent->contains("else")) {
  209. else_ = context.neighbor("else").node();
  210. }
  211. return std::make_unique<constraint::ConditionalConstraint>(context.node(), then_, else_);
  212. }
  213. static auto isInEnumuration(detail::ParserContext<A> const & context) {
  214. EXPECT(context.schema.type() == adapter::Type::Array);
  215. std::vector<std::unique_ptr<adapter::Const const>> rval;
  216. for (auto subschema : context.schema.as_array()) {
  217. rval.push_back(subschema.freeze());
  218. }
  219. return std::make_unique<constraint::EnumConstraint>(std::move(rval));
  220. }
  221. static auto isConstant(detail::ParserContext<A> const & context) {
  222. return std::make_unique<constraint::EnumConstraint>(context.schema.freeze());
  223. }
  224. static auto allOf(detail::ParserContext<A> const & context) {
  225. EXPECT(context.schema.type() == adapter::Type::Array);
  226. std::vector<schema::Node const *> rval;
  227. size_t index = 0;
  228. for (auto subschema : context.schema.as_array()) {
  229. rval.push_back(context.child(subschema, index).node());
  230. ++index;
  231. }
  232. return std::make_unique<constraint::AllOfConstraint>(rval);
  233. }
  234. static auto anyOf(detail::ParserContext<A> const & context) {
  235. EXPECT(context.schema.type() == adapter::Type::Array);
  236. std::vector<schema::Node const *> rval;
  237. size_t index = 0;
  238. for (auto subschema : context.schema.as_array()) {
  239. rval.push_back(context.child(subschema, index).node());
  240. ++index;
  241. }
  242. return std::make_unique<constraint::AnyOfConstraint>(rval);
  243. }
  244. static auto oneOf(detail::ParserContext<A> const & context) {
  245. EXPECT(context.schema.type() == adapter::Type::Array);
  246. std::vector<schema::Node const *> rval;
  247. size_t index = 0;
  248. for (auto subschema : context.schema.as_array()) {
  249. rval.push_back(context.child(subschema, index).node());
  250. ++index;
  251. }
  252. return std::make_unique<constraint::OneOfConstraint>(rval);
  253. }
  254. static auto isNot(detail::ParserContext<A> const & context) {
  255. return std::make_unique<constraint::NotConstraint>(context.node());
  256. }
  257. // SECTION: Numeric Constraints
  258. static auto minimum(detail::ParserContext<A> const & context) {
  259. double value = context.schema.as_number();
  260. if (context.vocab->version() < schema::Version::Draft06 &&
  261. context.parent->contains("exclusiveMinimum")) {
  262. auto exclusive = (*context.parent)["exclusiveMinimum"];
  263. EXPECT(exclusive.type() == adapter::Type::Boolean);
  264. return std::make_unique<constraint::MinimumConstraint>(value, exclusive.as_boolean());
  265. }
  266. return std::make_unique<constraint::MinimumConstraint>(value, false);
  267. }
  268. static pConstraint exclusiveMinimum(detail::ParserContext<A> const & context) {
  269. double value = context.schema.as_number();
  270. return std::make_unique<constraint::MinimumConstraint>(value, true);
  271. }
  272. static auto maximum(detail::ParserContext<A> const & context) {
  273. double value = context.schema.as_number();
  274. if (context.vocab->version() < schema::Version::Draft06 &&
  275. context.parent->contains("exclusiveMaximum")) {
  276. auto exclusive = (*context.parent)["exclusiveMaximum"];
  277. EXPECT(exclusive.type() == adapter::Type::Boolean);
  278. return std::make_unique<constraint::MaximumConstraint>(value, exclusive.as_boolean());
  279. }
  280. return std::make_unique<constraint::MaximumConstraint>(value, false);
  281. }
  282. static pConstraint exclusiveMaximum(detail::ParserContext<A> const & context) {
  283. double value = context.schema.as_number();
  284. return std::make_unique<constraint::MaximumConstraint>(value, true);
  285. }
  286. static auto multipleOf(detail::ParserContext<A> const & context) {
  287. double value = context.schema.as_number();
  288. return std::make_unique<constraint::MultipleOfConstraint>(value);
  289. }
  290. // SECTION: String Constraints
  291. static auto minLength(detail::ParserContext<A> const & context) {
  292. EXPECT(context.schema.type() == adapter::Type::Integer ||
  293. context.schema.type() == adapter::Type::Number);
  294. return std::make_unique<constraint::MinLengthConstraint>(context.schema.as_integer());
  295. }
  296. static auto maxLength(detail::ParserContext<A> const & context) {
  297. EXPECT(context.schema.type() == adapter::Type::Integer ||
  298. context.schema.type() == adapter::Type::Number);
  299. return std::make_unique<constraint::MaxLengthConstraint>(context.schema.as_integer());
  300. }
  301. static auto pattern(detail::ParserContext<A> const & context) {
  302. return std::make_unique<constraint::PatternConstraint>(context.schema.as_string());
  303. }
  304. static auto format(detail::ParserContext<A> const & context) {
  305. return std::make_unique<constraint::FormatConstraint>(context.schema.as_string(),
  306. context.vocab->is_format_assertion());
  307. }
  308. // SECTION: Array Constraints
  309. static auto contains(detail::ParserContext<A> const & context) {
  310. if (context.vocab->version() < schema::Version::Draft2019_09) {
  311. return std::make_unique<constraint::ContainsConstraint>(context.node());
  312. }
  313. std::optional<size_t> maximum;
  314. std::optional<size_t> minimum;
  315. if (context.parent->contains("maxContains")) {
  316. maximum = (*context.parent)["maxContains"].as_integer();
  317. }
  318. if (context.parent->contains("minContains")) {
  319. minimum = (*context.parent)["minContains"].as_integer();
  320. }
  321. return std::make_unique<constraint::ContainsConstraint>(context.node(), minimum, maximum);
  322. }
  323. static auto minItems(detail::ParserContext<A> const & context) {
  324. EXPECT(context.schema.type() == adapter::Type::Integer ||
  325. context.schema.type() == adapter::Type::Number);
  326. return std::make_unique<constraint::MinItemsConstraint>(context.schema.as_integer());
  327. }
  328. static auto maxItems(detail::ParserContext<A> const & context) {
  329. EXPECT(context.schema.type() == adapter::Type::Integer ||
  330. context.schema.type() == adapter::Type::Number);
  331. return std::make_unique<constraint::MaxItemsConstraint>(context.schema.as_integer());
  332. }
  333. static auto prefixItems(detail::ParserContext<A> const & context) {
  334. EXPECT(context.schema.type() == adapter::Type::Array);
  335. std::vector<schema::Node const *> rval;
  336. size_t index = 0;
  337. for (auto subschema : context.schema.as_array()) {
  338. rval.push_back(context.child(subschema, index).node());
  339. ++index;
  340. }
  341. return std::make_unique<constraint::TupleConstraint>(rval);
  342. }
  343. static auto additionalItemsAfter(detail::ParserContext<A> const & context, size_t n) {
  344. using C = constraint::AdditionalItemsConstraint;
  345. if (context.vocab->version() < schema::Version::Draft06 &&
  346. context.schema.type() == adapter::Type::Boolean) {
  347. return std::make_unique<C>(context.always(), n);
  348. }
  349. return std::make_unique<C>(context.node(), n);
  350. }
  351. static pConstraint additionalItems(detail::ParserContext<A> const & context) {
  352. std::string const prefix =
  353. context.vocab->version() >= schema::Version::Draft2020_12 ? "prefixItems" : "items";
  354. Object const & parent = *context.parent;
  355. // Before Draft 2020-12, the "items" could be either a subschema or a tuple.
  356. // When not provided, we therefore treat it as an "accept-all" schema, and
  357. // thus will never have additionalItems to process. Similarly - if it is an
  358. // Object, then it must act on all items.
  359. if (context.vocab->version() < schema::Version::Draft2020_12 &&
  360. (not parent.contains(prefix) || parent[prefix].type() == adapter::Type::Object)) {
  361. return nullptr;
  362. }
  363. return additionalItemsAfter(context, parent[prefix].array_size());
  364. }
  365. static pConstraint itemsTupleOrVector(detail::ParserContext<A> const & context) {
  366. if (context.schema.type() == adapter::Type::Array) {
  367. return prefixItems(context);
  368. }
  369. return additionalItemsAfter(context, 0);
  370. }
  371. static auto unevaluatedItems(detail::ParserContext<A> const & context) {
  372. return std::make_unique<constraint::UnevaluatedItemsConstraint>(context.node());
  373. }
  374. static pConstraint uniqueItems(detail::ParserContext<A> const & context) {
  375. EXPECT(context.schema.type() == adapter::Type::Boolean);
  376. if (not context.schema.as_boolean()) {
  377. return nullptr;
  378. }
  379. return std::make_unique<constraint::UniqueItemsConstraint>();
  380. }
  381. // SECTION: Object Constraints
  382. static auto required(detail::ParserContext<A> const & context) {
  383. EXPECT(context.schema.type() == adapter::Type::Array);
  384. std::unordered_set<std::string> rval;
  385. for (auto subschema : context.schema.as_array()) {
  386. EXPECT(subschema.type() == adapter::Type::String);
  387. rval.insert(subschema.as_string());
  388. }
  389. return std::make_unique<constraint::RequiredConstraint>(rval);
  390. }
  391. static auto minProperties(detail::ParserContext<A> const & context) {
  392. EXPECT(context.schema.type() == adapter::Type::Integer ||
  393. context.schema.type() == adapter::Type::Number);
  394. return std::make_unique<constraint::MinPropertiesConstraint>(context.schema.as_integer());
  395. }
  396. static auto maxProperties(detail::ParserContext<A> const & context) {
  397. EXPECT(context.schema.type() == adapter::Type::Integer ||
  398. context.schema.type() == adapter::Type::Number);
  399. return std::make_unique<constraint::MaxPropertiesConstraint>(context.schema.as_integer());
  400. }
  401. static auto patternProperties(detail::ParserContext<A> const & context) {
  402. EXPECT(context.schema.type() == adapter::Type::Object);
  403. std::vector<std::pair<std::string, schema::Node const *>> rval;
  404. for (auto [prop, subschema] : context.schema.as_object()) {
  405. rval.emplace_back(prop, context.child(subschema, prop).node());
  406. }
  407. return std::make_unique<constraint::PatternPropertiesConstraint>(rval);
  408. }
  409. static auto properties(detail::ParserContext<A> const & context) {
  410. EXPECT(context.schema.type() == adapter::Type::Object);
  411. std::map<std::string, schema::Node const *> rval;
  412. for (auto [prop, subschema] : context.schema.as_object()) {
  413. rval.emplace(prop, context.child(subschema, prop).node());
  414. }
  415. return std::make_unique<constraint::PropertiesConstraint>(rval);
  416. }
  417. static pConstraint propertiesDraft3(detail::ParserContext<A> const & context) {
  418. EXPECT(context.schema.type() == adapter::Type::Object);
  419. std::unordered_set<std::string> required;
  420. for (auto [prop, subschema] : context.schema.as_object()) {
  421. EXPECT(subschema.type() == adapter::Type::Object);
  422. if (auto sub = subschema.as_object();
  423. sub.contains("required") && sub["required"].as_boolean()) {
  424. required.insert(prop);
  425. }
  426. }
  427. if (required.empty()) {
  428. return properties(context);
  429. }
  430. std::vector<pConstraint> rval;
  431. rval.push_back(properties(context));
  432. rval.push_back(std::make_unique<constraint::RequiredConstraint>(std::move(required)));
  433. return std::make_unique<constraint::AllOfConstraint>(std::move(rval));
  434. }
  435. static auto propertyNames(detail::ParserContext<A> const & context) {
  436. return std::make_unique<constraint::PropertyNamesConstraint>(context.node());
  437. }
  438. static auto unevaluatedProperties(detail::ParserContext<A> const & context) {
  439. return std::make_unique<constraint::UnevaluatedPropertiesConstraint>(context.node());
  440. }
  441. static auto additionalProperties(detail::ParserContext<A> const & context) {
  442. std::unordered_set<std::string> properties;
  443. std::vector<std::string> patterns;
  444. Object const & parent = *context.parent;
  445. if (parent.contains("properties")) {
  446. for (auto [key, _] : parent["properties"].as_object()) {
  447. properties.insert(key);
  448. }
  449. }
  450. if (parent.contains("patternProperties")) {
  451. for (auto [key, _] : parent["patternProperties"].as_object()) {
  452. patterns.push_back(key);
  453. }
  454. }
  455. using C = constraint::AdditionalPropertiesConstraint;
  456. if (context.vocab->version() < schema::Version::Draft06 &&
  457. context.schema.type() == adapter::Type::Boolean) {
  458. return std::make_unique<C>(context.always(), properties, patterns);
  459. }
  460. return std::make_unique<C>(context.node(), properties, patterns);
  461. }
  462. static auto dependencies(detail::ParserContext<A> const & context) {
  463. EXPECT(context.schema.type() == adapter::Type::Object);
  464. std::map<std::string, schema::Node const *> schemas;
  465. std::map<std::string, std::unordered_set<std::string>> required;
  466. for (auto [prop, subschema] : context.schema.as_object()) {
  467. if (subschema.type() == adapter::Type::Array) {
  468. for (auto key : subschema.as_array()) {
  469. EXPECT(key.type() == adapter::Type::String);
  470. required[prop].insert(key.as_string());
  471. }
  472. } else if (context.vocab->version() <= schema::Version::Draft03 &&
  473. subschema.type() == adapter::Type::String) {
  474. required[prop].insert(subschema.as_string());
  475. } else {
  476. schemas.emplace(prop, context.child(subschema, prop).node());
  477. }
  478. }
  479. return std::make_unique<constraint::DependenciesConstraint>(schemas, required);
  480. }
  481. static auto dependentSchemas(detail::ParserContext<A> const & context) {
  482. EXPECT(context.schema.type() == adapter::Type::Object);
  483. std::map<std::string, schema::Node const *> rval;
  484. for (auto [prop, subschema] : context.schema.as_object()) {
  485. rval.emplace(prop, context.child(subschema, prop).node());
  486. }
  487. return std::make_unique<constraint::DependenciesConstraint>(rval);
  488. }
  489. static auto dependentRequired(detail::ParserContext<A> const & context) {
  490. EXPECT(context.schema.type() == adapter::Type::Object);
  491. std::map<std::string, std::unordered_set<std::string>> rval;
  492. for (auto [prop, subschema] : context.schema.as_object()) {
  493. EXPECT(subschema.type() == adapter::Type::Array);
  494. for (auto key : subschema.as_array()) {
  495. EXPECT(key.type() == adapter::Type::String);
  496. rval[prop].insert(key.as_string());
  497. }
  498. }
  499. return std::make_unique<constraint::DependenciesConstraint>(rval);
  500. }
  501. };
  502. }