Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oxidizing/conformist
Schema definition and validation with support for decoding to bridge the gap between runtime types and static types.
https://github.com/oxidizing/conformist
decoder ocaml schema schema-definitions schema-validation
Last synced: 3 months ago
JSON representation
Schema definition and validation with support for decoding to bridge the gap between runtime types and static types.
- Host: GitHub
- URL: https://github.com/oxidizing/conformist
- Owner: oxidizing
- License: mit
- Created: 2020-09-20T10:07:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-06T11:46:22.000Z (almost 3 years ago)
- Last Synced: 2023-03-05T16:26:32.188Z (almost 2 years ago)
- Topics: decoder, ocaml, schema, schema-definitions, schema-validation
- Language: OCaml
- Homepage: https://v3.ocaml.org/p/conformist
- Size: 160 KB
- Stars: 35
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
Conformist
Schema definition and validation with support for decoding to bridge the gap between runtime types and static types.
Explore the docs »
## Table of Contents
* [About](#about)
* [Installation](#installation)
* [Usage](#usage)
* [Documentation](#documentation)
* [License](#license)
* [Acknowledgements](#acknowledgements)## About
Conformist allows you to define schemas to decode, validate and sanitize input data declaratively. It comes with runtime types for primitive OCaml types such as `int`, `string`, `bool`, `float` and `Ptime.t`, `option` and custom types. Re-use business rules in validators and run it on the client side with [js_of_ocaml](https://github.com/ocsigen/js_of_ocaml/). Arbitrary meta data can be stored in schemas which is useful to build functionality on top of conformist.
Typical use cases are enforcing invariants of models or user input sanitization.
In essence, conformist helps you to keep your runtime types/contracts in sync with your static types.
## Installation
```sh
opam install conformist
```In your `dune` file:
```
(executable
(name app)
(libraries
...
conformist))
```## Usage
Let's look at an example.
```ocaml
type occupation =
| Mathematician
| Engineertype user =
{ occupation : occupation
; email : string
; birthday : Ptime.t
; nr_of_siblings : int
; comment : string option
; favorite_shows : string list
; wants_premium : bool
}let user
occupation
birthday
nr_of_siblings
comment
favorite_shows
wants_premium
=
{ occupation
; birthday
; nr_of_siblings
; comment
; favorite_shows
; wants_premium
}
;;let occupation_decoder = function
| [ "mathematician" ] -> Ok Mathematician
| [ "engineer" ] -> Ok Engineer
| _ -> Error "Unknown occupation provided"
;;let occupation_encoder = function
| Mathematician -> [ "mathematician" ]
| Engineer -> [ "engineer" ]
;;(* This is for example purpose only.
Please use emile (https://github.com/dinosaure/emile) instead *)
let validate_email value =
if String.index value '@' > 0 then None
else Some "This doesn't look like an email"
let user_schema =
Conformist.(
make
[ custom occupation_decoder occupation_encoder "occupation" ~meta:()
; string "email" ~validator: validate_email
; datetime "birthday"
; int ~default:0 "nr_of_siblings"
; optional (string "comment")
; list (string "favorite_shows")
; bool "wants_premium"
]
user)
;;let input =
[ "occupation", [ "engineer" ]
; "email", [ "[email protected]" ]
; "birthday", [ "2020-12-01T00:00:00.00Z" ]
; "nr_of_siblings", [ "3" ]
; "comment", [ "hello" ]
; "favorite_shows", [ "Iron Man"; "Avengers" ]
; "wants_premium", [ "true" ]
]
;;let user = Conformist.decode user_schema input
let validation_errors = Conformist.validate user_schema input
```Try to delete/swap some lines of the list of fields, to change the constructor or the user type. The compiler forces you to keep these three things in sync.
Decoding doesn't validate the data, it just makes sure that the types are correct and translates strings to the correct static types.
Note that if decoding of a field fails, validation fails as well. Before a field is validated, it gets decoded.
Since we are shadowing the list `[]`, dune warnings might fail compilation depending on the configuration. Suppress warning `-40` can help.
## Documentation
The documentation for the latest released version can be found [here](https://v3.ocaml.org/p/conformist/0.6.0).
## License
Copyright (c) 2020 [Oxidizing Systems](https://oxidizing.io/)
Distributed under the MIT License. See `LICENSE` for more information.
## Acknowledgements
The implementation of this project was inspired by [archi](https://github.com/anmonteiro/archi) and [re-web](https://github.com/yawaramin/re-web).