Useful extensions to Prismatic schema.
vice.coerce
add coercions to individual schema using(with-coercion func schema)
. Validate usingvice.coerce/validate
vice.valuetypes
common schemas for dates, numbers, etc. including coercions from strings. Useful for transforming json.vice.schemas
containsbetween
,conditional-on-key
,fail
,is-not
,in-any-order
,in-order
vice.midje
use schemas as Midje checkers.fail
,is-not
,in-any-order
,in-order
are useful here.
See clojars
(vice.coerce/validate
{:a (vice.coerce/with-coercion str s/Str)}
{:a 123})
; => {:a "123"}
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
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}
Copyright (C) 2014 Matt Savage. Distributed under the Eclipse Public License, the same as Clojure.