package atdgen

  1. Overview
  2. Docs

Source file ag_validate.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
(*
  Mapping from ATD to "validate"
*)

open Printf

type validate_repr = (string option * bool)
    (* (opt_v, b)
       is obtained by analyzing all available type definitions.
       The first value opt_v is the optional local validator
       coming from an ATD annotation (see `Local).
       The second value b is true iff the data doesn't need scanning.

       There are four cases:
       opt_v = None && b = true => no validation is needed at all
       opt_v = None && b = false => validators must be called on some
                                    sub-fields of the data
       opt_v <> None && b = true => the given validator must be called
                                    but there's no need to look into
                                    the sub-fields
       opt_v <> None && b = false => the given validator must be called
                                     in addition to scanning sub-fields
    *)

let make_full_validator s =
  sprintf "\
    fun path x -> \
      if ( %s ) x then None \
      else Some (Ag_util.Validation.error path)"
    s

let get_validator an =
  let full =
    Atd_annot.get_field (fun s -> Some (Some s)) None
      ["ocaml"] "validator" an
  in
  match full with
  | Some _ -> full
  | None ->
      let shorthand =
        Atd_annot.get_field (fun s -> Some (Some s)) None
          ["ocaml"] "valid" an
      in
      match shorthand with
      | None -> None
      | Some s -> Some (make_full_validator s)
OCaml

Innovation. Community. Security.