Explorar o código

refactor: restrict boolean schema resolution in Draft04

Sam Jaffe hai 1 ano
pai
achega
f8b84b59b6

+ 12 - 2
include/jvalidate/constraint.h

@@ -309,8 +309,13 @@ public:
       start_after = parent[prefix].as_integer();
     }
 
-    schema::Node const * schema = context.node();
-    return std::make_unique<constraint::AdditionalItemsConstraint>(schema, start_after);
+    using C = constraint::AdditionalItemsConstraint;
+    if (context.version < schema::Version::Draft06 &&
+        context.schema.type() == adapter::Type::Boolean) {
+      return std::make_unique<C>(context.always(), start_after);
+    }
+
+    return std::make_unique<C>(context.node(), start_after);
   }
 
   static pConstraint itemsTupleOrVector(ParserContext<A> const & context) {
@@ -405,6 +410,11 @@ public:
     }
 
     using C = constraint::AdditionalPropertiesConstraint;
+    if (context.version < schema::Version::Draft06 &&
+        context.schema.type() == adapter::Type::Boolean) {
+      return std::make_unique<C>(context.always(), properties, patterns);
+    }
+
     return std::make_unique<C>(context.node(), properties, patterns);
   }
 

+ 1 - 0
include/jvalidate/parser_context.h

@@ -28,5 +28,6 @@ template <Adapter A> struct ParserContext {
 
   schema::Node const * resolve(std::string_view uri) const;
   schema::Node const * node() const;
+  schema::Node const * always() const;
 };
 }

+ 6 - 2
include/jvalidate/schema.h

@@ -94,6 +94,7 @@ private:
 private:
   schema::Node accept_{true};
   schema::Node reject_{false};
+
   std::map<std::string, detail::Reference> anchors_;
   std::map<detail::Reference, schema::Node> cache_;
 
@@ -140,8 +141,7 @@ private:
 
   template <Adapter A> schema::Node const * fetch_schema(ParserContext<A> const & context) {
     adapter::Type const type = context.schema.type();
-    if (type == adapter::Type::Boolean) {
-      // TODO(samjaffe): Legal Context...
+    if (type == adapter::Type::Boolean && context.version >= schema::Version::Draft06) {
       return alias(context.where, context.schema.as_boolean() ? &accept_ : &reject_);
     }
 
@@ -173,6 +173,10 @@ template <Adapter A> schema::Node const * ParserContext<A>::resolve(std::string_
 template <Adapter A> schema::Node const * ParserContext<A>::node() const {
   return root.fetch_schema(*this);
 }
+
+template <Adapter A> schema::Node const * ParserContext<A>::always() const {
+  return schema.as_boolean() ? &root.accept_ : &root.reject_;
+}
 }
 
 namespace jvalidate::schema {