Skip to content

Commit

Permalink
Add test to check for valid schema
Browse files Browse the repository at this point in the history
This is the beginning of a suite of tests for schema correctness.
  • Loading branch information
malcolmsparks committed Jun 12, 2019
1 parent 81b55c4 commit c20d099
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
10 changes: 10 additions & 0 deletions 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)))
19 changes: 19 additions & 0 deletions src/juxt/jsonschema/schema.clj
Expand Up @@ -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
Expand Down Expand Up @@ -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->
Expand Down
13 changes: 3 additions & 10 deletions src/juxt/jsonschema/validate.cljc
Expand Up @@ -9,22 +9,14 @@
[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]
[lambdaisland.uri :as uri]))

(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.
Expand Down Expand Up @@ -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))

Expand Down
14 changes: 14 additions & 0 deletions 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})))))

0 comments on commit c20d099

Please sign in to comment.