I am happy to announce the release of conformist 0.2.1.
Conformist deals with schema definition and validation. It supports decoding to bridge the gap between runtime types and static types without ppx.
type occupation =
| Mathematician
| Engineer
type user =
{ occupation : occupation
; email : string
; birthday : int * int * int
; nr_of_siblings : int
; comment : string option
; wants_premium : bool
}
let user occupation email birthday nr_of_siblings comment wants_premium =
{ occupation; email; birthday; nr_of_siblings; comment; wants_premium }
;;
let occupation_decoder = function
| "mathematician" -> Ok Mathematician
| "engineer" -> Ok Engineer
| _ -> Error "Unknown occupation provided"
;;
let occupation_encoder = function
| Mathematician -> "mathematician"
| Engineer -> "engineer"
;;
let user_schema =
Conformist.(
make
Field.
[ custom
occupation_decoder
occupation_encoder
"occupation"
~meta:()
; string "email"
; date "birthday"
; int ~default:0 "nr_of_siblings"
; optional (string "comment")
; bool "wants_premium"
]
user)
;;
let input =
[ "occupation", [ "engineer" ]
; "email", [ "test@example.com" ]
; "birthday", [ "2020-12-01" ]
; "nr_of_siblings", [ "3" ]
; "comment", [ "hello" ]
; "wants_premium", [ "true" ]
]
let user =
Conformist.decode Schema.user_schema input
let validation_errors =
Conformist.validate Schema.user_schema input
The user_schema
and the user
create function are guaranteed to be in sync at compile time.