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

[DRAFT] feature: initial proposal of 'is not null' #3

Merged
merged 4 commits into from
Dec 19, 2024

Conversation

wellgenio
Copy link
Collaborator

No description provided.

@wellgenio wellgenio self-assigned this Nov 18, 2024
@jacobaraujo7
Copy link
Contributor

Como que ficou esse PR?

@wellgenio
Copy link
Collaborator Author

wellgenio commented Dec 15, 2024

vou da continuidade. Mas a proposta seria o seguinte:

final validator = TestLucidValidator<UserModel>();

    validator
        .ruleFor((e) => e.cpf, key: 'cpf') // cpf is nullable [String?]
        .validCPF();

final user = UserModel()..cpf = null;

final result = validator.validate(user); // is not valid

vou add suporte a nullable no Lucid, nesse caso acima .validCPF<String?>

final validator = TestLucidValidator<UserModel>();

    validator
        .ruleFor((e) => e.cpf, key: 'cpf') // cpf is nullable [String?]
        .when((e) => e.cpf != null)
        .notEmpty() // not applied, if cpf is null
        .validCPF(); // not applied, if cpf is null

final user = UserModel()..cpf = null;

final result = validator.validate(user); // is valid

nesse caso acima com o when, ele é valido em 2 cenarios validos: Ou cpf is null(Valida) ou ele é um cpf is valid (Valida)

final validator = TestLucidValidator<UserModel>();

    validator
        .ruleFor((e) => e.cpf, key: 'cpf') // cpf is nullable [String?]
        .isNotNull() // property type promotion: cpf is not nullable [String]
        .validCPF();

final user = UserModel()..cpf = null;

final result = validator.validate(user); // is not valid

E agora vai ter "promoção":

Aqui e.cpf é nullable (String?), a partir do .isNotNull() , o e.cpf é do tipo String não nullable.

também adicinei o OrNull para praticamente todas as validações:

final validator = TestLucidValidator<UserModel>();

    validator
        .ruleFor((e) => e.cpf, key: 'cpf') // cpf is nullable [String?]
        .validCPFOrNull();

final user = UserModel()..cpf = null;

final result = validator.validate(user); // is valid

@wellgenio
Copy link
Collaborator Author

@jacobaraujo7 ta pronto

@jacobaraujo7
Copy link
Contributor

#2

@jacobaraujo7 jacobaraujo7 merged commit 5716ae2 into main Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants