constraint.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. return constraint::PolyConstraint::AnyOf(
  177. std::make_unique<constraint::TypeConstraint>(types),
  178. std::make_unique<constraint::AnyOfConstraint>(children));
  179. }
  180. static pConstraint disallowDraft3(detail::ParserContext<A> const & context) {
  181. return constraint::PolyConstraint::Not(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. }
  307. // SECTION: Array Constraints
  308. static auto contains(detail::ParserContext<A> const & context) {
  309. if (context.vocab->version() < schema::Version::Draft2019_09) {
  310. return std::make_unique<constraint::ContainsConstraint>(context.node());
  311. }
  312. std::optional<size_t> maximum;
  313. std::optional<size_t> minimum;
  314. if (context.parent->contains("maxContains")) {
  315. maximum = (*context.parent)["maxContains"].as_integer();
  316. }
  317. if (context.parent->contains("minContains")) {
  318. minimum = (*context.parent)["minContains"].as_integer();
  319. }
  320. return std::make_unique<constraint::ContainsConstraint>(context.node(), minimum, maximum);
  321. }
  322. static auto minItems(detail::ParserContext<A> const & context) {
  323. EXPECT(context.schema.type() == adapter::Type::Integer ||
  324. context.schema.type() == adapter::Type::Number);
  325. return std::make_unique<constraint::MinItemsConstraint>(context.schema.as_integer());
  326. }
  327. static auto maxItems(detail::ParserContext<A> const & context) {
  328. EXPECT(context.schema.type() == adapter::Type::Integer ||
  329. context.schema.type() == adapter::Type::Number);
  330. return std::make_unique<constraint::MaxItemsConstraint>(context.schema.as_integer());
  331. }
  332. static auto prefixItems(detail::ParserContext<A> const & context) {
  333. EXPECT(context.schema.type() == adapter::Type::Array);
  334. std::vector<schema::Node const *> rval;
  335. size_t index = 0;
  336. for (auto subschema : context.schema.as_array()) {
  337. rval.push_back(context.child(subschema, index).node());
  338. ++index;
  339. }
  340. return std::make_unique<constraint::TupleConstraint>(rval);
  341. }
  342. static auto additionalItemsAfter(detail::ParserContext<A> const & context, size_t n) {
  343. using C = constraint::AdditionalItemsConstraint;
  344. if (context.vocab->version() < schema::Version::Draft06 &&
  345. context.schema.type() == adapter::Type::Boolean) {
  346. return std::make_unique<C>(context.always(), n);
  347. }
  348. return std::make_unique<C>(context.node(), n);
  349. }
  350. static pConstraint additionalItems(detail::ParserContext<A> const & context) {
  351. std::string const prefix =
  352. context.vocab->version() >= schema::Version::Draft2020_12 ? "prefixItems" : "items";
  353. Object const & parent = *context.parent;
  354. // Before Draft 2020-12, the "items" could be either a subschema or a tuple.
  355. // When not provided, we therefore treat it as an "accept-all" schema, and
  356. // thus will never have additionalItems to process. Similarly - if it is an
  357. // Object, then it must act on all items.
  358. if (context.vocab->version() < schema::Version::Draft2020_12 &&
  359. (not parent.contains(prefix) || parent[prefix].type() == adapter::Type::Object)) {
  360. return nullptr;
  361. }
  362. return additionalItemsAfter(context, parent[prefix].array_size());
  363. }
  364. static pConstraint itemsTupleOrVector(detail::ParserContext<A> const & context) {
  365. if (context.schema.type() == adapter::Type::Array) {
  366. return prefixItems(context);
  367. }
  368. return additionalItemsAfter(context, 0);
  369. }
  370. static auto unevaluatedItems(detail::ParserContext<A> const & context) {
  371. return std::make_unique<constraint::UnevaluatedItemsConstraint>(context.node());
  372. }
  373. static pConstraint uniqueItems(detail::ParserContext<A> const & context) {
  374. EXPECT(context.schema.type() == adapter::Type::Boolean);
  375. if (not context.schema.as_boolean()) {
  376. return nullptr;
  377. }
  378. return std::make_unique<constraint::UniqueItemsConstraint>();
  379. }
  380. // SECTION: Object Constraints
  381. static auto required(detail::ParserContext<A> const & context) {
  382. EXPECT(context.schema.type() == adapter::Type::Array);
  383. std::unordered_set<std::string> rval;
  384. for (auto subschema : context.schema.as_array()) {
  385. EXPECT(subschema.type() == adapter::Type::String);
  386. rval.insert(subschema.as_string());
  387. }
  388. return std::make_unique<constraint::RequiredConstraint>(rval);
  389. }
  390. static auto minProperties(detail::ParserContext<A> const & context) {
  391. EXPECT(context.schema.type() == adapter::Type::Integer ||
  392. context.schema.type() == adapter::Type::Number);
  393. return std::make_unique<constraint::MinPropertiesConstraint>(context.schema.as_integer());
  394. }
  395. static auto maxProperties(detail::ParserContext<A> const & context) {
  396. EXPECT(context.schema.type() == adapter::Type::Integer ||
  397. context.schema.type() == adapter::Type::Number);
  398. return std::make_unique<constraint::MaxPropertiesConstraint>(context.schema.as_integer());
  399. }
  400. static auto patternProperties(detail::ParserContext<A> const & context) {
  401. EXPECT(context.schema.type() == adapter::Type::Object);
  402. std::vector<std::pair<std::string, schema::Node const *>> rval;
  403. for (auto [prop, subschema] : context.schema.as_object()) {
  404. rval.emplace_back(prop, context.child(subschema, prop).node());
  405. }
  406. return std::make_unique<constraint::PatternPropertiesConstraint>(rval);
  407. }
  408. static auto properties(detail::ParserContext<A> const & context) {
  409. EXPECT(context.schema.type() == adapter::Type::Object);
  410. std::map<std::string, schema::Node const *> rval;
  411. for (auto [prop, subschema] : context.schema.as_object()) {
  412. rval.emplace(prop, context.child(subschema, prop).node());
  413. }
  414. return std::make_unique<constraint::PropertiesConstraint>(rval);
  415. }
  416. static pConstraint propertiesDraft3(detail::ParserContext<A> const & context) {
  417. EXPECT(context.schema.type() == adapter::Type::Object);
  418. std::unordered_set<std::string> required;
  419. for (auto [prop, subschema] : context.schema.as_object()) {
  420. EXPECT(subschema.type() == adapter::Type::Object);
  421. if (auto sub = subschema.as_object();
  422. sub.contains("required") && sub["required"].as_boolean()) {
  423. required.insert(prop);
  424. }
  425. }
  426. if (required.empty()) {
  427. return properties(context);
  428. }
  429. return constraint::PolyConstraint::AllOf(
  430. properties(context), std::make_unique<constraint::RequiredConstraint>(std::move(required)));
  431. }
  432. static auto propertyNames(detail::ParserContext<A> const & context) {
  433. return std::make_unique<constraint::PropertyNamesConstraint>(context.node());
  434. }
  435. static auto unevaluatedProperties(detail::ParserContext<A> const & context) {
  436. return std::make_unique<constraint::UnevaluatedPropertiesConstraint>(context.node());
  437. }
  438. static auto additionalProperties(detail::ParserContext<A> const & context) {
  439. std::unordered_set<std::string> properties;
  440. std::vector<std::string> patterns;
  441. Object const & parent = *context.parent;
  442. if (parent.contains("properties")) {
  443. for (auto [key, _] : parent["properties"].as_object()) {
  444. properties.insert(key);
  445. }
  446. }
  447. if (parent.contains("patternProperties")) {
  448. for (auto [key, _] : parent["patternProperties"].as_object()) {
  449. patterns.push_back(key);
  450. }
  451. }
  452. using C = constraint::AdditionalPropertiesConstraint;
  453. if (context.vocab->version() < schema::Version::Draft06 &&
  454. context.schema.type() == adapter::Type::Boolean) {
  455. return std::make_unique<C>(context.always(), properties, patterns);
  456. }
  457. return std::make_unique<C>(context.node(), properties, patterns);
  458. }
  459. static auto dependencies(detail::ParserContext<A> const & context) {
  460. EXPECT(context.schema.type() == adapter::Type::Object);
  461. std::map<std::string, schema::Node const *> schemas;
  462. std::map<std::string, std::unordered_set<std::string>> required;
  463. for (auto [prop, subschema] : context.schema.as_object()) {
  464. if (subschema.type() == adapter::Type::Array) {
  465. for (auto key : subschema.as_array()) {
  466. EXPECT(key.type() == adapter::Type::String);
  467. required[prop].insert(key.as_string());
  468. }
  469. } else if (context.vocab->version() <= schema::Version::Draft03 &&
  470. subschema.type() == adapter::Type::String) {
  471. required[prop].insert(subschema.as_string());
  472. } else {
  473. schemas.emplace(prop, context.child(subschema, prop).node());
  474. }
  475. }
  476. return std::make_unique<constraint::DependenciesConstraint>(schemas, required);
  477. }
  478. static auto dependentSchemas(detail::ParserContext<A> const & context) {
  479. EXPECT(context.schema.type() == adapter::Type::Object);
  480. std::map<std::string, schema::Node const *> rval;
  481. for (auto [prop, subschema] : context.schema.as_object()) {
  482. rval.emplace(prop, context.child(subschema, prop).node());
  483. }
  484. return std::make_unique<constraint::DependenciesConstraint>(rval);
  485. }
  486. static auto dependentRequired(detail::ParserContext<A> const & context) {
  487. EXPECT(context.schema.type() == adapter::Type::Object);
  488. std::map<std::string, std::unordered_set<std::string>> rval;
  489. for (auto [prop, subschema] : context.schema.as_object()) {
  490. EXPECT(subschema.type() == adapter::Type::Array);
  491. for (auto key : subschema.as_array()) {
  492. EXPECT(key.type() == adapter::Type::String);
  493. rval[prop].insert(key.as_string());
  494. }
  495. }
  496. return std::make_unique<constraint::DependenciesConstraint>(rval);
  497. }
  498. };
  499. }