@colisweb/rescript-toolkit 4.8.2 → 4.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "4.8.2",
3
+ "version": "4.9.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -36,6 +36,10 @@ module Make = (Lenses: Lenses) => {
36
36
  | FloatMin({field: Lenses.field<float>, min: float, error: option<string>}): t
37
37
  | FloatMax({field: Lenses.field<float>, max: float, error: option<string>}): t
38
38
  | Custom({field: Lenses.field<'a>, predicate: Lenses.state => fieldState}): t
39
+ | CustomNestedSchema({
40
+ field: Lenses.field<'a>,
41
+ predicate: Lenses.state => array<(int, recordValidationState<'a>)>,
42
+ }): t
39
43
  | True({field: Lenses.field<bool>, error: option<string>}): t
40
44
  | False({field: Lenses.field<bool>, error: option<string>}): t
41
45
  | OptionNonEmpty({field: Lenses.field<option<'a>>, error: option<string>}): t
@@ -56,6 +60,10 @@ module Make = (Lenses: Lenses) => {
56
60
 
57
61
  let custom = (field, predicate) => [Custom({field, predicate})]
58
62
 
63
+ let customNestedSchema = (field, predicate) => {
64
+ [CustomNestedSchema({field, predicate})]
65
+ }
66
+
59
67
  let optionNonEmpty = (~error=?, field) => [OptionNonEmpty({field, error})]
60
68
 
61
69
  let arrayNonEmpty = (~error=?, field) => [ArrayNonEmpty({field, error})]
@@ -216,7 +224,33 @@ module Make = (Lenses: Lenses) => {
216
224
  : Error(error->Belt.Option.getWithDefault(i18n.stringMax(~value, ~max))),
217
225
  )
218
226
  | Validation.Custom({field, predicate}) => (Field(field), predicate(values))
227
+ | Validation.CustomNestedSchema({field, predicate}) => {
228
+ let results = predicate(values)
219
229
 
230
+ let errors =
231
+ results
232
+ ->Array.map(((index, result)) => {
233
+ switch result {
234
+ | Errors(errors) =>
235
+ errors->Array.map(((fieldName, error)) => {
236
+ switch (fieldName->Obj.magic, error) {
237
+ | (Field(name), Error(e)) =>
238
+ Some({
239
+ error: e,
240
+ index,
241
+ name: name->Obj.magic,
242
+ })
243
+ | _ => None
244
+ }
245
+ })
246
+ | Valid => []
247
+ }
248
+ })
249
+ ->Array.reduce([], (acc, el) => acc->Array.concat(el))
250
+ ->Array.keepMap(v => v)
251
+
252
+ (Field(field)->Obj.magic, errors->Array.length > 0 ? NestedErrors(errors) : Valid)
253
+ }
220
254
  | Validation.OptionNonEmpty({field, error}) => {
221
255
  let value = Lenses.get(values, field)
222
256
 
@@ -255,6 +289,7 @@ module Make = (Lenses: Lenses) => {
255
289
  | Validation.StringMin({field}) => Field(field) == fieldName
256
290
  | Validation.StringMax({field}) => Field(field) == fieldName
257
291
  | Validation.Custom({field}) => Field(field) == fieldName
292
+ | Validation.CustomNestedSchema({field}) => Field(field) == fieldName
258
293
  | Validation.OptionNonEmpty({field}) => Field(field) == fieldName
259
294
  | Validation.ArrayNonEmpty({field}) => Field(field) == fieldName
260
295
  }
@@ -276,6 +311,7 @@ module Make = (Lenses: Lenses) => {
276
311
  | Validation.StringMin({field}) => Field(field) == fieldName
277
312
  | Validation.StringMax({field}) => Field(field) == fieldName
278
313
  | Validation.Custom({field}) => Field(field) == fieldName
314
+ | Validation.CustomNestedSchema({field}) => Field(field) == fieldName
279
315
  | Validation.OptionNonEmpty({field}) => Field(field) == fieldName
280
316
  | Validation.ArrayNonEmpty({field}) => Field(field) == fieldName
281
317
  }
@@ -59,7 +59,7 @@ module Make = (Config: Config) => {
59
59
  state: state,
60
60
  getFieldState: field => fieldState,
61
61
  getFieldError: field => option<string>,
62
- getNestedFieldError: (field, int) => array<childFieldError>,
62
+ getNestedFieldError: (field, string, int) => option<string>,
63
63
  handleChange: 'a. (Config.field<'a>, 'a) => unit,
64
64
  arrayPush: 'a. (Config.field<array<'a>>, 'a) => unit,
65
65
  arrayUpdateByIndex: 'a. (~field: Config.field<array<'a>>, ~index: int, 'a) => unit,
@@ -114,6 +114,7 @@ module Make = (Config: Config) => {
114
114
  | Validation.StringMin({field}) => (Field(field), Pristine)
115
115
  | Validation.StringMax({field}) => (Field(field), Pristine)
116
116
  | Validation.Custom({field}) => (Field(field), Pristine)
117
+ | Validation.CustomNestedSchema({field}) => (Field(field), Pristine)
117
118
  | Validation.OptionNonEmpty({field}) => (Field(field), Pristine)
118
119
  | Validation.ArrayNonEmpty({field}) => (Field(field), Pristine)
119
120
  }
@@ -400,12 +401,17 @@ module Make = (Config: Config) => {
400
401
  }
401
402
  )
402
403
 
403
- let getNestedFieldError = (field, index) =>
404
+ let getNestedFieldError = (field, subfield, index) =>
404
405
  switch getFieldState(field) {
405
- | NestedErrors(errors) => errors->Array.keep(error => error.index === index)
406
+ | NestedErrors(errors) =>
407
+ errors
408
+ ->Array.getBy(error => error.index === index && error.name === subfield)
409
+ ->Option.map(({error}) => error)
410
+
406
411
  | Pristine
407
412
  | Valid
408
- | Error(_) => []
413
+ | Error(_) =>
414
+ None
409
415
  }
410
416
 
411
417
  let validateFields = (fields: array<field>) => {
@@ -7,7 +7,7 @@ module Msg = {
7
7
  let requiredPosIntOrFloat = {
8
8
  defaultMessage: "Doit être un entier positif ou un nombre a virgule",
9
9
  }
10
- let requiredRatio = {
10
+ let requiredRatio = {
11
11
  defaultMessage: "Doit être un nombre compris entre 0 et 1 en excluant 0 (exemples: '0.1' '0.85' '1')",
12
12
  }
13
13
  let wrongFormat = {defaultMessage: "Mauvais format (req: {exemple})"}
@@ -102,7 +102,8 @@ let requiredStrictPosFloat = (intl, value) => {
102
102
  switch value {
103
103
  | "" => Some(Intl.formatMessage(intl, Msg.requiredValue))
104
104
  | value
105
- if !Toolkit__Utils_Regex.Test.isPositiveIntOrFloat(value) || value->Float.fromString === Some(0.) =>
105
+ if !Toolkit__Utils_Regex.Test.isPositiveIntOrFloat(value) ||
106
+ value->Float.fromString === Some(0.) =>
106
107
  Some(Intl.formatMessage(intl, Msg.requiredPosIntOrFloat))
107
108
  | _ => None
108
109
  }
@@ -112,7 +113,9 @@ let requiredRatio = (intl, value) => {
112
113
  switch value {
113
114
  | "" => Some(Intl.formatMessage(intl, Msg.requiredValue))
114
115
  | value
115
- if !Toolkit__Utils_Regex.Test.isPositiveIntOrFloat(value) || value->Float.fromString === Some(0.) || value->Float.fromString->Option.getWithDefault(2.) > 1. =>
116
+ if !Toolkit__Utils_Regex.Test.isPositiveIntOrFloat(value) ||
117
+ value->Float.fromString === Some(0.) ||
118
+ value->Float.fromString->Option.getWithDefault(2.) > 1. =>
116
119
  Some(Intl.formatMessage(intl, Msg.requiredRatio))
117
120
  | _ => None
118
121
  }
@@ -21,6 +21,10 @@ module String = {
21
21
  ->Js.String2.normalizeByForm("NFD")
22
22
  ->Js.String2.replaceByRe(%re("/[\u0300-\u036f]/g"), "")
23
23
  }
24
+
25
+ let includes = (str, search) => {
26
+ str->normalizeForSearch->Js.String2.includes(search)
27
+ }
24
28
  }
25
29
 
26
30
  module Option = {
@@ -91,6 +91,7 @@ module ReactDayPicker = {
91
91
  ~firstDayOfWeek: 'b=?,
92
92
  ~months: 'a=?,
93
93
  ~numberOfMonths: int=?,
94
+ ~initialMonth: Js.Date.t=?,
94
95
  ~showOutsideDays: bool=?,
95
96
  ) => React.element = "DayPicker"
96
97
  }