deckchair-technicians / vice

Prismatic schema extensions. Coercions attached to schemas. Use schema as Midje checkers...
Eclipse Public License 1.0
3 stars 0 forks source link

vice

Useful extensions to Prismatic schema.

Latest version

See clojars

Usage

Add coercions to schema:

(vice.coerce/validate 
   {:a (vice.coerce/with-coercion str s/Str)}
   {:a 123})
; => {:a "123"}

Midje checkers.

Using schemas for midje checks provides better error messages and flexibility in assertions.

Non-schema values in maps are treated as (schema.core/eq x)

(fact "matches ok"
   {:a 123}
   => (vice.midje/matches {:a 123})

in-order and in-any-order help with assertions:

(fact "matches ok"
   [{:a 123} {:a 345} {:a 234}] 
   => (vice.midje/matches (vice.schemas/in-any-order [{:a 123} {:a 234}] :extras-ok true)))

Map matching is loose by default:

(fact "matches even though it has an extra key"
   {:a 123 :not-in-schema 234} 
   => (vice.midje/matches {:a 123}))

Strictness can be turned on and is scoped:

(fact "strictness is scoped"
   {:a {:b 123
        :not-in-schema 234}} 
   => (vice.midje/matches (vice.midje/strict {:a {:b Long}})))
; checker will fail because strictness is inherited when checking value of :a

Common types with coercions

See vice.valuetypes for full list.

(vice.coerce/validate 
   vice.valuetypes/JodaDateMidnight
   "2014-07-29")
; => (clj-time/date-midnight 2014 7 29)

; NB: UK date format only
(vice.coerce/validate
   vice.valuetypes/JodaDateMidnight 
   "29/07/2014")
; => (clj-time/date-midnight 2014 7 29)

(vice.coerce/validate 
   vice.valuetypes/PositiveInteger
   "123")
; => (int 123)

(vice.coerce/validate 
   {(s/optional-key :a) vice.valuetypes/GenUuid}
   {})
; => {:a eafa7062-7bb3-4b60-b1ea-ada2dbd283c8}

License

Copyright (C) 2014 Matt Savage. Distributed under the Eclipse Public License, the same as Clojure.