validation_visitor.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. #pragma once
  2. #include <algorithm>
  3. #include <tuple>
  4. #include <type_traits>
  5. #include <vector>
  6. #include <jvalidate/compat/enumerate.h>
  7. #include <jvalidate/constraint/array_constraint.h>
  8. #include <jvalidate/constraint/general_constraint.h>
  9. #include <jvalidate/constraint/number_constraint.h>
  10. #include <jvalidate/constraint/object_constraint.h>
  11. #include <jvalidate/constraint/string_constraint.h>
  12. #include <jvalidate/detail/expect.h>
  13. #include <jvalidate/detail/iostream.h>
  14. #include <jvalidate/detail/number.h>
  15. #include <jvalidate/detail/pointer.h>
  16. #include <jvalidate/detail/scoped_state.h>
  17. #include <jvalidate/detail/string_adapter.h>
  18. #include <jvalidate/forward.h>
  19. #include <jvalidate/schema.h>
  20. #include <jvalidate/status.h>
  21. #include <jvalidate/validation_config.h>
  22. #include <jvalidate/validation_result.h>
  23. #define VISITED(type) std::get<std::unordered_set<type>>(*visited_)
  24. #define VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(subschema, subinstance, path, local_visited, ...) \
  25. do { \
  26. Status const partial = \
  27. validate_subschema_on(subschema, subinstance, path __VA_OPT__(, ) __VA_ARGS__); \
  28. rval &= partial; \
  29. if (result_ and partial != Status::Noop) { \
  30. local_visited.insert(local_visited.end(), path); \
  31. } \
  32. } while (false)
  33. #define NOOP_UNLESS_TYPE(etype) RETURN_UNLESS(adapter::Type::etype == document.type(), Status::Noop)
  34. #define BREAK_EARLY_IF_NO_RESULT_TREE() \
  35. do { \
  36. if (rval == Status::Reject and not result_ and not visited_) { \
  37. break; \
  38. } \
  39. } while (false)
  40. namespace jvalidate {
  41. template <RegexEngine RE, typename ExtensionVisitor> class ValidationVisitor {
  42. private:
  43. JVALIDATE_TRIBOOL_TYPE(StoreResults, ForValid, ForInvalid, ForAnything);
  44. using VisitedAnnotation = std::tuple<std::unordered_set<size_t>, std::unordered_set<std::string>>;
  45. friend ExtensionVisitor;
  46. private:
  47. detail::Pointer where_;
  48. detail::Pointer schema_path_;
  49. schema::Node const * schema_;
  50. ValidationResult * result_;
  51. ValidationConfig const & cfg_;
  52. ExtensionVisitor extension_;
  53. RE & regex_;
  54. mutable VisitedAnnotation * visited_ = nullptr;
  55. mutable StoreResults tracking_ = StoreResults::ForInvalid;
  56. public:
  57. /**
  58. * @brief Construct a new ValidationVisitor
  59. *
  60. * @param schema The parsed JSON Schema
  61. * @param cfg General configuration settings for how the run is executed
  62. * @param regex A cache of string regular expressions to compiled
  63. * regular expressions
  64. * @param[optional] extension A special visitor for extension constraints.
  65. * @param[optional] result A cache of result/annotation info for the user to
  66. * receive a detailed summary of why a document is supported/unsupported.
  67. */
  68. ValidationVisitor(schema::Node const & schema, ValidationConfig const & cfg, RE & regex,
  69. ExtensionVisitor extension, ValidationResult * result)
  70. : schema_(&schema), result_(result), cfg_(cfg), extension_(extension), regex_(regex) {}
  71. Status visit(constraint::ExtensionConstraint const & cons, Adapter auto const & document) const {
  72. // Because we don't provide any contract constraint on our ExtensionVisitor,
  73. // we instead defer it to here where we validate that the extension can be
  74. // validated given the input document.
  75. // This covers a case where we write the extension around a specific adapter
  76. // instead of generically.
  77. if constexpr (std::is_invocable_r_v<Status, ExtensionVisitor, decltype(cons),
  78. decltype(document), ValidationVisitor const &>) {
  79. return extension_(cons, document, *this);
  80. }
  81. annotate("unsupported extension");
  82. return Status::Noop;
  83. }
  84. Status visit(constraint::TypeConstraint const & cons, Adapter auto const & document) const {
  85. adapter::Type const type = document.type();
  86. for (adapter::Type const accept : cons.types) {
  87. if (type == accept) { // Simple case, types are equal
  88. return result(Status::Accept, type, " is in types [", cons.types, "]");
  89. }
  90. if (accept == adapter::Type::Number && type == adapter::Type::Integer) {
  91. // Number is a super-type of Integer, therefore all Integer values are
  92. // accepted by a `"type": "number"` schema.
  93. return result(Status::Accept, type, " is in types [", cons.types, "]");
  94. }
  95. if (accept == adapter::Type::Integer && type == adapter::Type::Number &&
  96. detail::is_json_integer(document.as_number())) {
  97. // Since the JSON specification does not distinguish between Number
  98. // and Integer, but JSON Schema does, we need to check that the number
  99. // is a whole integer that is representable within the system (64-bit).
  100. return result(Status::Accept, type, " is in types [", cons.types, "]");
  101. }
  102. }
  103. return result(Status::Reject, type, " is not in types [", cons.types, "]");
  104. }
  105. Status visit(constraint::ConstConstraint const & cons, Adapter auto const & document) const {
  106. auto is_equal = [this, &document](auto const & frozen) {
  107. return document.equals(frozen, cfg_.strict_equality);
  108. };
  109. if (cons.value->apply(is_equal)) {
  110. return result(Status::Accept, "matches value");
  111. }
  112. return result(Status::Reject, cons.value, " was expected");
  113. }
  114. Status visit(constraint::EnumConstraint const & cons, Adapter auto const & document) const {
  115. auto is_equal = [this, &document](auto const & frozen) {
  116. return document.equals(frozen, cfg_.strict_equality);
  117. };
  118. for (auto const & [index, option] : detail::enumerate(cons.enumeration)) {
  119. if (option->apply(is_equal)) {
  120. return result(Status::Accept, index);
  121. }
  122. }
  123. return result(Status::Reject, document, " value is not one of ", cons.enumeration);
  124. }
  125. Status visit(constraint::AllOfConstraint const & cons, Adapter auto const & document) const {
  126. Status rval = Status::Accept;
  127. std::set<size_t> unmatched;
  128. for (auto const & [index, subschema] : detail::enumerate(cons.children)) {
  129. if (auto stat = validate_subschema(subschema, document, index); stat == Status::Reject) {
  130. rval = Status::Reject;
  131. unmatched.insert(index);
  132. }
  133. BREAK_EARLY_IF_NO_RESULT_TREE();
  134. }
  135. if (rval == Status::Reject) {
  136. return result(rval, "does not validate subschemas ", unmatched);
  137. }
  138. return result(rval, "validates all subschemas");
  139. }
  140. Status visit(constraint::AnyOfConstraint const & cons, Adapter auto const & document) const {
  141. std::optional<size_t> first_validated;
  142. for (auto const & [index, subschema] : detail::enumerate(cons.children)) {
  143. if (validate_subschema(subschema, document, index)) {
  144. // This technically will produce different results when we're tracking
  145. // visited nodes, but in practice it doesn't actually matter which
  146. // subschema index we record in the annotation.
  147. first_validated = index;
  148. }
  149. if (not visited_ && first_validated.has_value()) {
  150. break;
  151. }
  152. }
  153. if (first_validated.has_value()) {
  154. return result(Status::Accept, "validates subschema ", *first_validated);
  155. }
  156. return result(Status::Reject, "validates none of the subschemas");
  157. }
  158. Status visit(constraint::OneOfConstraint const & cons, Adapter auto const & document) const {
  159. std::set<size_t> matches;
  160. for (auto const & [index, subschema] : detail::enumerate(cons.children)) {
  161. scoped_state(tracking_, StoreResults::ForAnything);
  162. if (validate_subschema(subschema, document, index)) {
  163. matches.insert(index);
  164. }
  165. }
  166. if (matches.size() == 1) {
  167. return result(Status::Accept, "validates subschema ", *matches.begin());
  168. }
  169. return result(Status::Reject, "validates multiple subschemas ", matches);
  170. }
  171. Status visit(constraint::NotConstraint const & cons, Adapter auto const & document) const {
  172. scoped_state(visited_, nullptr);
  173. scoped_state(tracking_, !tracking_);
  174. bool const rejected = validate_subschema(cons.child, document) == Status::Reject;
  175. return rejected;
  176. }
  177. Status visit(constraint::ConditionalConstraint const & cons,
  178. Adapter auto const & document) const {
  179. Status const if_true = [this, &cons, &document]() {
  180. scoped_state(tracking_, StoreResults::ForAnything);
  181. return validate_subschema(cons.if_constraint, document);
  182. }();
  183. annotate(if_true ? "valid" : "invalid");
  184. if (if_true) {
  185. return validate_subschema(cons.then_constraint, document, detail::parent, "then");
  186. }
  187. return validate_subschema(cons.else_constraint, document, detail::parent, "else");
  188. }
  189. Status visit(constraint::MaximumConstraint const & cons, Adapter auto const & document) const {
  190. switch (document.type()) {
  191. case adapter::Type::Integer:
  192. if (int64_t value = document.as_integer(); not cons(value)) {
  193. return result(Status::Reject, value, cons.exclusive ? " >= " : " > ", cons.value);
  194. } else {
  195. return result(Status::Accept, value, cons.exclusive ? " < " : " <= ", cons.value);
  196. }
  197. case adapter::Type::Number:
  198. if (double value = document.as_number(); not cons(value)) {
  199. return result(Status::Reject, value, cons.exclusive ? " >= " : " > ", cons.value);
  200. } else {
  201. return result(Status::Accept, value, cons.exclusive ? " < " : " <= ", cons.value);
  202. }
  203. default:
  204. return Status::Noop;
  205. }
  206. }
  207. Status visit(constraint::MinimumConstraint const & cons, Adapter auto const & document) const {
  208. switch (document.type()) {
  209. case adapter::Type::Integer:
  210. if (int64_t value = document.as_integer(); not cons(value)) {
  211. return result(Status::Reject, value, cons.exclusive ? " <= " : " < ", cons.value);
  212. } else {
  213. return result(Status::Accept, value, cons.exclusive ? " > " : " >= ", cons.value);
  214. }
  215. case adapter::Type::Number:
  216. if (double value = document.as_number(); not cons(value)) {
  217. return result(Status::Reject, value, cons.exclusive ? " <= " : " < ", cons.value);
  218. } else {
  219. return result(Status::Accept, value, cons.exclusive ? " > " : " >= ", cons.value);
  220. }
  221. default:
  222. return Status::Noop;
  223. }
  224. }
  225. Status visit(constraint::MultipleOfConstraint const & cons, Adapter auto const & document) const {
  226. adapter::Type const type = document.type();
  227. RETURN_UNLESS(type == adapter::Type::Number || type == adapter::Type::Integer, Status::Noop);
  228. if (double value = document.as_number(); not cons(value)) {
  229. return result(Status::Reject, value, " is not a multiple of ", cons.value);
  230. } else {
  231. return result(Status::Accept, value, " is a multiple of ", cons.value);
  232. }
  233. }
  234. Status visit(constraint::MaxLengthConstraint const & cons, Adapter auto const & document) const {
  235. NOOP_UNLESS_TYPE(String);
  236. std::string const str = document.as_string();
  237. if (int64_t len = detail::length(str); len > cons.value) {
  238. return result(Status::Reject, "string of length ", len, " is >", cons.value);
  239. } else {
  240. return result(Status::Accept, "string of length ", len, " is <=", cons.value);
  241. }
  242. }
  243. Status visit(constraint::MinLengthConstraint const & cons, Adapter auto const & document) const {
  244. NOOP_UNLESS_TYPE(String);
  245. std::string const str = document.as_string();
  246. if (int64_t len = detail::length(str); len < cons.value) {
  247. return result(Status::Reject, "string of length ", len, " is <", cons.value);
  248. } else {
  249. return result(Status::Accept, "string of length ", len, " is >=", cons.value);
  250. }
  251. }
  252. Status visit(constraint::PatternConstraint const & cons, Adapter auto const & document) const {
  253. NOOP_UNLESS_TYPE(String);
  254. std::string const str = document.as_string();
  255. annotate(regex_.engine_name());
  256. if (regex_.search(cons.regex, str)) {
  257. return result(Status::Accept, "string matches pattern /", cons.regex, "/");
  258. }
  259. return result(Status::Reject, "string does not match pattern /", cons.regex, "/");
  260. }
  261. Status visit(constraint::FormatConstraint const & cons, Adapter auto const & document) const {
  262. // https://json-schema.org/draft/2020-12/json-schema-validation#name-defined-formats
  263. NOOP_UNLESS_TYPE(String);
  264. annotate(cons.format);
  265. if (not cfg_.validate_format && not cons.is_assertion) {
  266. // Don't both validating formats if we're not in assertion mode
  267. // Assertion mode is specified either by using the appropriate "$vocab"
  268. // meta-schema or by requesting it in the ValidationConfig.
  269. return true; // TODO: I think this can be made into Noop
  270. }
  271. return result(Status::Reject, " is unimplemented");
  272. }
  273. Status visit(constraint::AdditionalItemsConstraint const & cons,
  274. Adapter auto const & document) const {
  275. NOOP_UNLESS_TYPE(Array);
  276. auto array = document.as_array();
  277. Status rval = Status::Accept;
  278. std::vector<size_t> items;
  279. for (size_t i = cons.applies_after_nth; i < array.size(); ++i) {
  280. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(cons.subschema, array[i], i, items);
  281. BREAK_EARLY_IF_NO_RESULT_TREE();
  282. }
  283. annotate_list(items);
  284. return rval;
  285. }
  286. Status visit(constraint::ContainsConstraint const & cons, Adapter auto const & document) const {
  287. NOOP_UNLESS_TYPE(Array);
  288. auto array = document.as_array();
  289. size_t const minimum = cons.minimum.value_or(1);
  290. size_t const maximum = cons.maximum.value_or(array.size());
  291. size_t matches = 0;
  292. for (size_t i = 0; i < array.size(); ++i) {
  293. if (validate_subschema_on(cons.subschema, array[i], i)) {
  294. ++matches;
  295. }
  296. }
  297. if (matches < minimum) {
  298. return result(Status::Reject, "array contains <", minimum, " matching items");
  299. }
  300. if (matches > maximum) {
  301. return result(Status::Reject, "array contains >", maximum, " matching items");
  302. }
  303. return result(Status::Accept, "array contains ", matches, " matching items");
  304. }
  305. Status visit(constraint::MaxItemsConstraint const & cons, Adapter auto const & document) const {
  306. NOOP_UNLESS_TYPE(Array);
  307. if (size_t size = document.array_size(); size > cons.value) {
  308. return result(Status::Reject, "array of size ", size, " is >", cons.value);
  309. } else {
  310. return result(Status::Accept, "array of size ", size, " is <=", cons.value);
  311. }
  312. }
  313. Status visit(constraint::MinItemsConstraint const & cons, Adapter auto const & document) const {
  314. NOOP_UNLESS_TYPE(Array);
  315. if (size_t size = document.array_size(); size < cons.value) {
  316. return result(Status::Reject, "array of size ", size, " is <", cons.value);
  317. } else {
  318. return result(Status::Accept, "array of size ", size, " is >=", cons.value);
  319. }
  320. }
  321. Status visit(constraint::TupleConstraint const & cons, Adapter auto const & document) const {
  322. NOOP_UNLESS_TYPE(Array);
  323. Status rval = Status::Accept;
  324. std::vector<size_t> items;
  325. for (auto const & [index, item] : detail::enumerate(document.as_array())) {
  326. if (index >= cons.items.size()) {
  327. break;
  328. }
  329. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(cons.items[index], item, index, items);
  330. BREAK_EARLY_IF_NO_RESULT_TREE();
  331. }
  332. annotate_list(items);
  333. return rval;
  334. }
  335. template <Adapter A>
  336. Status visit(constraint::UniqueItemsConstraint const & cons, A const & document) const {
  337. NOOP_UNLESS_TYPE(Array);
  338. if constexpr (std::totally_ordered<A>) {
  339. // If the adapter defines comparison operators, then it becomes possible
  340. // to compute uniqueness in O(n*log(n)) checks.
  341. std::map<A, size_t> cache;
  342. for (auto const & [index, elem] : detail::enumerate(document.as_array())) {
  343. if (auto [it, created] = cache.emplace(elem, index); not created) {
  344. return result(Status::Reject, "items ", it->second, " and ", index, " are equal");
  345. }
  346. }
  347. } else {
  348. // Otherwise, we need to run an O(n^2) triangular array search comparing
  349. // equality for each element. This still guarantees that each element is
  350. // compared against each other element no more than once.
  351. auto array = document.as_array();
  352. for (size_t i = 0; i < array.size(); ++i) {
  353. for (size_t j = i + 1; j < array.size(); ++j) {
  354. if (array[i].equals(array[j], true)) {
  355. return result(Status::Reject, "items ", i, " and ", j, " are equal");
  356. }
  357. }
  358. }
  359. }
  360. return result(Status::Accept, "all array items are unique");
  361. }
  362. Status visit(constraint::AdditionalPropertiesConstraint const & cons,
  363. Adapter auto const & document) const {
  364. NOOP_UNLESS_TYPE(Object);
  365. auto matches_any_pattern = [this, &cons](std::string const & key) {
  366. return std::ranges::any_of(cons.patterns, [this, &key](auto const & pattern) {
  367. return regex_.search(pattern, key);
  368. });
  369. };
  370. Status rval = Status::Accept;
  371. std::vector<std::string> properties;
  372. for (auto const & [key, elem] : document.as_object()) {
  373. if (not cons.properties.contains(key) && not matches_any_pattern(key)) {
  374. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(cons.subschema, elem, key, properties);
  375. }
  376. BREAK_EARLY_IF_NO_RESULT_TREE();
  377. }
  378. annotate_list(properties);
  379. return rval;
  380. }
  381. Status visit(constraint::DependenciesConstraint const & cons,
  382. Adapter auto const & document) const {
  383. NOOP_UNLESS_TYPE(Object);
  384. auto object = document.as_object();
  385. Status rval = Status::Accept;
  386. for (auto const & [key, subschema] : cons.subschemas) {
  387. if (not object.contains(key)) {
  388. continue;
  389. }
  390. rval &= validate_subschema(subschema, document, key);
  391. BREAK_EARLY_IF_NO_RESULT_TREE();
  392. }
  393. for (auto [key, required] : cons.required) {
  394. if (not object.contains(key)) {
  395. continue;
  396. }
  397. for (auto const & [key, _] : object) {
  398. required.erase(key);
  399. }
  400. rval &= required.empty();
  401. BREAK_EARLY_IF_NO_RESULT_TREE();
  402. }
  403. return rval;
  404. }
  405. Status visit(constraint::MaxPropertiesConstraint const & cons,
  406. Adapter auto const & document) const {
  407. NOOP_UNLESS_TYPE(Object);
  408. if (size_t size = document.object_size(); size > cons.value) {
  409. return result(Status::Reject, "object of size ", size, " is >", cons.value);
  410. } else {
  411. return result(Status::Accept, "object of size ", size, " is <=", cons.value);
  412. }
  413. }
  414. Status visit(constraint::MinPropertiesConstraint const & cons,
  415. Adapter auto const & document) const {
  416. NOOP_UNLESS_TYPE(Object);
  417. if (size_t size = document.object_size(); size < cons.value) {
  418. return result(Status::Reject, "object of size ", size, " is <", cons.value);
  419. } else {
  420. return result(Status::Accept, "object of size ", size, " is >=", cons.value);
  421. }
  422. }
  423. Status visit(constraint::PatternPropertiesConstraint const & cons,
  424. Adapter auto const & document) const {
  425. NOOP_UNLESS_TYPE(Object);
  426. std::vector<std::string> properties;
  427. Status rval = Status::Accept;
  428. for (auto const & [pattern, subschema] : cons.properties) {
  429. for (auto const & [key, elem] : document.as_object()) {
  430. if (not regex_.search(pattern, key)) {
  431. continue;
  432. }
  433. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(subschema, elem, key, properties);
  434. BREAK_EARLY_IF_NO_RESULT_TREE();
  435. }
  436. }
  437. annotate_list(properties);
  438. return rval;
  439. }
  440. template <Adapter A>
  441. Status visit(constraint::PropertiesConstraint const & cons, A const & document) const {
  442. NOOP_UNLESS_TYPE(Object);
  443. Status rval = Status::Accept;
  444. auto object = document.as_object();
  445. if constexpr (MutableAdapter<A>) {
  446. // Special Rule - if the adapter is of a mutable json document (wraps a
  447. // non-const reference and exposes the assign function) we will process
  448. // the "default" annotation will be applied.
  449. // https://json-schema.org/draft/2020-12/json-schema-validation#section-9.2
  450. //
  451. // Although the JSON Schema draft only says the the default value ought be
  452. // valid against the schema, this implementation will assure that it is
  453. // valid against this PropertiesConstraint, and any other constraints that
  454. // are run after this one.
  455. for (auto const & [key, subschema] : cons.properties) {
  456. auto const * default_value = subschema->default_value();
  457. if (default_value && not object.contains(key)) {
  458. object.assign(key, *default_value);
  459. }
  460. }
  461. }
  462. std::vector<std::string> properties;
  463. for (auto const & [key, elem] : object) {
  464. if (auto it = cons.properties.find(key); it != cons.properties.end()) {
  465. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(it->second, elem, key, properties, key);
  466. }
  467. BREAK_EARLY_IF_NO_RESULT_TREE();
  468. }
  469. annotate_list(properties);
  470. return rval;
  471. }
  472. template <Adapter A>
  473. Status visit(constraint::PropertyNamesConstraint const & cons, A const & document) const {
  474. NOOP_UNLESS_TYPE(Object);
  475. Status rval = Status::Accept;
  476. for (auto const & [key, _] : document.as_object()) {
  477. rval &=
  478. validate_subschema_on(cons.key_schema, detail::StringAdapter(key), std::string("$$key"));
  479. }
  480. return rval;
  481. }
  482. Status visit(constraint::RequiredConstraint const & cons, Adapter auto const & document) const {
  483. NOOP_UNLESS_TYPE(Object);
  484. auto required = cons.properties;
  485. for (auto const & [key, _] : document.as_object()) {
  486. required.erase(key);
  487. }
  488. if (required.empty()) {
  489. return result(Status::Accept, "contains all required properties ", cons.properties);
  490. }
  491. return result(Status::Reject, "missing required properties ", required);
  492. }
  493. Status visit(constraint::UnevaluatedItemsConstraint const & cons,
  494. Adapter auto const & document) const {
  495. NOOP_UNLESS_TYPE(Array);
  496. if (not visited_) {
  497. return Status::Reject;
  498. }
  499. Status rval = Status::Accept;
  500. std::vector<size_t> items;
  501. for (auto const & [index, item] : detail::enumerate(document.as_array())) {
  502. if (not VISITED(size_t).contains(index)) {
  503. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(cons.subschema, item, index, items);
  504. }
  505. BREAK_EARLY_IF_NO_RESULT_TREE();
  506. }
  507. annotate_list(items);
  508. return rval;
  509. }
  510. Status visit(constraint::UnevaluatedPropertiesConstraint const & cons,
  511. Adapter auto const & document) const {
  512. NOOP_UNLESS_TYPE(Object);
  513. if (not visited_) {
  514. return Status::Reject;
  515. }
  516. Status rval = Status::Accept;
  517. std::vector<std::string> properties;
  518. for (auto const & [key, elem] : document.as_object()) {
  519. if (not VISITED(std::string).contains(key)) {
  520. VALIDATE_SUBSCHEMA_AND_MARK_LOCAL_VISIT(cons.subschema, elem, key, properties);
  521. }
  522. BREAK_EARLY_IF_NO_RESULT_TREE();
  523. }
  524. annotate_list(properties);
  525. return rval;
  526. }
  527. /**
  528. * @brief The main entry point into the validator. Validates the provided
  529. * document according to the schema.
  530. */
  531. Status validate(Adapter auto const & document) {
  532. // Step 1) Check if this is an always-false schema. Sometimes, this will
  533. // have a custom message.
  534. if (std::optional<std::string> const & reject = schema_->rejects_all()) {
  535. if (should_annotate(Status::Reject)) {
  536. // This will only be run if we are interested in why something is
  537. // rejected. For example - `{ "not": false }` doesn't produce a
  538. // meaningful annotation...
  539. result_->error(where_, schema_path_, "", *reject);
  540. }
  541. // ...We do always record the result if a result object is present.
  542. (result_ ? result_->valid(where_, schema_path_, false) : void());
  543. return Status::Reject;
  544. }
  545. if (schema_->accepts_all()) {
  546. // An accept-all schema is not No-Op for the purpose of unevaluated*
  547. (result_ ? result_->valid(where_, schema_path_, true) : void());
  548. return Status::Accept;
  549. }
  550. // Begin tracking evaluations for unevaluated* keywords. The annotation
  551. // object is passed down from parent visitor to child visitor to allow all
  552. // schemas to mark whether they visited a certain item or property.
  553. VisitedAnnotation annotate;
  554. if (schema_->requires_result_context() and not visited_) {
  555. visited_ = &annotate;
  556. }
  557. Status rval = Status::Noop;
  558. // Before Draft2019_09, reference schemas could not coexist with other
  559. // constraints. This is enforced in the parsing of the schema, rather than
  560. // during validation {@see jvalidate::schema::Node::construct}.
  561. if (std::optional<schema::Node const *> ref = schema_->reference_schema()) {
  562. // TODO: Investigate why this seems to produce .../$ref/$ref pointers
  563. rval = validate_subschema(*ref, document, "$ref");
  564. }
  565. if (result_ && !schema_->description().empty()) {
  566. result_->annotate(where_, schema_path_, "description", schema_->description());
  567. }
  568. detail::Pointer const current_schema = schema_path_;
  569. for (auto const & [key, p_constraint] : schema_->constraints()) {
  570. BREAK_EARLY_IF_NO_RESULT_TREE();
  571. schema_path_ = current_schema / key;
  572. rval &= std::visit([this, &document](auto & c) { return this->visit(c, document); },
  573. *p_constraint);
  574. }
  575. // Post Constraints represent the unevaluatedItems and unevaluatedProperties
  576. // keywords.
  577. for (auto const & [key, p_constraint] : schema_->post_constraints()) {
  578. BREAK_EARLY_IF_NO_RESULT_TREE();
  579. schema_path_ = current_schema / key;
  580. rval &= std::visit([this, &document](auto & c) { return this->visit(c, document); },
  581. *p_constraint);
  582. }
  583. (result_ ? result_->valid(where_, current_schema, static_cast<bool>(rval)) : void());
  584. return rval;
  585. }
  586. private:
  587. template <typename S>
  588. requires(std::is_constructible_v<std::string, S>)
  589. // Optimization to avoid running string-like objects through a
  590. // std::stringstream in fmtlist.
  591. static std::string fmt(S const & str) {
  592. return std::string(str);
  593. }
  594. // Format va_args into a single string to annotate or mark an error message
  595. static std::string fmt(auto const &... args) {
  596. std::stringstream ss;
  597. using ::jvalidate::operator<<;
  598. [[maybe_unused]] int _[] = {(ss << args, 0)...};
  599. return ss.str();
  600. }
  601. // Format an iterable argument into a vector of strings to annotate or mark
  602. // an error.
  603. static std::vector<std::string> fmtlist(auto const & arg) {
  604. std::vector<std::string> strs;
  605. for (auto const & elem : arg) {
  606. strs.push_back(fmt(elem));
  607. }
  608. return strs;
  609. }
  610. bool should_annotate(Status stat) const {
  611. if (not result_) {
  612. return false;
  613. }
  614. switch (*tracking_) {
  615. case StoreResults::ForAnything:
  616. return stat != Status::Noop;
  617. case StoreResults::ForValid:
  618. return stat == Status::Accept;
  619. case StoreResults::ForInvalid:
  620. return stat == Status::Reject;
  621. }
  622. }
  623. #define ANNOTATION_HELPER(name, ADD, FMT) \
  624. void name(auto const &... args) const { \
  625. if (not result_) { \
  626. /* do nothing if there's no result object to append to */ \
  627. } else if (schema_path_.empty()) { \
  628. result_->ADD(where_, schema_path_, "", FMT(args...)); \
  629. } else { \
  630. result_->ADD(where_, schema_path_.parent(), schema_path_.back(), FMT(args...)); \
  631. } \
  632. }
  633. ANNOTATION_HELPER(error, error, fmt)
  634. ANNOTATION_HELPER(annotate, annotate, fmt)
  635. ANNOTATION_HELPER(annotate_list, annotate, fmtlist)
  636. Status result(Status stat, auto const &... args) const {
  637. return (should_annotate(stat) ? error(args...) : void(), stat);
  638. }
  639. /**
  640. * @brief Walking function for entering a subschema.
  641. *
  642. * @param subschema The "subschema" being validated. This is either another
  643. * schema object (jvalidate::schema::Node), or a constraint.
  644. * @param keys... The path to this subschema, relative to the current schema
  645. * evaluation.
  646. *
  647. * @return The status of validating the current instance against the
  648. * subschema.
  649. */
  650. template <typename... K>
  651. Status validate_subschema(constraint::SubConstraint const & subschema,
  652. Adapter auto const & document, K const &... keys) const {
  653. if (schema::Node const * const * ppschema = std::get_if<0>(&subschema)) {
  654. return validate_subschema(*ppschema, document, keys...);
  655. } else {
  656. return std::visit([this, &document](auto & c) { return this->visit(c, document); },
  657. *std::get<1>(subschema));
  658. }
  659. }
  660. /**
  661. * @brief Walking function for entering a subschema. Creates a new validation
  662. * visitor in order to continue evaluation.
  663. *
  664. * @param subschema The subschema being validated.
  665. * @param keys... The path to this subschema, relative to the current schema
  666. * evaluation.
  667. *
  668. * @return The status of validating the current instance against the
  669. * subschema.
  670. */
  671. template <typename... K>
  672. Status validate_subschema(schema::Node const * subschema, Adapter auto const & document,
  673. K const &... keys) const {
  674. VisitedAnnotation annotate;
  675. ValidationVisitor next = *this;
  676. ((next.schema_path_ /= keys), ...);
  677. std::tie(next.schema_, next.visited_) =
  678. std::forward_as_tuple(subschema, visited_ ? &annotate : nullptr);
  679. Status rval = next.validate(document);
  680. // Only update the visited annotation of the current context if the
  681. // subschema validates as Accepted.
  682. if (rval == Status::Accept and visited_) {
  683. std::get<0>(*visited_).merge(std::get<0>(annotate));
  684. std::get<1>(*visited_).merge(std::get<1>(annotate));
  685. }
  686. return rval;
  687. }
  688. /**
  689. * @brief Walking function for entering a subschema and child document.
  690. * Creates a new validation visitor in order to continue evaluation.
  691. *
  692. * @param subschema The subschema being validated.
  693. * @param document The child document being evaluated.
  694. * @param key The path to this document instance.
  695. * @param schema_keys... The path to this subschema, relative to the current
  696. * schema evaluation.
  697. *
  698. * @return The status of validating the current instance against the
  699. * subschema.
  700. */
  701. template <typename K>
  702. Status validate_subschema_on(schema::Node const * subschema, Adapter auto const & document,
  703. K const & key, auto const &... schema_keys) const {
  704. ValidationResult result;
  705. ValidationVisitor next = *this;
  706. next.where_ /= key;
  707. ((next.schema_path_ /= schema_keys), ...);
  708. std::tie(next.schema_, next.result_, next.visited_) =
  709. std::forward_as_tuple(subschema, result_ ? &result : nullptr, nullptr);
  710. Status rval = next.validate(document);
  711. // Only update the visited annotation of the current context if the
  712. // subschema validates as Accepted.
  713. if (rval == Status::Accept and visited_) {
  714. VISITED(K).insert(key);
  715. }
  716. // Update the annotation/error content only if a failure is being reported,
  717. // or if we are in an "if" schema.
  718. if ((rval == Status::Reject or tracking_ == StoreResults::ForAnything) and result_) {
  719. result_->merge(std::move(result));
  720. }
  721. return rval;
  722. }
  723. };
  724. }