constraint.h 23 KB

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