annotation_test.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "matchers.h"
  12. using enum jvalidate::schema::Version;
  13. using testing::Not;
  14. auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
  15. jvalidate::schema::Version version = Draft2020_12) {
  16. jvalidate::Schema const schema(schema_doc, version);
  17. jvalidate::ValidationResult result;
  18. (void)jvalidate::Validator(schema).validate(instance_doc, &result);
  19. return result;
  20. }
  21. TEST(Annotation, AttachesFormattingAnnotation) {
  22. auto const schema = R"({
  23. "format": "uri"
  24. })"_json;
  25. auto const instance = R"("http://json-schema.org")"_json;
  26. jvalidate::ValidationResult result = validate(schema, instance);
  27. EXPECT_THAT(result, AnnotationAt("format", "uri"));
  28. }
  29. TEST(Annotation, AnnotatesErrors) {
  30. auto const schema = R"({
  31. "minimum": 5
  32. })"_json;
  33. auto const instance = R"(4)"_json;
  34. jvalidate::ValidationResult result = validate(schema, instance);
  35. EXPECT_THAT(result, ErrorAt("minimum", "4 < 5"));
  36. }
  37. TEST(Annotation, DoesNotAnnotatesValid) {
  38. auto const schema = R"({
  39. "minimum": 5
  40. })"_json;
  41. auto const instance = R"(6)"_json;
  42. jvalidate::ValidationResult result = validate(schema, instance);
  43. EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
  44. }
  45. TEST(Annotation, NotSchemaFlipsAnnotationRule) {
  46. auto const schema = R"({
  47. "not": { "minimum": 5 }
  48. })"_json;
  49. auto const instance = R"(6)"_json;
  50. jvalidate::ValidationResult result = validate(schema, instance);
  51. EXPECT_THAT(result, ErrorAt(""_jptr, "/not"_jptr, "minimum", "6 >= 5"));
  52. }
  53. TEST(Annotation, NotSchemaPropogatesDeeply) {
  54. auto const schema = R"({
  55. "not": {
  56. "properties": {
  57. "A": {
  58. "enum": [ 1, 3, 4 ]
  59. }
  60. }
  61. }
  62. })"_json;
  63. auto const instance = R"({"A": 3})"_json;
  64. jvalidate::ValidationResult result = validate(schema, instance);
  65. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/not/properties/A"_jptr, "enum", "1"));
  66. }
  67. TEST(Annotation, PathFollowsSchemaNotConstraintModel) {
  68. auto const schema = R"({
  69. "$comment": "disallow is implemented in the form of NotConstraint[TypeConstraint]",
  70. "disallow": "string"
  71. })"_json;
  72. auto const instance = R"("hello")"_json;
  73. jvalidate::ValidationResult result = validate(schema, instance, Draft03);
  74. EXPECT_THAT(result, ErrorAt("disallow", "string is in types [string]"));
  75. }
  76. TEST(Annotation, AttachesAlwaysFalseSensibly) {
  77. auto const schema = R"({
  78. "properties": {
  79. "A": false
  80. }
  81. })"_json;
  82. auto const instance = R"({
  83. "A": null
  84. })"_json;
  85. jvalidate::ValidationResult result = validate(schema, instance);
  86. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
  87. }
  88. TEST(Annotation, IfThenCanGiveABecauseReason) {
  89. auto const schema = R"({
  90. "if": {
  91. "properties": {
  92. "A": { "const": true }
  93. },
  94. "required": [ "A" ]
  95. },
  96. "then": {
  97. "properties": {
  98. "B": { "multipleOf": 3 }
  99. }
  100. },
  101. "else": {
  102. "properties": {
  103. "B": { "multipleOf": 5 }
  104. }
  105. }
  106. })"_json;
  107. auto const instance = R"({
  108. "A": true,
  109. "B": 4
  110. })"_json;
  111. jvalidate::ValidationResult result = validate(schema, instance);
  112. EXPECT_THAT(result,
  113. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  114. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "matches value"));
  115. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/then/properties/B"_jptr, "multipleOf",
  116. "4 is not a multiple of 3"));
  117. }
  118. TEST(Annotation, IfElseCanGiveABecauseReason) {
  119. auto const schema = R"({
  120. "if": {
  121. "properties": {
  122. "A": { "const": true }
  123. },
  124. "required": [ "A" ]
  125. },
  126. "then": {
  127. "properties": {
  128. "B": { "multipleOf": 3 }
  129. }
  130. },
  131. "else": {
  132. "properties": {
  133. "B": { "multipleOf": 5 }
  134. }
  135. }
  136. })"_json;
  137. auto const instance = R"({
  138. "A": false,
  139. "B": 4
  140. })"_json;
  141. jvalidate::ValidationResult result = validate(schema, instance);
  142. EXPECT_THAT(result,
  143. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  144. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "true was expected"));
  145. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/else/properties/B"_jptr, "multipleOf",
  146. "4 is not a multiple of 5"));
  147. }
  148. TEST(Annotation, OneOfGivesAllReasonsOnTooMany) {
  149. auto const schema = R"({
  150. "oneOf": [
  151. { "minimum": 5 },
  152. { "multipleOf": 3 },
  153. { "maximum": 2 }
  154. ]
  155. })"_json;
  156. auto const instance = R"(6)"_json;
  157. jvalidate::ValidationResult result = validate(schema, instance);
  158. EXPECT_THAT(result, ErrorAt(""_jptr, ""_jptr, "oneOf", "validates multiple subschemas [ 0, 1 ]"));
  159. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/0"_jptr, "minimum", "6 >= 5"));
  160. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "6 is a multiple of 3"));
  161. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "6 > 2"));
  162. }
  163. TEST(Annotation, OneOfGivesAllReasonsOnNoMatches) {
  164. auto const schema = R"({
  165. "oneOf": [
  166. { "minimum": 5 },
  167. { "multipleOf": 3 },
  168. { "maximum": 2 }
  169. ]
  170. })"_json;
  171. auto const instance = R"(4)"_json;
  172. jvalidate::ValidationResult result = validate(schema, instance);
  173. EXPECT_THAT(result, ErrorAt(""_jptr, ""_jptr, "oneOf", "validates no subschemas"));
  174. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/0"_jptr, "minimum", "4 < 5"));
  175. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "4 is not a multiple of 3"));
  176. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "4 > 2"));
  177. }
  178. TEST(Annotation, OneOfGivesAllReasonsOnMatch) {
  179. auto const schema = R"({
  180. "oneOf": [
  181. { "minimum": 5 },
  182. { "multipleOf": 3 },
  183. { "maximum": 2 }
  184. ]
  185. })"_json;
  186. auto const instance = R"(3)"_json;
  187. jvalidate::ValidationResult result = validate(schema, instance);
  188. EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, ""_jptr)));
  189. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "3 is a multiple of 3"));
  190. }
  191. int main(int argc, char ** argv) {
  192. testing::InitGoogleMock(&argc, argv);
  193. return RUN_ALL_TESTS();
  194. }