|
@@ -9,14 +9,15 @@
|
|
|
#include <jvalidate/uri.h>
|
|
#include <jvalidate/uri.h>
|
|
|
#include <jvalidate/validation_result.h>
|
|
#include <jvalidate/validation_result.h>
|
|
|
#include <jvalidate/validator.h>
|
|
#include <jvalidate/validator.h>
|
|
|
|
|
+#include <sstream>
|
|
|
|
|
|
|
|
#include "matchers.h"
|
|
#include "matchers.h"
|
|
|
|
|
|
|
|
using enum jvalidate::schema::Version;
|
|
using enum jvalidate::schema::Version;
|
|
|
using testing::Not;
|
|
using testing::Not;
|
|
|
|
|
|
|
|
-auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
|
|
|
|
|
- jvalidate::schema::Version version = Draft2020_12) {
|
|
|
|
|
|
|
+static auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
|
|
|
|
|
+ jvalidate::schema::Version version = Draft2020_12) {
|
|
|
jvalidate::Schema const schema(schema_doc, version);
|
|
jvalidate::Schema const schema(schema_doc, version);
|
|
|
|
|
|
|
|
jvalidate::ValidationResult result;
|
|
jvalidate::ValidationResult result;
|
|
@@ -25,7 +26,7 @@ auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, AttachesFormattingAnnotation) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, AttachesFormattingAnnotation) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"format": "uri"
|
|
"format": "uri"
|
|
|
})"_json;
|
|
})"_json;
|
|
@@ -37,7 +38,17 @@ TEST(Annotation, AttachesFormattingAnnotation) {
|
|
|
EXPECT_THAT(result, AnnotationAt("format", "uri"));
|
|
EXPECT_THAT(result, AnnotationAt("format", "uri"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, AnnotatesErrors) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, AnnotatesAtRootSafely) {
|
|
|
|
|
+ auto const schema = R"(false)"_json;
|
|
|
|
|
+
|
|
|
|
|
+ auto const instance = R"(4)"_json;
|
|
|
|
|
+
|
|
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance);
|
|
|
|
|
+
|
|
|
|
|
+ EXPECT_THAT(result, ErrorAt("", "always false"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(AnnotationTest, AnnotatesErrors) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"minimum": 5
|
|
"minimum": 5
|
|
|
})"_json;
|
|
})"_json;
|
|
@@ -49,7 +60,42 @@ TEST(Annotation, AnnotatesErrors) {
|
|
|
EXPECT_THAT(result, ErrorAt("minimum", "4 < 5"));
|
|
EXPECT_THAT(result, ErrorAt("minimum", "4 < 5"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, DoesNotAnnotatesValid) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, AnnotatesDescription) {
|
|
|
|
|
+ auto const schema = R"({
|
|
|
|
|
+ "description": "lorem ipsum",
|
|
|
|
|
+ "minimum": 5
|
|
|
|
|
+ })"_json;
|
|
|
|
|
+
|
|
|
|
|
+ auto const instance = R"(4)"_json;
|
|
|
|
|
+
|
|
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance);
|
|
|
|
|
+
|
|
|
|
|
+ EXPECT_THAT(result, AnnotationAt("description", "lorem ipsum"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(AnnotationTest, CanRecordAnnotationsAsList) {
|
|
|
|
|
+ auto const schema = R"({
|
|
|
|
|
+ "items": false
|
|
|
|
|
+ })"_json;
|
|
|
|
|
+
|
|
|
|
|
+ auto const instance = R"([ 0, 1 ])"_json;
|
|
|
|
|
+
|
|
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance);
|
|
|
|
|
+ EXPECT_THAT(result, AnnotationAt("items", std::vector<std::string>{"0", "1"}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(AnnotationTest, OnlyErrors) {
|
|
|
|
|
+ auto const schema = R"({
|
|
|
|
|
+ "items": false
|
|
|
|
|
+ })"_json;
|
|
|
|
|
+
|
|
|
|
|
+ auto const instance = R"([ 0, 1 ])"_json;
|
|
|
|
|
+
|
|
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance).only_errors();
|
|
|
|
|
+ EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, ""_jptr)));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(AnnotationTest, DoesNotAnnotatesValid) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"minimum": 5
|
|
"minimum": 5
|
|
|
})"_json;
|
|
})"_json;
|
|
@@ -61,7 +107,7 @@ TEST(Annotation, DoesNotAnnotatesValid) {
|
|
|
EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
|
|
EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, NotSchemaFlipsAnnotationRule) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, NotSchemaFlipsAnnotationRule) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"not": { "minimum": 5 }
|
|
"not": { "minimum": 5 }
|
|
|
})"_json;
|
|
})"_json;
|
|
@@ -73,7 +119,7 @@ TEST(Annotation, NotSchemaFlipsAnnotationRule) {
|
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/not"_jptr, "minimum", "6 >= 5"));
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/not"_jptr, "minimum", "6 >= 5"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, NotSchemaPropogatesDeeply) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, NotSchemaPropogatesDeeply) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"not": {
|
|
"not": {
|
|
|
"properties": {
|
|
"properties": {
|
|
@@ -91,7 +137,7 @@ TEST(Annotation, NotSchemaPropogatesDeeply) {
|
|
|
EXPECT_THAT(result, ErrorAt("/A"_jptr, "/not/properties/A"_jptr, "enum", "1"));
|
|
EXPECT_THAT(result, ErrorAt("/A"_jptr, "/not/properties/A"_jptr, "enum", "1"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, PathFollowsSchemaNotConstraintModel) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, PathFollowsSchemaNotConstraintModel) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"$comment": "disallow is implemented in the form of NotConstraint[TypeConstraint]",
|
|
"$comment": "disallow is implemented in the form of NotConstraint[TypeConstraint]",
|
|
|
"disallow": "string"
|
|
"disallow": "string"
|
|
@@ -104,7 +150,7 @@ TEST(Annotation, PathFollowsSchemaNotConstraintModel) {
|
|
|
EXPECT_THAT(result, ErrorAt("disallow", "string is in types [string]"));
|
|
EXPECT_THAT(result, ErrorAt("disallow", "string is in types [string]"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, AttachesAlwaysFalseSensibly) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, AttachesAlwaysFalseSensibly) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"properties": {
|
|
"properties": {
|
|
|
"A": false
|
|
"A": false
|
|
@@ -119,7 +165,7 @@ TEST(Annotation, AttachesAlwaysFalseSensibly) {
|
|
|
EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
|
|
EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, IfThenCanGiveABecauseReason) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, IfThenCanGiveABecauseReason) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"if": {
|
|
"if": {
|
|
|
"properties": {
|
|
"properties": {
|
|
@@ -153,7 +199,7 @@ TEST(Annotation, IfThenCanGiveABecauseReason) {
|
|
|
"4 is not a multiple of 3"));
|
|
"4 is not a multiple of 3"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, IfElseCanGiveABecauseReason) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, IfElseCanGiveABecauseReason) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"if": {
|
|
"if": {
|
|
|
"properties": {
|
|
"properties": {
|
|
@@ -187,7 +233,7 @@ TEST(Annotation, IfElseCanGiveABecauseReason) {
|
|
|
"4 is not a multiple of 5"));
|
|
"4 is not a multiple of 5"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, OneOfGivesAllReasonsOnTooMany) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, OneOfGivesAllReasonsOnTooMany) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"oneOf": [
|
|
"oneOf": [
|
|
|
{ "minimum": 5 },
|
|
{ "minimum": 5 },
|
|
@@ -206,7 +252,7 @@ TEST(Annotation, OneOfGivesAllReasonsOnTooMany) {
|
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "6 > 2"));
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "6 > 2"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, OneOfGivesAllReasonsOnNoMatches) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, OneOfGivesAllReasonsOnNoMatches) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"oneOf": [
|
|
"oneOf": [
|
|
|
{ "minimum": 5 },
|
|
{ "minimum": 5 },
|
|
@@ -225,7 +271,7 @@ TEST(Annotation, OneOfGivesAllReasonsOnNoMatches) {
|
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "4 > 2"));
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/2"_jptr, "maximum", "4 > 2"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TEST(Annotation, OneOfGivesAllReasonsOnMatch) {
|
|
|
|
|
|
|
+TEST(AnnotationTest, OneOfGivesAllReasonsOnMatch) {
|
|
|
auto const schema = R"({
|
|
auto const schema = R"({
|
|
|
"oneOf": [
|
|
"oneOf": [
|
|
|
{ "minimum": 5 },
|
|
{ "minimum": 5 },
|
|
@@ -242,7 +288,54 @@ TEST(Annotation, OneOfGivesAllReasonsOnMatch) {
|
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "3 is a multiple of 3"));
|
|
EXPECT_THAT(result, ErrorAt(""_jptr, "/oneOf/1"_jptr, "multipleOf", "3 is a multiple of 3"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+TEST(ValidationResultJsonTest, OutputsFormattedJSON) {
|
|
|
|
|
+ auto const schema = R"({
|
|
|
|
|
+ "items": false
|
|
|
|
|
+ })"_json;
|
|
|
|
|
+
|
|
|
|
|
+ auto const instance = R"([ 0, 1 ])"_json;
|
|
|
|
|
+
|
|
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance);
|
|
|
|
|
+ std::stringstream ss;
|
|
|
|
|
+ ss << result;
|
|
|
|
|
+
|
|
|
|
|
+ EXPECT_THAT(ss.str(), R"({
|
|
|
|
|
+ "valid": false,
|
|
|
|
|
+ "details": [
|
|
|
|
|
+ {
|
|
|
|
|
+ "valid": false,
|
|
|
|
|
+ "evaluationPath": "",
|
|
|
|
|
+ "instanceLocation": "",
|
|
|
|
|
+ "annotations": {
|
|
|
|
|
+ "items": [
|
|
|
|
|
+ "0",
|
|
|
|
|
+ "1"
|
|
|
|
|
+ ]
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "valid": false,
|
|
|
|
|
+ "evaluationPath": "/items",
|
|
|
|
|
+ "instanceLocation": "/0",
|
|
|
|
|
+ "errors": {
|
|
|
|
|
+ "": "always false"
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "valid": false,
|
|
|
|
|
+ "evaluationPath": "/items",
|
|
|
|
|
+ "instanceLocation": "/1",
|
|
|
|
|
+ "errors": {
|
|
|
|
|
+ "": "always false"
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ ]
|
|
|
|
|
+})");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#if !defined(JVALIDATE_MONOTEST)
|
|
|
int main(int argc, char ** argv) {
|
|
int main(int argc, char ** argv) {
|
|
|
testing::InitGoogleMock(&argc, argv);
|
|
testing::InitGoogleMock(&argc, argv);
|
|
|
return RUN_ALL_TESTS();
|
|
return RUN_ALL_TESTS();
|
|
|
}
|
|
}
|
|
|
|
|
+#endif
|