validation_visitor.h 33 KB

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