package conformist

Build not checked
  1. Overview
  2. Docs
Conformist allows you to define schemas to decode, validate and sanitize input data declaratively

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.5.0.tar.gz
md5=4a9eed391f23b44c3b24ea0891efd8b3
sha512=1a2fa6d4c36b7cd74807a686297ee4fc3361b6c6a8e42732206a12a448ace93d91d59c56fe758a4a9bd6a1d330f91b0f7910541d671544ea6cf7630b3b26c473

Description

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 and float but also Ptime.date, optional and custom types. Re-use business rules in validators and run it on the client side with js_of_ocaml. Arbitrary meta data can be stored in schemas which is useful to build functionality on top of conformist.

Added to opam-repository:

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

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 andPtime.t, optional and custom types. Re-use business rules in validators and run it on the client side with 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

opam install conformist

In your dune file:

(executable
  (name app)
  (libraries
   ...
   conformist))

Usage

Let's look at an example.

type occupation =
  | Mathematician
  | Engineer

type user =
  { occupation : occupation
  ; email : string
  ; birthday : Ptime.t
  ; 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-01T00:00:00.00Z" ]
    ; "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

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.

Documentation

The documentation for the latest released version can be found here.

License

Copyright (c) 2020 Oxidizing Systems

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

The implementation of this project was inspired by archi and re-web.

Dependencies (3)

  1. ptime >= "0.8.5"
  2. ocaml >= "4.08.0"
  3. dune >= "2.4"

Dev Dependencies (2)

  1. sexplib >= "v0.13.0" & with-test
  2. alcotest >= "1.2.3" & with-test

Optional Dependencies

None

Used by (1)

  1. sihl >= "0.4.0" & < "1.0.0~rc1"

Conflicts (1)

  1. result < "1.5"

Build status

Results of building conformist with each compiler on check.ci.ocaml.org, run of against opam-repository 8d8d144. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.8.1 good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure