This is WIP.
| Service | OCaml | Service |
|---|---|---|
| Circle | 4.06 |
Data validation with first-class and first-order labels in OCaml.
Homepage for the library is available here.
Changesets are tools to validate data while accumulating errors along the way. They are composed of:
Changes: an heterogeneous map whose keys are of type
'a labeland values'a. When deriving a changeset from a record type definition, the labels correspond to the fields of this one.Errors: a list of strings associated with labels.
Labels are first-class, that means you can give them as argument to functions, and first-order, so you can pattern match against them. The library promotes the pipeline design approach.
There is a ppx deriving plugin so there is no need to write any boilerplate code.
$ opam pin add changeset_lib https://github.com/phink/changeset.git
$ opam pin add ppx_changeset https://github.com/phink/changeset.git
(libraries (... changeset))
(preprocess (pps (... ppx_changeset)))
typet = {
age: int;phone: string;password: string;
} [@@deriving changeset]
letvalidatecset=
cset
|>Changeset.validate_int Age [`greater_than_or_equal_to0]
|>Changeset.validate_string_length Password [`min12]
|>Changeset.validate_format Phone (Str.regexp "^\\+?[1-9][0-9]+$")
letcreatet=let cset =Changeset.from_record t inChangeset.apply (validate cset)
let data = {age =12; password ="dolphin"; phone ="+14155552671"}
let()=match create data with|Okt -> (* do what you want *)|Errorcset ->
Stdio.prerr_endline (Changeset.show_errors cset)Execution
{
"errors":{
"password":"should be at least 12 character(s)"
}
}Parameterized source types are not yet supported.