summaryrefslogtreecommitdiff
path: root/lib/model/field.ml
blob: 8ea38fddbdf9db2d46026cccadb0c21e25a0f82b (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
type textI18n = (string * string) list
(** A label is just an association list*)

type validation = { rule : string list Dsl.t; message : textI18n }
type 't field_ex = T : ('a, 't) Typ.t -> 't field_ex

type 't t = {
  name : string;  (** The internal name *)
  label : textI18n;
  placeholder : textI18n option;
  icon : unit option;
  type' : 't field_ex;
  required : bool;
  validation : validation list;
  criteria : string list Dsl.t option;
      (** Display the field when the criteria is valid *)
}

let name : 'ref t -> string = fun t -> t.name
let label : 'ref t -> textI18n = fun t -> t.label
let placeholder : 'ref t -> textI18n option = fun t -> t.placeholder
let icon : 'ref t -> unit option = fun t -> t.icon
let type_ : 'ref t -> 'ref field_ex = fun t -> t.type'
let required : 'ref t -> bool = fun t -> t.required
let validation : 'ref t -> validation list = fun t -> t.validation
let criteria : 'ref t -> string list Dsl.t option = fun t -> t.criteria

let create :
    't field_ex ->
    textI18n ->
    ?placeholder:textI18n ->
    ?icon:unit ->
    ?required:bool ->
    ?validation:validation list ->
    ?criteria:string list Dsl.t ->
    string ->
    't t =
 fun type_ label ?placeholder ?icon ?(required = false) ?(validation = [])
     ?criteria name ->
  {
    name;
    label;
    placeholder;
    icon;
    type' = type_;
    required;
    validation;
    criteria;
  }

let map : ('a -> 'b) -> 'a t -> 'b t =
 fun f t ->
  let (T field_type) = t.type' in
  let new_type = T (Typ.map f field_type) in
  { t with type' = new_type }