From c20d0997a3bc9e2d2b97d14f55a5e9f3b03a0031 Mon Sep 17 00:00:00 2001 From: Malcolm Sparks Date: Wed, 12 Jun 2019 09:01:43 +0100 Subject: [PATCH] Add test to check for valid schema This is the beginning of a suite of tests for schema correctness. --- src/juxt/jsonschema/core.clj | 10 ++++++++++ src/juxt/jsonschema/schema.clj | 19 +++++++++++++++++++ src/juxt/jsonschema/validate.cljc | 13 +++---------- test/juxt/jsonschema/schema_test.clj | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 src/juxt/jsonschema/core.clj create mode 100644 test/juxt/jsonschema/schema_test.clj diff --git a/src/juxt/jsonschema/core.clj b/src/juxt/jsonschema/core.clj new file mode 100644 index 0000000..3bb0c3e --- /dev/null +++ b/src/juxt/jsonschema/core.clj @@ -0,0 +1,10 @@ +(ns juxt.jsonschema.core) + +(defn array? [x] + (sequential? x)) + +(defn object? [x] + (map? x)) + +(defn schema? [x] + (or (object? x) (boolean? x))) diff --git a/src/juxt/jsonschema/schema.clj b/src/juxt/jsonschema/schema.clj index f88e67a..cd2c240 100644 --- a/src/juxt/jsonschema/schema.clj +++ b/src/juxt/jsonschema/schema.clj @@ -2,6 +2,7 @@ (ns juxt.jsonschema.schema (:require + [juxt.jsonschema.core :refer [array? object? schema?]] [lambdaisland.uri :refer [join]])) (defn- with-base-uri-meta @@ -35,7 +36,25 @@ (vector? schema) (mapcat index-by-uri schema))) +(defmulti validate-keyword (fn [kw v] kw)) + +(defmethod validate-keyword :default [kw v] nil) + +(defmethod validate-keyword "type" [kw v] + (when-not (or (string? v) (array? v)) + (throw (ex-info "The value of 'type' MUST be either a string or an array" {})))) + +(defn- validate [schema] + (or + (boolean? schema) + (nil? schema) + (doseq [[k v] (seq schema)] + (validate-keyword k v)))) + (defn schema [schema] + ;; TODO: Ensure schema is returned as-is if it's existing schema + ;; TODO: Add ^:juxt/schema true - which is the right keyword here? + (validate schema) (let [schema (with-base-uri-meta schema) index (into {} (index-by-uri schema))] (cond-> diff --git a/src/juxt/jsonschema/validate.cljc b/src/juxt/jsonschema/validate.cljc index d1653f3..dfaeccf 100644 --- a/src/juxt/jsonschema/validate.cljc +++ b/src/juxt/jsonschema/validate.cljc @@ -9,6 +9,7 @@ [clojure.java.io :as io] ;; TODO: Support cljs [clojure.test :refer [deftest is are]] [juxt.jsonschema.jsonpointer :as jsonpointer] + [juxt.jsonschema.core :refer [array? object? schema?]] [juxt.jsonschema.schema :as schema] [juxt.jsonschema.resolve :as resolv] [juxt.jsonschema.regex :as regex] @@ -16,15 +17,6 @@ (declare validate*) -(defn array? [x] - (sequential? x)) - -(defn object? [x] - (map? x)) - -(defn schema? [x] - (or (object? x) (boolean? x))) - ;; All references here relate to ;; draft-handrews-json-schema-validation-01.txt unless otherwise ;; stated. @@ -451,7 +443,8 @@ :causes (:errors result)}}))))) ;; TODO: Rather than get, use a macro to retrieve either strings and -;; keywords, supporting both +;; keywords, supporting both. But BE CAREFUL with :default as we'll +;; need to repoint the defmulti's :default in that case. (defmulti check-format (fn [fmt instance ctx] fmt)) diff --git a/test/juxt/jsonschema/schema_test.clj b/test/juxt/jsonschema/schema_test.clj new file mode 100644 index 0000000..caa62cc --- /dev/null +++ b/test/juxt/jsonschema/schema_test.clj @@ -0,0 +1,14 @@ +(ns juxt.jsonschema.schema-test + (:require + [juxt.jsonschema.schema :refer [schema]] + [clojure.test :refer [deftest is are testing]] + [juxt.jsonschema.schema :as schema]) + (:import + (clojure.lang ExceptionInfo))) + +(deftest schema-test + (testing "bad types" + (is + (thrown? + ExceptionInfo + (schema {"type" 10})))))