summaryrefslogtreecommitdiff
path: root/lib/configuration/read_conf.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-18 20:24:52 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-18 21:57:48 +0100
commit62123e577ee5d4066d53f8ad7e5ac3d2eb190430 (patch)
treef569686630681a9eceb2685f69b55fe5a2bac17c /lib/configuration/read_conf.ml
parent7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (diff)
Securized the queries in the database with a dedicated type for the Identifiers
Diffstat (limited to 'lib/configuration/read_conf.ml')
-rw-r--r--lib/configuration/read_conf.ml48
1 files changed, 27 insertions, 21 deletions
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index 66cd1fe..4150295 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -7,10 +7,12 @@ module Make (S : Decoders.Decode.S) = struct
(** Recursive type declaring a content and the subcontents *)
type declared_content =
| C of
- string * (S.value * string) Model.Section.t list * declared_content list
+ Model.Identifier.t
+ * (S.value * Model.Identifier.t) Model.Section.t list
+ * declared_content list
let rec content_builder :
- declared_content -> (S.value * string) Model.Content.t =
+ declared_content -> (S.value * Model.Identifier.t) Model.Content.t =
fun c ->
let (C (content_id, sections, declared_subcontents)) = c in
let children = List.map declared_subcontents ~f:content_builder in
@@ -31,8 +33,17 @@ module Make (S : Decoders.Decode.S) = struct
let dsl = Parser.ExpressionParser.of_string content in
Result.map_error Decoders.Error.make dsl |> S.from_result
- let field_type : (S.value * string) Model.Field.field_ex S.decoder =
- let text_type : (S.value * string) Model.Field.field_ex S.decoder =
+ let identifier : Model.Identifier.t S.decoder =
+ let* id = S.string in
+ if String.equal String.empty id then
+ S.fail "Expected a non empty identifier"
+ else
+ match Model.Identifier.of_string_opt id with
+ | Some id -> S.succeed id
+ | None -> S.fail "Expected an identifier with only the accepted characters"
+
+ let field_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ let text_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
S.and_then
(fun value ->
match String.lowercase_ascii value with
@@ -40,8 +51,8 @@ module Make (S : Decoders.Decode.S) = struct
| "integer" -> S.succeed (Model.Field.T Integer)
| other -> S.fail other)
S.string
- and content : (S.value * string) Model.Field.field_ex S.decoder =
- let* content_t = keep @@ S.field "content" S.string
+ and content : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ let* content_t = keep @@ S.field "content" identifier
and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool
and* constraints = S.field_opt "constraints" dsl in
@@ -62,8 +73,8 @@ module Make (S : Decoders.Decode.S) = struct
let* rule = S.field "rule" dsl and* message = S.field "message" textI18n in
S.succeed Model.Field.{ rule; message }
- let field : (S.value * string) Model.Field.t S.decoder =
- let* name = S.field "name" S.string
+ let field : (S.value * Model.Identifier.t) Model.Field.t S.decoder =
+ let* name = S.field "name" identifier
and* label = S.field "label" textI18n
and* type' = S.field "type" field_type
and* required = S.field_opt_or ~default:false "required" S.bool
@@ -76,22 +87,14 @@ module Make (S : Decoders.Decode.S) = struct
(Model.Field.create type' label ~required ?placeholder ~validation
?criteria ?icon name)
- let section : (S.value * string) Model.Section.t S.decoder =
+ let section : (S.value * Model.Identifier.t) Model.Section.t S.decoder =
let* label = S.field_opt_or ~default:[] "label" textI18n
and* criteria = S.field_opt "criteria" dsl
and* fields = S.field "field" (S.list field) in
let icon = None in
S.succeed Model.Section.{ label; criteria; fields; icon }
- let identifier : string S.decoder =
- let* id = S.string in
- if String.equal String.empty id then
- S.fail "Expected a non empty identifier"
- else if String.contains id '.' then
- S.fail "Expected an identifier with only the accepted characters"
- else S.succeed id
-
- let content : (S.value * string) Model.Content.t S.decoder =
+ let content : (S.value * Model.Identifier.t) Model.Content.t S.decoder =
(* Build a temporary type without the parent information *)
let* c =
S.fix (fun content_decoder ->
@@ -130,7 +133,7 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Undefined target \"" ^ name
+ "Undefined target \"" ^ Model.Identifier.to_string name
^ "\". Expected existing list or content"
in
S.fail_with (Decoders.Error.make ~context message))
@@ -139,12 +142,15 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Unsupported target \"" ^ name
+ "Unsupported target \"" ^ Model.Identifier.to_string name
^ "\". Lists as target are not implemented yet"
in
S.fail_with (Decoders.Error.make ~context message))
| Error (Graph_tree.Cycle contents) ->
- let content_name = List.map contents ~f:(fun c -> Model.Content.id c) in
+ let content_name =
+ List.map contents ~f:(fun c ->
+ Model.Identifier.to_string (Model.Content.id c))
+ in
let message =
String.concat ~sep:" "
("Dependency circle found between:" :: content_name)