@colisweb/rescript-toolkit 4.8.1 → 4.9.0
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
package/src/form/ReSchema.res
CHANGED
|
@@ -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
|
}
|
package/src/form/Reform.res
CHANGED
|
@@ -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) =>
|
|
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
|
}
|
|
@@ -180,7 +181,7 @@ module Make = (Config: Config) => {
|
|
|
180
181
|
~onSubmit,
|
|
181
182
|
~onSubmitFail=ignore,
|
|
182
183
|
~i18n=?,
|
|
183
|
-
~validationStrategy=
|
|
184
|
+
~validationStrategy=OnChange,
|
|
184
185
|
(),
|
|
185
186
|
) => {
|
|
186
187
|
let intl = useIntl()
|
|
@@ -257,6 +258,8 @@ module Make = (Config: Config) => {
|
|
|
257
258
|
field,
|
|
258
259
|
(Valid: fieldState),
|
|
259
260
|
))
|
|
261
|
+
|
|
262
|
+
self.send(SetFormState(Valid))
|
|
260
263
|
self.send(SetFieldsState(newFieldsState))
|
|
261
264
|
submit ? self.send(Submit) : ()
|
|
262
265
|
}
|
|
@@ -332,16 +335,25 @@ module Make = (Config: Config) => {
|
|
|
332
335
|
),
|
|
333
336
|
})
|
|
334
337
|
| FieldArrayUpdateByIndex(field, value, index) =>
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
i
|
|
338
|
+
UpdateWithSideEffects(
|
|
339
|
+
{
|
|
340
|
+
...state,
|
|
341
|
+
values: Config.set(
|
|
342
|
+
state.values,
|
|
343
|
+
field,
|
|
344
|
+
Config.get(state.values, field)->Belt.Array.mapWithIndex((i, currentValue) =>
|
|
345
|
+
i == index ? value : currentValue
|
|
346
|
+
),
|
|
342
347
|
),
|
|
343
|
-
|
|
344
|
-
|
|
348
|
+
},
|
|
349
|
+
self => {
|
|
350
|
+
switch validationStrategy {
|
|
351
|
+
| OnChange => self.send(ValidateField(Field(field)))
|
|
352
|
+
| OnDemand => ()
|
|
353
|
+
}
|
|
354
|
+
None
|
|
355
|
+
},
|
|
356
|
+
)
|
|
345
357
|
| SetFormState(newState) => Update({...state, formState: newState})
|
|
346
358
|
| ResetForm =>
|
|
347
359
|
Update({
|
|
@@ -389,12 +401,17 @@ module Make = (Config: Config) => {
|
|
|
389
401
|
}
|
|
390
402
|
)
|
|
391
403
|
|
|
392
|
-
let getNestedFieldError = (field, index) =>
|
|
404
|
+
let getNestedFieldError = (field, subfield, index) =>
|
|
393
405
|
switch getFieldState(field) {
|
|
394
|
-
| NestedErrors(errors) =>
|
|
406
|
+
| NestedErrors(errors) =>
|
|
407
|
+
errors
|
|
408
|
+
->Array.getBy(error => error.index === index && error.name === subfield)
|
|
409
|
+
->Option.map(({error}) => error)
|
|
410
|
+
|
|
395
411
|
| Pristine
|
|
396
412
|
| Valid
|
|
397
|
-
| Error(_) =>
|
|
413
|
+
| Error(_) =>
|
|
414
|
+
None
|
|
398
415
|
}
|
|
399
416
|
|
|
400
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
|
-
|
|
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) ||
|
|
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) ||
|
|
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
|
}
|