|
|
@@ -1,34 +1,22 @@
|
|
|
-#include <jvalidate/constraint/extension_constraint.h>
|
|
|
-#include <jvalidate/detail/expect.h>
|
|
|
#include <jvalidate/extension.h>
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
|
#include <gtest/gtest.h>
|
|
|
-
|
|
|
-#include <json/reader.h>
|
|
|
#include <json/value.h>
|
|
|
|
|
|
#include <jvalidate/adapters/jsoncpp.h>
|
|
|
+#include <jvalidate/constraint/extension_constraint.h>
|
|
|
+#include <jvalidate/detail/expect.h>
|
|
|
#include <jvalidate/detail/relative_pointer.h>
|
|
|
+#include <jvalidate/forward.h>
|
|
|
#include <jvalidate/status.h>
|
|
|
#include <jvalidate/validator.h>
|
|
|
|
|
|
-using jvalidate::Status;
|
|
|
-using testing::Eq;
|
|
|
-
|
|
|
-Json::Value operator""_json(char const * data, size_t len) {
|
|
|
- Json::Value value;
|
|
|
-
|
|
|
- Json::CharReaderBuilder builder;
|
|
|
- std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
|
|
-
|
|
|
- std::string error;
|
|
|
- if (not reader->parse(data, data + len, &value, &error)) {
|
|
|
- throw std::runtime_error(error);
|
|
|
- }
|
|
|
+#include "matchers.h"
|
|
|
|
|
|
- return value;
|
|
|
-}
|
|
|
+using enum jvalidate::schema::Version;
|
|
|
+using jvalidate::Status;
|
|
|
+using jvalidate::constraint::ExtensionConstraint;
|
|
|
|
|
|
struct IsKeyOfConstraint : jvalidate::extension::ConstraintBase<IsKeyOfConstraint> {
|
|
|
IsKeyOfConstraint(std::string_view ptr) : ptr(ptr) {
|
|
|
@@ -58,11 +46,47 @@ private:
|
|
|
A const & root_document_;
|
|
|
};
|
|
|
|
|
|
+auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
|
|
|
+ jvalidate::schema::Version version = Draft2020_12) {
|
|
|
+ jvalidate::ConstraintFactory<jvalidate::adapter::AdapterFor<Json::Value>> factory{
|
|
|
+ {"is_key_of", [](auto const & context) {
|
|
|
+ return ExtensionConstraint::make<IsKeyOfConstraint>(context.schema.as_string());
|
|
|
+ }}};
|
|
|
+ jvalidate::Schema const schema(schema_doc, version);
|
|
|
+
|
|
|
+ jvalidate::ValidationResult result;
|
|
|
+ (void)jvalidate::Validator(schema).validate(instance_doc, &result);
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
TEST(ExtensionConstraint, CanExtend) {
|
|
|
- jvalidate::schema::Node schema;
|
|
|
- jvalidate::detail::StdRegexEngine re;
|
|
|
+ auto schema = R"({
|
|
|
+ "properties": {
|
|
|
+ "nodes": {
|
|
|
+ "type": "object"
|
|
|
+ },
|
|
|
+ "edges": {
|
|
|
+ "items": {
|
|
|
+ "properties": {
|
|
|
+ "destination": {
|
|
|
+ "is_key_of": "3/nodes",
|
|
|
+ "type": "string"
|
|
|
+ },
|
|
|
+ "source": {
|
|
|
+ "is_key_of": "3/nodes",
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "type": "object"
|
|
|
+ },
|
|
|
+ "type": "array"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "type": "object"
|
|
|
+ })"_json;
|
|
|
|
|
|
- auto json = R"({
|
|
|
+ auto instance = R"({
|
|
|
"nodes": {
|
|
|
"A": {},
|
|
|
"B": {}
|
|
|
@@ -71,13 +95,9 @@ TEST(ExtensionConstraint, CanExtend) {
|
|
|
{ "source": "A", "destination": "B" }
|
|
|
]
|
|
|
})"_json;
|
|
|
- using Adapter = jvalidate::adapter::JsonCppAdapter<Json::Value>;
|
|
|
- Adapter document = json;
|
|
|
|
|
|
- jvalidate::ValidationVisitor validator(schema, {}, re, Visitor(document), nullptr);
|
|
|
- jvalidate::constraint::ExtensionConstraint cons{std::make_unique<IsKeyOfConstraint>("1/nodes")};
|
|
|
- EXPECT_THAT(validator.visit(cons, Adapter(json["edges"][0]["source"])), Eq(Status::Accept));
|
|
|
- EXPECT_THAT(validator.visit(cons, Adapter(json["edges"][0]["destination"])), Eq(Status::Accept));
|
|
|
+ jvalidate::ValidationResult result = validate(schema, instance);
|
|
|
+ EXPECT_THAT(result, Valid()) << result;
|
|
|
}
|
|
|
|
|
|
int main(int argc, char ** argv) {
|