diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
| commit | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch) | |
| tree | 448864635d29b1ea66424cbb9e0816fbcf1e1907 /lib/model/field.ml | |
Initial commit
Diffstat (limited to 'lib/model/field.ml')
| -rw-r--r-- | lib/model/field.ml | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/model/field.ml b/lib/model/field.ml new file mode 100644 index 0000000..8ea38fd --- /dev/null +++ b/lib/model/field.ml @@ -0,0 +1,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 } |
