Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose pipe rest parameter argument type #852

Closed
ruiaraujo012 opened this issue Sep 26, 2024 Discussed in #851 · 10 comments
Closed

Expose pipe rest parameter argument type #852

ruiaraujo012 opened this issue Sep 26, 2024 Discussed in #851 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@ruiaraujo012
Copy link

Discussed in #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:

[
    {
        "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?

@fabian-hiller fabian-hiller self-assigned this Oct 2, 2024
@fabian-hiller fabian-hiller added the enhancement New feature or request label Oct 2, 2024
@fabian-hiller
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

This will be possible 🔜

@ruiaraujo012
Copy link
Author

Thank you.

@fabian-hiller
Copy link
Owner

v1.0.0-beta.4 is available

@ruiaraujo012
Copy link
Author

ruiaraujo012 commented Nov 8, 2024

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
Copy link
Owner

Can you share a playground link?

@ruiaraujo012
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants