Create validations by iterating over an array #851
-
I have an array of password validations that came from the BE when the app starts, the array is something like this: [
{
"id": 0,
"mensagem": "Deve conter pelo menos 8 caracteres",
"descricao": "8 caracteres",
"valor": "/^.{8,}$/",
"estado": {
"id": 2,
"codigo": null,
"label": "Ativo"
}
},
{
"id": 1,
"mensagem": "Deve conter pelo menos 1 letra minúscula",
"descricao": "1 minúscula",
"valor": "/(?=.*[a-z])/",
"estado": {
"id": 2,
"codigo": null,
"label": "Ativo"
}
},
{
"id": 2,
"mensagem": "Deve conter pelo menos 1 letra maiúscula",
"descricao": "1 maiúscula",
"valor": "/(?=.*[A-Z])/",
"estado": {
"id": 2,
"codigo": null,
"label": "Ativo"
}
},
{
"id": 3,
"mensagem": "Deve conter pelo menos 1 número",
"descricao": "1 número",
"valor": "/(?=.*\\d)/",
"estado": {
"id": 2,
"codigo": null,
"label": "Ativo"
}
},
{
"id": 4,
"mensagem": "Deve conter pelo menos 1 caracter especial (@$!%*?&)",
"descricao": "1 caracter especial",
"valor": "/(?=.*[@$!%*?&])/",
"estado": {
"id": 2,
"codigo": null,
"label": "Ativo"
}
}
] It would be nice if I could iterate over it and return one validation for each, like: export const PasswordSchema = v.pipe(
v.string('required.password'),
v.nonEmpty('required.password'),
...usePasswordRulesStore
.getState()
.rules.filter((rule) => rule.valor)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.map((rule) => v.regex(new RegExp(rule.valor!.replaceAll('/', '')), rule.mensagem)),
); is that possible? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Related to #643 |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, since we are not exposing a rest parameter argument via the overload signatures of |
Beta Was this translation helpful? Give feedback.
Unfortunately, since we are not exposing a rest parameter argument via the overload signatures of
pipe
, TS will complain. Feel free to create a separate issue for this use case. I will see if we can make this possible. In the meantime, you can usecheck
orrawCheck
as a workaround. You could also tell/force TS that[].map(...)
returns[v.RegexAction<string, string>]
.