Skip to content

Commit

Permalink
0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
joseferben committed Mar 16, 2021
1 parent 64384a1 commit 593c2d9
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 20 deletions.
27 changes: 25 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Retrieve opam cache
uses: actions/cache@v2
if: runner.os != 'Windows'
Expand All @@ -29,18 +30,40 @@ jobs:
key: v1-${{ runner.os }}-opam-${{ matrix.ocaml-version }}-${{ hashFiles('*.opam.locked') }}
restore-keys: |
v2-${{ runner.os }}-opam-${{ matrix.ocaml-version }}-
- name: Use OCaml ${{ matrix.ocaml-version }}
uses: avsm/setup-ocaml@v1
with:
ocaml-version: ${{ matrix.ocaml-version }}

- name: Update opam repository
if: steps.cache-opam.outputs.cache-hit != 'true'
run: opam update

- name: Pin package
run: opam pin add . --yes --no-action

- name: Query and install external dependencies
run: opam depext conformist --yes --with-doc --with-test

- name: Install dependencies
if: steps.cache-opam.outputs.cache-hit != 'true'
run: |
opam install -y . --deps-only --with-doc --with-test --locked --unlock-base
- name: Recover from an Opam broken state
if: steps.cache-opam.outputs.cache-hit == 'true'
opam install ocamlformat --skip-updates
- name: Upgrade dependencies
run: opam upgrade --fixup
if: steps.cache-opam.outputs.cache-hit == 'true'

- name: Build
run: make build

- name: Check formatting
run: make format

- name: Build docs
run: make doc

- name: Run tests
run: make test
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog
## 0.2.1 - 2021-03-16
### Changed
- Replace `ppx_deriving` with `sexplib`

## [0.2.0] - 2021-03-07
### Changed
Expand Down
5 changes: 2 additions & 3 deletions conformist.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.2.0"
version: "0.2.1"
synopsis:
"Conformist allows you to define schemas to decode, validate and sanitize input data declaratively"
description: """
Expand All @@ -20,8 +20,7 @@ depends: [
"dune" {>= "2.4"}
"ocaml" {>= "4.08.0"}
"alcotest" {>= "1.2.3" & with-test}
"ppx_deriving" {>= "4.5" & with-test}
"ppx_fields_conv" {>= "v0.14.1" & with-test}
"sexplib" {>= "v0.13.0" & with-test}
]
build: [
["dune" "subst"] {pinned}
Expand Down
42 changes: 42 additions & 0 deletions conformist.opam.locked
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
opam-version: "2.0"
version: "0.2.1"
synopsis:
"Conformist allows you to define schemas to decode, validate and sanitize input data declaratively"
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.
"""
maintainer: ["josef@oxidizing.io"]
authors: ["Josef Erben"]
license: "MIT"
homepage: "https://github.com/oxidizing/conformist"
doc: "https://oxidizing.github.io/conformist/"
bug-reports: "https://github.com/oxidizing/conformist/issues"
depends: [
"base-bigarray" {= "base"}
"base-threads" {= "base"}
"base-unix" {= "base"}
"dune" {= "2.8.4"}
"ocaml" {= "4.08.1"}
"ocaml-config" {= "1"}
"ocaml-system" {= "4.08.1"}
]
build: [
["dune" "subst"] {pinned}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/oxidizing/conformist.git"
name: "conformist"
5 changes: 2 additions & 3 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(generate_opam_files true)

(name conformist)
(version 0.2.0)
(version 0.2.1)

(authors "Josef Erben")
(source (github oxidizing/conformist))
Expand All @@ -28,5 +28,4 @@ Arbitrary meta data can be stored in schemas which is useful to build functional

;; Test
(alcotest (and (>= 1.2.3) :with-test))
(ppx_deriving (and (>= 4.5) :with-test))
(ppx_fields_conv (and (>= v0.14.1) :with-test))))
(sexplib (and (>= v0.13.0) :with-test))))
4 changes: 1 addition & 3 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
(test
(name test)
(libraries conformist alcotest)
(preprocess
(pps ppx_fields_conv ppx_deriving.eq ppx_deriving.show ppx_deriving.make)))
(libraries conformist alcotest sexplib))
57 changes: 52 additions & 5 deletions test/schema.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
module C = Conformist

type gender =
| Male
| Female
| Other
[@@deriving eq, show]

let equal_gender = function
| Male, Male -> true
| Female, Female -> true
| Other, Other -> true
| _ -> false
;;

let sexp_of_gender g =
let open Sexplib0.Sexp_conv in
match g with
| Male -> sexp_of_string "Male"
| Female -> sexp_of_string "Female"
| Other -> sexp_of_string "Other"
;;

type user =
{ gender : gender
; name : string
Expand All @@ -16,7 +29,40 @@ type user =
; comment : string option
; wants_premium : bool
}
[@@deriving eq, show]

let user_to_sexp
{ gender
; name
; email
; birthday
; country
; nr_of_siblings
; comment
; wants_premium
}
=
let open Sexplib0.Sexp_conv in
let open Sexplib0.Sexp in
let y, m, d = birthday in
List
[ List [ Atom "gender"; sexp_of_gender gender ]
; List [ Atom "name"; sexp_of_string name ]
; List [ Atom "email"; sexp_of_string email ]
; List [ Atom "birthday"; sexp_of_string (Format.sprintf "%d-%d-%d" y m d) ]
; List [ Atom "country"; sexp_of_string country ]
; List [ Atom "nr_of_siblings"; sexp_of_int nr_of_siblings ]
; List [ Atom "comment"; sexp_of_option sexp_of_string comment ]
; List [ Atom "wants_premium"; sexp_of_bool wants_premium ]
]
;;

let pp_user fmt t = Sexplib0.Sexp.pp_hum fmt (user_to_sexp t)

let equal_user u1 u2 =
String.equal
(Format.asprintf "%a" pp_user u1)
(Format.asprintf "%a" pp_user u2)
;;

let user gender name email birthday country nr_of_siblings comment wants_premium
=
Expand Down Expand Up @@ -45,8 +91,9 @@ let gender_encoder = function
;;

let user_schema =
C.make
C.Field.
let module C = Conformist in
Conformist.make
Conformist.Field.
[ C.custom gender_decoder gender_encoder ~default:Female "gender" ~meta:()
; C.string "name"
; C.string "email"
Expand Down
63 changes: 60 additions & 3 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@ module C = Conformist

let testable_form = Alcotest.testable Schema.pp_user Schema.equal_user

type validation_error = (string * string) list [@@deriving show, eq]
type validation_error = (string * string) list

let validation_error_to_sexp e =
let open Sexplib0.Sexp_conv in
let open Sexplib0.Sexp in
List
(List.map
(fun (k, v) ->
List
[ List [ Atom "key"; sexp_of_string k ]
; List [ Atom "error"; sexp_of_string v ]
])
e)
;;

let pp_validation_error fmt t =
Sexplib0.Sexp.pp_hum fmt (validation_error_to_sexp t)
;;

let equal_validation_error e1 e2 =
String.equal
(Format.asprintf "%a" pp_validation_error e1)
(Format.asprintf "%a" pp_validation_error e2)
;;

let testable_validation_error =
Alcotest.testable pp_validation_error equal_validation_error
Expand All @@ -18,7 +41,25 @@ type schema_optional =
{ name : string
; address : string option
}
[@@deriving show, eq]

let schema_option_to_sexp s =
let open Sexplib0.Sexp_conv in
let open Sexplib0.Sexp in
List
[ List [ Atom "name"; sexp_of_string s.name ]
; List [ Atom "address"; sexp_of_option sexp_of_string s.address ]
]
;;

let pp_schema_optional fmt t =
Sexplib0.Sexp.pp_hum fmt (schema_option_to_sexp t)
;;

let equal_schema_optional s1 s2 =
String.equal
(Format.asprintf "%a" pp_schema_optional s1)
(Format.asprintf "%a" pp_schema_optional s2)
;;

let testable_schema_optional =
Alcotest.testable pp_schema_optional equal_schema_optional
Expand Down Expand Up @@ -117,7 +158,23 @@ type schema_multi =
{ name : string
; age : int
}
[@@deriving show, eq]

let schema_multi_to_sexp s =
let open Sexplib0.Sexp_conv in
let open Sexplib0.Sexp in
List
[ List [ Atom "name"; sexp_of_string s.name ]
; List [ Atom "age"; sexp_of_int s.age ]
]
;;

let pp_schema_multi fmt t = Sexplib0.Sexp.pp_hum fmt (schema_multi_to_sexp t)

let equal_schema_multi s1 s2 =
String.equal
(Format.asprintf "%a" pp_schema_multi s1)
(Format.asprintf "%a" pp_schema_multi s2)
;;

let testable_schema_multi = Alcotest.testable pp_schema_multi equal_schema_multi

Expand Down

0 comments on commit 593c2d9

Please sign in to comment.