package conformist

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

Install

Dune Dependency

Authors

Maintainers

Sources

0.8.0.tar.gz
md5=a428f6d935fdb92aa0a8dfebf1ddbf2e
sha512=e61ba1a6c746b84e2f3df700de68869eba26433de3b6c428f0b97174d0781dac8c5ff107793506f68cf4aff2b947f11e116c5721d5ad75c3677bf98f249ac41e

doc/src/conformist/core.ml.html

Source file core.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
module type CONFORMIST = sig
  type error_msg

  (** {1 Fields}

      Every member of the list in the example is a field. Use the provided
      [fold_left] to traverse the list of fiels. Helper functions are provided
      that operate on fields. *)

  module Field : sig
    (** A field of type [('meta, 'a) t] represents the static type ['a] and it
        can hold arbitrary meta data of type ['meta]. That meta data can be used
        to build functionality on top of conformist. *)
    type ('meta, 'a) t

    type (_, _, _) list =
      | [] : ('meta, 'ty, 'ty) list
      | ( :: ) :
          ('meta, 'a) t * ('meta, 'b, 'ty) list
          -> ('meta, 'a -> 'b, 'ty) list
          (** A [list] is a list of fields. Note that this is not the list from
              [List.t] so make sure to open this scope locally when defining a
              list of fields. *)

    type _ any_field = AnyField : ('meta, 'a) t -> 'meta any_field

    (** [meta field] returns an optional meta data of a [field]. This can be
        used to store arbitrary meta data in each field. Note that the type of
        the meta data has to be the same for all fields. *)
    val meta : 'a any_field -> 'a option

    (** [name field] returns the name of the [field], which uniquely identifies
        the field within one schema. *)
    val name : 'a any_field -> string

    (** [validate field values] decodes [values] and runs the [field]'s
        validation logic on the decoded values. Both decoding and validation
        might fail, which results in an error string. *)
    val validate : 'a any_field -> string List.t -> error_msg option

    (** [optional field] returns [true] if the [field] is optional and [false]
        otherwise. *)
    val optional : 'a any_field -> bool
      [@@deprecated "Please use is_optional instead"]

    (** [is_optional field] returns [true] if the [field] is optional and
        [false] otherwise. *)
    val is_optional : 'a any_field -> bool

    (** [type_ field] returns a string representation of the type of [field]. *)
    val type_ : 'a any_field -> string

    (** [encode_default field] tries to encode the default value if present and
        to return it as string. *)
    val encode_default : 'a any_field -> string List.t
  end

  (** A ['a decoder] tries to turn values into a value of type ['a]. It returns
      a descriptive errors message upon failure. *)
  type 'a decoder = string list -> ('a, error_msg) result

  (** A ['a encoder] encodes a value of type ['a] into a list of strings. *)
  type 'a encoder = 'a -> string list

  (** A ['a validator] takes something of type ['a] and returns an error string
      if validation fails, [None] if everything is ok *)
  type 'a validator = 'a -> error_msg option

  (** Use [custom decoder encoder ?default ?type_ ?meta ?validator field_name]
      to create a field with a custom type that is not supported out-of-the box.
      Provide a custom [decoder] with a descriptive error message so conformist
      knows how to turn a string into your custom value.

      A string representation of the static [type_] can also be provided, by
      default the [field_name] is taken.

      A [default] value can be provided. *)
  val custom
    :  'a decoder
    -> 'a encoder
    -> ?default:'a
    -> ?type_:string
    -> ?meta:'b
    -> ?validator:'a validator
    -> string
    -> ('b, 'a) Field.t

  (** [optional ?meta field] turns a [field] into an optional field. If the
      field does not exist in the input data or if the associated value in the
      input data is an empty list, the value is [None]. If the data is not
      provided in the input at all, no validation logic is executed.

      Example:

      {[
        let make name address = { name; address } in
        let schema =
          Conformist.(make [ string "name"; optional (string "address") ] make)
        in
        (* Decoding fails *)
        let decoded = Conformist.decode schema [] in
        (* Validation fails *)
        let validated = Conformist.validate [] in
        (* Decoding succeeds, address is [None] *)
        let decoded = Conformist.decode schema [ "name", [ "Walter" ] ] in
        let decoded =
          Conformist.decode schema [ "name", [ "Walter" ]; "address", [] ]
        in
        (* Validation succeeds *)
        let validated = Conformist.validate [ "name", [ "Walter" ] ] in
        ()
      ]} *)
  val optional : ?meta:'a -> ('b, 'c) Field.t -> ('a, 'c option) Field.t

  (** [list ?default ?meta field] returns a field that decodes to a list of
      [field].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist. *)
  val list
    :  ?default:'c list
    -> ?meta:'a
    -> ('b, 'c) Field.t
    -> ('a, 'c list) Field.t

  (** [bool ?default ?meta ?msg field_name] returns a field with name
      [field_name] that decodes to a [bool].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist.

      [msg] is the decode error message that is returned if {!decode} fails. *)
  val bool
    :  ?default:bool
    -> ?meta:'a
    -> ?msg:error_msg
    -> string
    -> ('a, bool) Field.t

  (** [float ?default ?meta ?msg ?validator field_name] returns a field with
      name [field_name] that decodes to [float].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist.

      [msg] is the decode error message that is returned if {!decode} fails.

      [validator] is an optional validator that is run when calling {!validate}.
      By default, no validation logic is executed. This means that if a value
      decodes, it is valid. *)
  val float
    :  ?default:float
    -> ?meta:'a
    -> ?msg:error_msg
    -> ?validator:float validator
    -> string
    -> ('a, float) Field.t

  (** [int ?meta ?msg ?validator field_name] returns a field with name
      [field_name] that decodes to [int].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist.

      [msg] is the decode error message that is returned if {!decode} fails.

      [validator] is an optional validator that is run when calling {!validate}.
      By default, no validation logic is executed. This means that if a value
      decodes, it is valid. *)
  val int
    :  ?default:int
    -> ?meta:'a
    -> ?msg:error_msg
    -> ?validator:int validator
    -> string
    -> ('a, int) Field.t

  (** [string ?meta ?validator field_name] return a field with name [field_name]
      that decodes to [string].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist.

      [msg] is the decode error message that is returned if {!decode} fails.

      [validator] is an optional validator that is run when calling {!validate}.
      By default, no validation logic is executed. This means that if a value
      decodes, it is valid. *)
  val string
    :  ?default:string
    -> ?meta:'a
    -> ?msg:error_msg
    -> ?validator:string validator
    -> string
    -> ('a, string) Field.t

  (** Don't use [date], use {!datetime} instead.*)
  val date
    :  ?default:Ptime.date
    -> ?meta:'a
    -> ?msg:error_msg
    -> ?validator:(int * int * int) validator
    -> string
    -> ('a, Ptime.date) Field.t
    [@@ocaml.deprecated "Use [Conformist.datetime] instead."]

  (** [datetime ?default ?meta ?validator field_name] returns a field with name
      [field_name] that decodes to [datetime].

      [default] is an optional default value for the field.

      [meta] is optional meta data that is attached to the field. This is useful
      when implementing features on top of conformist.

      [msg] is the decode error message that is returned if {!decode} fails.

      [validator] is an optional validator that is run when calling {!validate}.
      By default, no validation logic is executed. This means that if a value
      decodes, it is valid. *)
  val datetime
    :  ?default:Ptime.t
    -> ?meta:'a
    -> ?msg:error_msg
    -> ?validator:Ptime.t validator
    -> string
    -> ('a, Ptime.t) Field.t

  (** {1 Schema}

      A schema is a list of fields. Input data can be decoded and validated
      using a schema. *)

  (** [t] is a conformist schema. *)
  type ('meta, 'ctor, 'ty) t

  (** [empty] creates an empty schema. *)
  val empty : ('a, unit, unit) t

  (** [make fields constructor] create a schema. *)
  val make : ('a, 'b, 'c) Field.list -> 'b -> ('a, 'b, 'c) t

  (** [fold_left ~f ~init schema] traverses the list of fields of [schema]. Use
      the functions in {!Field} to work with a generic field. *)
  val fold_left
    :  f:('res -> 'meta Field.any_field -> 'res)
    -> init:'res
    -> ('meta, 'args, 'ty) t
    -> 'res

  (** An error [(field, value, error_message)] is used to for decoding errors
      and validation errors.

      [field] is the field name of the input that failed to decode or validate,
      [values] are the input values (if they were provided) and [error_message]
      is the decoding or validation error message.

      An empty list of [error] means that the schema is valid. *)
  type error = string * string list * error_msg

  (** The [input] represents unsafe data that needs to be decoded and validated.
      This is typically some user input. *)
  type input = (string * string list) list

  (** [decode schema input] returns the decoded value of type ['ty] by decoding
      the [input] using the [schema].

      No validation logic is executed in this step. *)
  val decode : ('meta, 'ctor, 'ty) t -> input -> ('ty, error) result

  (** [validate schema input] returns a list of validation errors by running the
      validators defined in [schema] on the [input] data. An empty list implies
      that there are no validation errors and that the input is valid according
      to the schema.

      Note that [input] that has no validation errors might still fail to
      decode, depending on the validation functions specified in [schema]. *)
  val validate : ('meta, 'ctor, 'ty) t -> input -> error list

  (** [decode_and_validate schema input] returns the decoded and validated value
      of type ['ty] by decoding the [input] using the [schema] and running its
      validators.

      Use [decode_and_validate] to combine the functions [decode] and [validate]
      and to either end up with the decoded value or all errors that happened
      during the decoding and validation steps. *)
  val decode_and_validate
    :  ('meta, 'ctor, 'ty) t
    -> input
    -> ('ty, error list) Result.t
end

module type ERROR = sig
  type error

  val invalid_bool : error
  val invalid_float : error
  val invalid_int : error
  val invalid_string : error
  val invalid_date : error
  val invalid_datetime : error
  val no_value : error
  val of_string : string -> error
end

module Make (Error : ERROR) = struct
  type error_msg = Error.error
  type 'a decoder = string list -> ('a, Error.error) result
  type 'a encoder = 'a -> string list
  type 'a validator = 'a -> Error.error option

  let always_valid _ = None

  module Field = struct
    type ('meta, 'a) t =
      { name : string
      ; meta : 'meta option
      ; default : 'a option
      ; decoder : 'a decoder
      ; encoder : 'a encoder
      ; type_ : string
      ; validator : 'a validator
      ; optional : bool
      }

    type (_, _, _) list =
      | [] : ('meta, 'ty, 'ty) list
      | ( :: ) :
          ('meta, 'a) t * ('meta, 'b, 'ty) list
          -> ('meta, 'a -> 'b, 'ty) list

    type _ any_field = AnyField : ('meta, 'a) t -> 'meta any_field

    let meta (AnyField field) = field.meta
    let name (AnyField field) = field.name

    let validate (AnyField field) input =
      match field.decoder input with
      | Ok value -> field.validator value
      | Error msg -> Some msg
    ;;

    let optional (AnyField field) = field.optional
    let is_optional (AnyField field) = field.optional
    let type_ (AnyField field) = field.type_

    let encode_default (AnyField field) : string List.t =
      match field.default with
      | Some v -> field.encoder v
      | None -> []
    ;;

    let make name meta decoder encoder default type_ validator optional =
      { name; meta; default; decoder; encoder; type_; validator; optional }
    ;;

    let make_custom
        decoder
        encoder
        ?default
        ?type_
        ?meta
        ?(validator = always_valid)
        name
      =
      let type_ = Option.value type_ ~default:name in
      make name meta decoder encoder default type_ validator false
    ;;

    let make_optional ?meta field =
      let decoder strings =
        match field.decoder strings, strings with
        (* Decoding succeeds with nothing when no strings provided *)
        | _, [] -> Ok None
        | Ok result, _ -> Ok (Some result)
        | Error msg, _ -> Error msg
      in
      let validator a =
        match a with
        | Some a -> field.validator a
        | None -> None
      in
      let encoder a =
        match a with
        | Some a -> field.encoder a
        | None -> [ "None" ]
      in
      let default =
        match field.default with
        | Some d -> Some (Some d)
        | None -> None
      in
      make field.name meta decoder encoder default field.type_ validator true
    ;;

    let make_list ?default ?meta field =
      let decoder (l : string List.t) : ('a List.t, Error.error) result =
        List.fold_left
          (fun res (el : string) ->
            match res, field.decoder [ el ] with
            | Ok result, Ok el -> Ok (List.cons el result)
            | Ok _, Error msg -> Error msg
            | Error msg, _ -> Error msg)
          (Ok [])
          l
        |> Result.map List.rev
      in
      let validator l =
        List.fold_left
          (fun res el ->
            match res, field.validator el with
            | None, None -> None
            | None, Some msg -> Some msg
            | Some msg, _ -> Some msg)
          None
          l
      in
      let encoder (a : 'a List.t) = List.map field.encoder a |> List.concat in
      make field.name meta decoder encoder default field.type_ validator true
    ;;

    let make_bool ?default ?meta ?(msg = Error.invalid_bool) name =
      let decoder input =
        try Ok (bool_of_string (List.hd input)) with
        | _ -> Error msg
      in
      let encoder input = List.[ string_of_bool input ] in
      make name meta decoder encoder default "bool" always_valid false
    ;;

    let make_float
        ?default
        ?meta
        ?(msg = Error.invalid_float)
        ?(validator = always_valid)
        name
      =
      let decoder string =
        try Ok (float_of_string (List.hd string)) with
        | _ -> Error msg
      in
      let encoder input = List.[ string_of_float input ] in
      make name meta decoder encoder default "float" validator false
    ;;

    let make_int
        ?default
        ?meta
        ?(msg = Error.invalid_int)
        ?(validator = always_valid)
        name
      =
      let decoder string =
        try Ok (int_of_string (List.hd string)) with
        | _ -> Error msg
      in
      let encoder input = List.[ string_of_int input ] in
      make name meta decoder encoder default "int" validator false
    ;;

    let make_string
        ?default
        ?meta
        ?(msg = Error.invalid_string)
        ?(validator = always_valid)
        name
      =
      let decoder input =
        try Ok (List.hd input) with
        | _ -> Error msg
      in
      let encoder id = List.[ id ] in
      make name meta decoder encoder default "string" validator false
    ;;

    let make_date
        ?default
        ?meta
        ?(msg = Error.invalid_date)
        ?(validator = always_valid)
        name
      =
      let decoder input =
        try
          match String.split_on_char '-' (List.hd input) with
          | [ y; m; d ] ->
            (match
               int_of_string_opt y, int_of_string_opt m, int_of_string_opt d
             with
            | Some y, Some m, Some d -> Ok (y, m, d)
            | _ -> Error msg)
          | _ -> Error msg
        with
        | _ -> Error msg
      in
      let encoder (y, m, d) = List.[ Format.sprintf "%d-%d-%d" y m d ] in
      make name meta decoder encoder default "date" validator false
    ;;

    let make_datetime
        ?default
        ?meta
        ?(msg = Error.invalid_datetime)
        ?(validator = always_valid)
        name
      =
      let decoder string =
        try
          match Ptime.of_rfc3339 (List.hd string) with
          | Ok (timestamp, _, _) -> Ok timestamp
          | Error (`RFC3339 (_, _)) -> Error msg
        with
        | _ -> Error msg
      in
      let encoder ptime = List.[ Ptime.to_rfc3339 ptime ] in
      make name meta decoder encoder default "time" validator false
    ;;
  end

  let custom = Field.make_custom
  let optional = Field.make_optional
  let list = Field.make_list
  let bool = Field.make_bool
  let float = Field.make_float
  let int = Field.make_int
  let string = Field.make_string
  let date = Field.make_date
  let datetime = Field.make_datetime

  type ('meta, 'ctor, 'ty) t =
    { fields : ('meta, 'ctor, 'ty) Field.list
    ; ctor : 'ctor
    }

  let empty = { fields = Field.[]; ctor = () }
  let make fields ctor = { fields; ctor }

  let rec fold_left'
      : type ty args.
        f:('res -> 'meta Field.any_field -> 'res)
        -> init:'res
        -> ('meta, args, ty) Field.list
        -> 'res
    =
   fun ~f ~init fields ->
    match fields with
    | [] -> init
    | field :: fields -> fold_left' ~f ~init:(f init (AnyField field)) fields
 ;;

  let fold_left ~f ~init schema = fold_left' ~f ~init schema.fields

  type error = string * string list * Error.error
  type input = (string * string list) list

  let validate
      (schema : ('meta, 'ctor, 'ty) t)
      (input : (string * string list) list)
      : error list
    =
    let f errors field =
      let name = Field.name field in
      match List.assoc name input with
      | values ->
        (match Field.validate field values with
        | Some msg -> List.cons (name, values, msg) errors
        | None -> errors)
      | exception Not_found ->
        (match Field.is_optional field, Field.encode_default field with
        | true, List.[] -> errors
        | false, List.[] -> List.cons (name, [], Error.no_value) errors
        | _, default ->
          (match Field.validate field default with
          | Some msg -> List.cons (name, [], msg) errors
          | None -> errors))
    in
    fold_left ~f ~init:[] schema |> List.rev
  ;;

  let rec decode
      : type meta ctor ty.
        (meta, ctor, ty) t
        -> (string * string list) list
        -> (ty, error) Result.t
    =
   fun { fields; ctor } fields_assoc ->
    let open! Field in
    match fields with
    | [] -> Ok ctor
    | field :: fields ->
      let handle_missing =
        match field.decoder [] with
        | Ok value ->
          (match ctor value with
          | ctor -> decode { fields; ctor } fields_assoc
          | exception exn ->
            let msg = Error.of_string (Printexc.to_string exn) in
            Error (field.name, [], msg))
        | Error msg -> Error (field.name, [], msg)
      in
      (match List.assoc field.name fields_assoc with
      | [] ->
        (match field.default with
        | Some value ->
          (match ctor value with
          | ctor -> decode { fields; ctor } fields_assoc
          | exception exn ->
            let msg = Error.of_string (Printexc.to_string exn) in
            Error (field.name, [], msg))
        | None -> handle_missing)
      | values ->
        (match field.decoder values with
        | Ok value ->
          (match ctor value with
          | ctor -> decode { fields; ctor } fields_assoc
          | exception exn ->
            let msg = Error.of_string (Printexc.to_string exn) in
            Error (field.name, values, msg))
        | Error msg -> Error (field.name, values, msg))
      | exception Not_found ->
        (match field.default, Field.is_optional @@ AnyField field with
        | Some value, _ ->
          (match ctor value with
          | ctor -> decode { fields; ctor } fields_assoc
          | exception exn ->
            let msg = Error.of_string (Printexc.to_string exn) in
            let values =
              match field.default with
              | Some default -> field.encoder default
              | None -> []
            in
            Error (field.name, values, msg))
        | None, false -> Error (field.name, [], Error.no_value)
        | None, true -> handle_missing))
 ;;

  let decode_and_validate schema input =
    let validation_errors = validate schema input in
    match decode schema input, validation_errors with
    | Ok value, [] -> Ok value
    | Ok _, validation_errors -> Error validation_errors
    | Error (field_name, value, msg), validation_errors ->
      validation_errors
      |> List.filter (fun (name, _, _) -> not (String.equal name field_name))
      |> List.cons (field_name, value, msg)
      |> Result.error
  ;;
end
OCaml

Innovation. Community. Security.