fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.33k stars 204 forks source link

Expose `pipe` rest parameter argument type #852

Closed ruiaraujo012 closed 2 weeks ago

ruiaraujo012 commented 1 month ago

Discussed in https://github.com/fabian-hiller/valibot/discussions/851

Originally posted by **ruiaraujo012** September 26, 2024 I have an array of password validations that came from the BE when the app starts, the array is something like this: ```js [ { "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: ```ts 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?
fabian-hiller commented 1 month ago

Thank you for creating this issue. I will try to investigate in the next few weeks if we can support such a use case.

ruiaraujo012 commented 1 month ago

Thank you.

It's not an important feature to me, it's more of a nice to have. But it would be nice.

fabian-hiller commented 3 weeks ago

This will be possible 🔜

ruiaraujo012 commented 3 weeks ago

Thank you.

fabian-hiller commented 2 weeks ago

v1.0.0-beta.4 is available

ruiaraujo012 commented 2 weeks ago

TS still complains about it A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)

I'm forced to use as [v.RegexAction<string, string>]

fabian-hiller commented 1 week ago

Can you share a playground link?

ruiaraujo012 commented 1 week ago

Well, I'm unable to replicate, so it could be something with my codebase. If I manage to replicate, I'll share it here.

Thank you.

fabian-hiller commented 1 week ago

Okay. Because I have tested different use cases. As long as the array or action output is typed correctly, it should work. Note that it only works if the data type stays the same. Transformations are not allowed because they can't be tracked with TypeScript in this way.

ruiaraujo012 commented 1 week ago

Yes, it's probably something with my type definitions or because I'm using the return type of the zustand store.