Skip to content

Instantly share code, notes, and snippets.

@mauricioszabo
Created December 27, 2018 14:21
Show Gist options
  • Save mauricioszabo/54cf7be02fd5ce7f85fe531032f4f39c to your computer and use it in GitHub Desktop.
Save mauricioszabo/54cf7be02fd5ce7f85fe531032f4f39c to your computer and use it in GitHub Desktop.
Coercion example with Clojure, Schemas, and Paprika
(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"})
@mauricioszabo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment