annotation_test.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include <gmock/gmock.h>
  2. #include <gtest/gtest.h>
  3. #include <jvalidate/adapter.h>
  4. #include <jvalidate/adapters/jsoncpp.h>
  5. #include <jvalidate/enum.h>
  6. #include <jvalidate/schema.h>
  7. #include <jvalidate/status.h>
  8. #include <jvalidate/uri.h>
  9. #include <jvalidate/validation_result.h>
  10. #include <jvalidate/validator.h>
  11. #include <sstream>
  12. #include "matchers.h"
  13. using enum jvalidate::schema::Version;
  14. using testing::Not;
  15. static auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
  16. jvalidate::schema::Version version = Draft2020_12) {
  17. jvalidate::Schema const schema(schema_doc, version);
  18. jvalidate::ValidationResult result;
  19. (void)jvalidate::Validator(schema).validate(instance_doc, &result);
  20. return result;
  21. }
  22. TEST(AnnotationTest, AttachesFormattingAnnotation) {
  23. auto const schema = R"({
  24. "format": "uri"
  25. })"_json;
  26. auto const instance = R"("http://json-schema.org")"_json;
  27. jvalidate::ValidationResult result = validate(schema, instance);
  28. EXPECT_THAT(result, AnnotationAt("format", "uri"));
  29. }
  30. TEST(AnnotationTest, AnnotatesAtRootSafely) {
  31. auto const schema = R"(false)"_json;
  32. auto const instance = R"(4)"_json;
  33. jvalidate::ValidationResult result = validate(schema, instance);
  34. EXPECT_THAT(result, ErrorAt("", "always false"));
  35. }
  36. TEST(AnnotationTest, AnnotatesErrors) {
  37. auto const schema = R"({
  38. "minimum": 5
  39. })"_json;
  40. auto const instance = R"(4)"_json;
  41. jvalidate::ValidationResult result = validate(schema, instance);
  42. EXPECT_THAT(result, ErrorAt("minimum", "4 < 5"));
  43. }
  44. TEST(AnnotationTest, AnnotatesDescription) {
  45. auto const schema = R"({
  46. "description": "lorem ipsum",
  47. "minimum": 5
  48. })"_json;
  49. auto const instance = R"(4)"_json;
  50. jvalidate::ValidationResult result = validate(schema, instance);
  51. EXPECT_THAT(result, AnnotationAt("description", "lorem ipsum"));
  52. }
  53. TEST(AnnotationTest, CanRecordAnnotationsAsList) {
  54. auto const schema = R"({
  55. "items": false
  56. })"_json;
  57. auto const instance = R"([ 0, 1 ])"_json;
  58. jvalidate::ValidationResult result = validate(schema, instance);
  59. EXPECT_THAT(result, AnnotationAt("items", std::vector<std::string>{"0", "1"}));
  60. }
  61. TEST(AnnotationTest, OnlyErrors) {
  62. auto const schema = R"({
  63. "items": false
  64. })"_json;
  65. auto const instance = R"([ 0, 1 ])"_json;
  66. jvalidate::ValidationResult result = validate(schema, instance).only_errors();
  67. EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, ""_jptr)));
  68. }
  69. TEST(AnnotationTest, DoesNotAnnotatesValid) {
  70. auto const schema = R"({
  71. "minimum": 5
  72. })"_json;
  73. auto const instance = R"(6)"_json;
  74. jvalidate::ValidationResult result = validate(schema, instance);
  75. EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
  76. }
  77. TEST(AnnotationTest, NotSchemaFlipsAnnotationRule) {
  78. auto const schema = R"({
  79. "not": { "minimum": 5 }
  80. })"_json;
  81. auto const instance = R"(6)"_json;
  82. jvalidate::ValidationResult result = validate(schema, instance);
  83. EXPECT_THAT(result, ErrorAt(""_jptr, "/not"_jptr, "minimum", "6 >= 5"));
  84. }
  85. TEST(AnnotationTest, NotSchemaPropogatesDeeply) {
  86. auto const schema = R"({
  87. "not": {
  88. "properties": {
  89. "A": {
  90. "enum": [ 1, 3, 4 ]
  91. }
  92. }
  93. }
  94. })"_json;
  95. auto const instance = R"({"A": 3})"_json;
  96. jvalidate::ValidationResult result = validate(schema, instance);
  97. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/not/properties/A"_jptr, "enum", "1"));
  98. }
  99. TEST(AnnotationTest, PathFollowsSchemaNotConstraintModel) {
  100. auto const schema = R"({
  101. "$comment": "disallow is implemented in the form of NotConstraint[TypeConstraint]",
  102. "disallow": "string"
  103. })"_json;
  104. auto const instance = R"("hello")"_json;
  105. jvalidate::ValidationResult result = validate(schema, instance, Draft03);
  106. EXPECT_THAT(result, ErrorAt("disallow", "string is in types [string]"));
  107. }
  108. TEST(AnnotationTest, AttachesAlwaysFalseSensibly) {
  109. auto const schema = R"({
  110. "properties": {
  111. "A": false
  112. }
  113. })"_json;
  114. auto const instance = R"({
  115. "A": null
  116. })"_json;
  117. jvalidate::ValidationResult result = validate(schema, instance);
  118. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
  119. }
  120. TEST(AnnotationTest, IfThenCanGiveABecauseReason) {
  121. auto const schema = R"({
  122. "if": {
  123. "properties": {
  124. "A": { "const": true }
  125. },
  126. "required": [ "A" ]
  127. },
  128. "then": {
  129. "properties": {
  130. "B": { "multipleOf": 3 }
  131. }
  132. },
  133. "else": {
  134. "properties": {
  135. "B": { "multipleOf": 5 }
  136. }
  137. }
  138. })"_json;
  139. auto const instance = R"({
  140. "A": true,
  141. "B": 4
  142. })"_json;
  143. jvalidate::ValidationResult result = validate(schema, instance);
  144. EXPECT_THAT(result,
  145. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  146. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "matches value"));
  147. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/then/properties/B"_jptr, "multipleOf",
  148. "4 is not a multiple of 3"));
  149. }
  150. TEST(AnnotationTest, IfElseCanGiveABecauseReason) {
  151. auto const schema = R"({
  152. "if": {
  153. "properties": {
  154. "A": { "const": true }
  155. },
  156. "required": [ "A" ]
  157. },
  158. "then": {
  159. "properties": {
  160. "B": { "multipleOf": 3 }
  161. }
  162. },
  163. "else": {
  164. "properties": {
  165. "B": { "multipleOf": 5 }
  166. }
  167. }
  168. })"_json;
  169. auto const instance = R"({
  170. "A": false,
  171. "B": 4
  172. })"_json;
  173. jvalidate::ValidationResult result = validate(schema, instance);
  174. EXPECT_THAT(result,
  175. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  176. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "true was expected"));
  177. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/else/properties/B"_jptr, "multipleOf",
  178. "4 is not a multiple of 5"));
  179. }
  180. TEST(AnnotationTest, OneOfGivesAllReasonsOnTooMany) {
  181. auto const schema = R"({
  182. "oneOf": [
  183. { "minimum": 5 },
  184. { "multipleOf": 3 },
  185. { "maximum": 2 }
  186. ]
  187. })"_json;
  188. auto const instance = R"(6)"_json;
  189. jvalidate::ValidationResult result = validate(schema, instance);
  190. EXPECT_THAT(result, ErrorAt(""_jptr, ""_jptr, "oneOf", "validates multiple subschemas [ 0, 1 ]"));
  191. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/0"_jptr, "minimum", "6 >= 5"));
  192. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "6 is a multiple of 3"));
  193. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "6 > 2"));
  194. }
  195. TEST(AnnotationTest, OneOfGivesAllReasonsOnNoMatches) {
  196. auto const schema = R"({
  197. "oneOf": [
  198. { "minimum": 5 },
  199. { "multipleOf": 3 },
  200. { "maximum": 2 }
  201. ]
  202. })"_json;
  203. auto const instance = R"(4)"_json;
  204. jvalidate::ValidationResult result = validate(schema, instance);
  205. EXPECT_THAT(result, ErrorAt(""_jptr, ""_jptr, "oneOf", "validates no subschemas"));
  206. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/0"_jptr, "minimum", "4 < 5"));
  207. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "4 is not a multiple of 3"));
  208. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "4 > 2"));
  209. }
  210. TEST(AnnotationTest, OneOfGivesAllReasonsOnMatch) {
  211. auto const schema = R"({
  212. "oneOf": [
  213. { "minimum": 5 },
  214. { "multipleOf": 3 },
  215. { "maximum": 2 }
  216. ]
  217. })"_json;
  218. auto const instance = R"(3)"_json;
  219. jvalidate::ValidationResult result = validate(schema, instance);
  220. EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, ""_jptr)));
  221. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "3 is a multiple of 3"));
  222. }
  223. TEST(ValidationResultJsonTest, OutputsFormattedJSON) {
  224. auto const schema = R"({
  225. "items": false
  226. })"_json;
  227. auto const instance = R"([ 0, 1 ])"_json;
  228. jvalidate::ValidationResult result = validate(schema, instance);
  229. std::stringstream ss;
  230. ss << result;
  231. EXPECT_THAT(ss.str(), R"({
  232. "valid": false,
  233. "details": [
  234. {
  235. "valid": false,
  236. "evaluationPath": "",
  237. "instanceLocation": "",
  238. "annotations": {
  239. "items": [
  240. "0",
  241. "1"
  242. ]
  243. }
  244. },
  245. {
  246. "valid": false,
  247. "evaluationPath": "/items",
  248. "instanceLocation": "/0",
  249. "errors": {
  250. "": "always false"
  251. }
  252. },
  253. {
  254. "valid": false,
  255. "evaluationPath": "/items",
  256. "instanceLocation": "/1",
  257. "errors": {
  258. "": "always false"
  259. }
  260. }
  261. ]
  262. })");
  263. }
  264. #if !defined(JVALIDATE_MONOTEST)
  265. int main(int argc, char ** argv) {
  266. testing::InitGoogleMock(&argc, argv);
  267. return RUN_ALL_TESTS();
  268. }
  269. #endif