validator

Create a record validator via composable sub-validators
README

Create a record validator via composable sub-validators

Installation

Using Opam

opam install validator

Using Esy

esy add @opam/validator

Usage

In 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_age

validator { name = "Sam"; email = Some "sam@sample.com"; age = 20}

==>

Ok { name = "Sam"; email = "sam@sample.com"; age = 20}

Contributing

Take a look at our Contributing Guide.

Install
Published
27 Aug 2021
Maintainers
Sources
validator-0.2.3.tbz
sha256=c54012be18ee9ebe850b0f051fd3ffc5ffd86879c9f74a7133c97a77ce9ab6ce
sha512=8a392666a3afc7637551f81cdcecb2071c048844552fe4cd28d4314b6229bb19bd7197460c509fa82bf9bdcf5e76b3b89d0a98963689b98dd7d9f8596e031ed2
Dependencies
odoc
with-doc
ppx_deriving
with-test
alcotest
with-test
dune
>= "2.7"
ocaml
>= "4.08.0"
Reverse Dependencies