Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sporto/ocaml_validator
A validator library for OCaml
https://github.com/sporto/ocaml_validator
ocaml
Last synced: about 1 month ago
JSON representation
A validator library for OCaml
- Host: GitHub
- URL: https://github.com/sporto/ocaml_validator
- Owner: sporto
- License: isc
- Created: 2021-08-21T23:02:21.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-26T23:44:49.000Z (over 3 years ago)
- Last Synced: 2024-11-07T22:32:57.328Z (3 months ago)
- Topics: ocaml
- Language: OCaml
- Homepage: https://sporto.github.io/ocaml_validator/
- Size: 69.3 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Validator
[![CI](https://github.com/sporto/ocaml_validator/actions/workflows/ci.yml/badge.svg)](https://github.com/sporto/ocaml_validator/actions/workflows/ci.yml)
Create a record validator via composable sub-validators
## Installation
### Using Opam
```bash
opam install validator
```### Using Esy
```bash
esy add @opam/validator
```## Usage
### In OCaml
```ocaml
type input_form = {
name: string;
email: string option;
age: int;
}type valid_form = {
name: string;
email: string;
age: int;
}let build_valid_form name email age =
{ name; email; age }let validator_name =
let open Validator in
string_is_not_empty "Empty"
|> compose
(string_has_min_length 3 "Name is too short")let validator_email =
let open Validator in
option_is_some "Missing email"
|> compose (string_is_email "Not an email")let validator_age =
let open Validator in
int_min 13 "Must be 13"let validator (input: input_form) =
let open Validator in
build build_valid_form
|> validate input.name validator_name
|> validate input.email validator_email
|> validate input.age validator_agevalidator { name = "Sam"; email = Some "[email protected]"; age = 20}
==>
Ok { name = "Sam"; email = "[email protected]"; age = 20}
```## Contributing
Take a look at our [Contributing Guide](CONTRIBUTING.md).