validation_visitor.h 33 KB

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