Skip to content
plain data schemas
Branch: master
Clone or download
Latest commit 4c1bb2c Jul 14, 2019

README.md

malli Build Status

Plain data Schemas for Clojure/Script.

STATUS: Pre-alpha, in design and prototyping phase.

  • Schemas as data
  • Schema-driven Runtime Validation
  • Schema-driven Transformations
  • Tools for programming with Schemas
  • No global state, explicit everything
  • First class error messages
  • Fast

Examples

Definining and validating Schemas:

(require '[malli.core :as m])

(m/validate int? "1")
; => false

(m/validate int? 1)
; => true

(m/validate [:and int? [:> 6]] 7)
; => true

(def valid?
  (m/validator
    [:map
     [:x boolean?]
     [[:opt :y] int?]
     [:z string?]]))

(valid? {:x true, :z "kikka"})
; => true

Schemas can have properties:

(def Age
  [:and
   {:title "Age"
    :description "It's an age"
    :json-schema/example 20}
   int? [:> 18]])
   
(m/properties Age)
; => {:title "Age"
;     :description "It's an age"
;     :json-schema/example 20}   

Serializing & Deserializing schemas:

(require '[clojure.edn :as edn])

(-> [:map
     [:x boolean?]
     [[:opt :y] int?]
     [:z string?]]
    (m/schema)
    (pr-str)
    (edn/read-string)
    (m/schema)
    (m/validate
      {:x true, :z "kikka"}))
; => true

Performance:

(require '[clojure.spec.alpha :as s])
(require '[criterium.core :as cc])

;; 40ns
(let [spec (s/and int? (s/or :pos-int pos-int? :neg-int neg-int?))
      valid? (partial s/valid? spec)]
  (assert (= [true false true] (map valid? [-1 0 1])))
  (cc/quick-bench
    (valid? spec 0)))

;; 5ns
(let [valid? (m/validator [:and int? [:or pos-int? neg-int?]])]
  (assert (= [true false true] (map valid? [-1 0 1])))
  (cc/quick-bench
    (valid? 0)))

More

Should have:

  • Context dependent pluggable validators
  • Context dependent pluggable coercers
  • Close compatibility to Json Schema 7

Could have:

  • Clojure spec generation
  • Database schema generations

Links

Running tests

We use Kaocha as a test runner. Before running the tests, you need to install NPM dependencies.

npm install
bin/kaocha
You can’t perform that action at this time.