Skip to content

Instantly share code, notes, and snippets.

@peter
Last active September 30, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/c7db8d8b99fa9ee106f257ee0feb8856 to your computer and use it in GitHub Desktop.
Save peter/c7db8d8b99fa9ee106f257ee0feb8856 to your computer and use it in GitHub Desktop.
(require '[schema.core :as s])
(def StrOrKeyword (s/cond-pre s/Str s/Keyword))
(declare Schema)
(declare SchemaValue)
(def SchemaMap {s/Keyword (s/recursive #'SchemaValue)})
(def SchemaArray [(s/recursive #'SchemaValue)])
(def SchemaValue (s/cond-pre s/Str s/Num Nil s/Bool SchemaMap SchemaArray))
(def SchemaType (s/enum "string" "number" "integer" "null" "boolean" "array" "object"))
(def Schema {
(s/optional-key :type) (s/cond-pre SchemaType [SchemaType])
(s/optional-key :properties) {s/Keyword (s/recursive #'Schema)}
(s/optional-key :additionalProperties) s/Bool
(s/optional-key :required) [StrOrKeyword]
(s/optional-key :items) (s/recursive #'Schema)
(s/optional-key :enum) [SchemaValue]
s/Keyword SchemaValue})
;; (testing "Schema"
;; (is (s/validate Schema {:type "string"}))
;; (is (s/validate Schema {:type "number"}))
;; (is (s/validate Schema {:type "integer"}))
;; (is (s/validate Schema {:type "boolean"}))
;; (is (s/validate Schema {:type ["string" "null"]}))
;; (is (s/validate Schema {
;; :type "object"
;; :properties {
;; :title {:type "string"}
;; :admin {:type "boolean"}
;; }
;; :required [:title]
;; :additionalProperties false
;; }))
;; (is (s/validate Schema {
;; :type "object"
;; :properties {
;; :title {
;; :type "object"
;; :properties {
;; :se {:type "string"}
;; }
;; }
;; :admin {:type "boolean"}
;; }
;; :required [:title]
;; :additionalProperties false
;; }))
;; (is (s/validate Schema {
;; :type "array"
;; :items {
;; :type "object"
;; :properties {
;; :title {:type "string"}
;; :admin {:type "boolean"}
;; }
;; :required [:title]
;; :additionalProperties false
;; }
;; }))
;; (is (not-empty ((s/checker Schema) {
;; :type "object"
;; :properties {
;; :title {:type "string" :meta {:foo (fn [])}} ; Function is not a SchemaValue
;; }})))
;; )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment