package validate

  1. Overview
  2. Docs
OCaml library enabling efficient data validation through PPX derivers and a suite of annotation-based validators

Install

Dune Dependency

Authors

Maintainers

Sources

validate-0.1.0.tbz
sha256=7b3e8978f003d5af74b182c476bbf829572000dd53e7d72ed98cecd4c4b26daf
sha512=ce9f0be079fff7a9250756384954e36e6b12376b5b1b7a75d7c88a14eb323758048faa906a47c7e36ed65854014c549457be6f4f4a6d9cce6be7c52f108b354c

doc/src/ppx_derive_validate/field.ml.html

Source file field.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
open Ppxlib

type record_field = {
  name : string;
  field_type : record_field_type;
  loc : Location.t;
}

and record_field_type =
  | Bool
  | Int
  | Float
  | String
  | Option of record_field_type
  | Other of longident
  | List of record_field_type

let rec extract_field_type label_loc (t : core_type) =
  let field_type =
    match t.ptyp_desc with
    | Ptyp_constr ({ txt = Lident "string"; _ }, []) -> String
    | Ptyp_constr ({ txt = Lident "int"; _ }, []) -> Int
    | Ptyp_constr ({ txt = Lident "bool"; _ }, []) -> Bool
    | Ptyp_constr ({ txt = Lident "float"; _ }, []) -> Float
    | Ptyp_constr ({ txt = Lident "option"; _ }, [ arg ]) ->
        Option (extract_field_type label_loc arg)
    | Ptyp_constr ({ txt = Lident "list"; _ }, [ arg ]) ->
        List (extract_field_type label_loc arg)
    | Ptyp_constr ({ txt = name; _ }, []) -> Other name
    | _ -> Location.raise_errorf ~loc:label_loc "Unsupported type"
  in
  field_type

let extract_record_field (ld : label_declaration) =
  let field_type = extract_field_type ld.pld_name.loc ld.pld_type in
  let record_field = { name = ld.pld_name.txt; field_type; loc = ld.pld_loc } in
  record_field
OCaml

Innovation. Community. Security.