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

How to achieve Password matching? #35

Closed
ronjunevaldoz opened this issue Mar 8, 2022 · 5 comments
Closed

How to achieve Password matching? #35

ronjunevaldoz opened this issue Mar 8, 2022 · 5 comments

Comments

@ronjunevaldoz
Copy link

No description provided.

@ronjunevaldoz ronjunevaldoz changed the title How to Password matching? How to achieve Password matching? Mar 8, 2022
@cies
Copy link

cies commented Apr 18, 2022

I hope this helps:

    val validator = Validation<SignupFormDto> {
      SignupFormDto::password required {}
      SignupFormDto::confirmPassword required {}

      val checkPasswordsMatch = Validation<SignupFormDto> {
        addConstraint("Passwords should match") {
          if (it.password == null || it.confirmPassword == null) {
            true // dont whine about non matching passwords when not filled in yet
          } else {
            it.password == it.confirmPassword
          }
        }
      }
      SignupFormDto::discountedPlanPriceUntil {
        run(checkPasswordsMatch)
      }
    }

I've not tested this, but I hope this helps you. Sorry for the late response, I've only just learned about this library.

@nlochschmidt
Copy link
Member

@ronjunevaldoz did this answer your question?

Note that the snippet suffers from the issue described in #37.

Also, the addConstraint can be directly applied to the outer Validation<SignupFormDto>. This means it can be simplified to:

data class SignupFormDto(val password: String?, val confirmPassword: String?)

val validator = Validation<SignupFormDto> {
    SignupFormDto::password required {}
    SignupFormDto::confirmPassword required {}

    addConstraint("Passwords should match") {
        if (it.password == null || it.confirmPassword == null) {
            true // dont whine about non matching passwords when not filled in yet
        } else {
            it.password == it.confirmPassword
        }
    }
}

println(validator(SignupFormDto("Secret", "NoSecret")))
// Invalid(errors=[ValidationError(dataPath=, message=Passwords should match)])

println(validator(SignupFormDto("Secret", "Secret")))
// Valid(value=SignupFormDto(password=Secret, confirmPassword=Secret))

@nlochschmidt
Copy link
Member

Closing because of inactivity.

@ronjunevaldoz
Copy link
Author

 addConstraint("Passwords should match") {
        if (it.password == null || it.confirmPassword == null) {
            true // dont whine about non matching passwords when not filled in yet
        } else {
            it.password == it.confirmPassword
        }

in the above example it does't show dataPath value.

I wanted to show the error for confirmPassword dataPath

 CreateBasicAccountInput::confirmPassword required { 
        addConstraint("Passwords should match") {
            it.password == it.confirmPassword
        }
    }

@dhoepelman
Copy link
Collaborator

@ronjunevaldoz see #29

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

No branches or pull requests

4 participants