Created
December 27, 2018 14:21
-
-
Save mauricioszabo/54cf7be02fd5ce7f85fe531032f4f39c to your computer and use it in GitHub Desktop.
Coercion example with Clojure, Schemas, and Paprika
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[paprika.schemas :as schemas] | |
'[cheshire.core :as json]) | |
(def Person {:name schemas/NonEmptyStr | |
:surname schemas/NonEmptyStr | |
:cpf (schemas/digits-string 11) | |
:birth-date schemas/Date}) | |
(def as-person (schemas/coercer-for Person)) | |
(def some-json "{\"name\":\"Mauricio\"}") | |
; This fails! | |
(as-person (json/decode some-json keyword)) | |
; This passes, and coerces `:birth-date` to a Joda Date | |
(as-person {:name "Mauricio" | |
:surname "Szabo" | |
:cpf "12345678900" | |
:birth-date "1992-01-01"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can run it with
lein try
plug-in:lein try paprika "0.1.1-SNAPSHOT" cheshire "5.8.1"
Or
clj
cli tool, but I don't really use it so I don't know the exact command