annotation_test.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, SomeConstraintsAnnotateBothValidAndInvalid) {
  77. auto const schema = R"({
  78. "$comment": "accepts any number <= 0 or >= 10",
  79. "oneOf": [
  80. { "minimum": 10 },
  81. { "maximum": 0 }
  82. ]
  83. })"_json;
  84. auto const instance = R"(-1)"_json;
  85. jvalidate::ValidationResult result = validate(schema, instance);
  86. EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, "/oneOf"_jptr)));
  87. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/0"_jptr, "minimum", "-1 < 10"));
  88. EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "maximum", "-1 <= 0"));
  89. }
  90. TEST(Annotation, AttachesAlwaysFalseSensibly) {
  91. auto const schema = R"({
  92. "properties": {
  93. "A": false
  94. }
  95. })"_json;
  96. auto const instance = R"({
  97. "A": null
  98. })"_json;
  99. jvalidate::ValidationResult result = validate(schema, instance);
  100. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
  101. }
  102. TEST(Annotation, IfThenCanGiveABecauseReason) {
  103. auto const schema = R"({
  104. "if": {
  105. "properties": {
  106. "A": { "const": true }
  107. },
  108. "required": [ "A" ]
  109. },
  110. "then": {
  111. "properties": {
  112. "B": { "multipleOf": 3 }
  113. }
  114. },
  115. "else": {
  116. "properties": {
  117. "B": { "multipleOf": 5 }
  118. }
  119. }
  120. })"_json;
  121. auto const instance = R"({
  122. "A": true,
  123. "B": 4
  124. })"_json;
  125. jvalidate::ValidationResult result = validate(schema, instance);
  126. EXPECT_THAT(result,
  127. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  128. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "matches value"));
  129. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/then/properties/B"_jptr, "multipleOf",
  130. "4 is not a multiple of 3"));
  131. }
  132. TEST(Annotation, IfElseCanGiveABecauseReason) {
  133. auto const schema = R"({
  134. "if": {
  135. "properties": {
  136. "A": { "const": true }
  137. },
  138. "required": [ "A" ]
  139. },
  140. "then": {
  141. "properties": {
  142. "B": { "multipleOf": 3 }
  143. }
  144. },
  145. "else": {
  146. "properties": {
  147. "B": { "multipleOf": 5 }
  148. }
  149. }
  150. })"_json;
  151. auto const instance = R"({
  152. "A": false,
  153. "B": 4
  154. })"_json;
  155. jvalidate::ValidationResult result = validate(schema, instance);
  156. EXPECT_THAT(result,
  157. ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
  158. EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "true was expected"));
  159. EXPECT_THAT(result, ErrorAt("/B"_jptr, "/else/properties/B"_jptr, "multipleOf",
  160. "4 is not a multiple of 5"));
  161. }
  162. int main(int argc, char ** argv) {
  163. testing::InitGoogleMock(&argc, argv);
  164. return RUN_ALL_TESTS();
  165. }