-
Notifications
You must be signed in to change notification settings - Fork 32
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 compare two values? #101
Comments
Hi! |
This works great! I didn't know about it either until I cloned the repo and started trying to play with the code and I ran across those unit tests. Is it possible that something like that could make its way into the docs too? I ended up referring a co-worker to a workaround about a month ago because I didn't realized spected has that feature. |
Maybe a basic suggestion description pulling code directly out of the tests: A predicate function for any of the rules will also receive the entire input object as a second parameter, allowing you to access other values at the same time. An example of this might be for password confirmation, since both values (password and the repeated password field) need to match. // some validation helpers
const minimumMsg = (field, len) => `Minimum ${field} length of ${len} is required.`
const capitalLetterMag = field => `${field} should contain at least one uppercase letter.`
const isEqual = compareKey => (val, allValues) => val === allValues[compareKey]
const equalMsg = (field1, field2) => `${field2} should be equal with ${field1}`
const validationRules = {
password: [
[isLengthGreaterThan(5), minimumMsg('Password', 6)],
[hasCapitalLetter, capitalLetterMag('Password')],
],
repeatPassword: [
[isLengthGreaterThan(5), minimumMsg('RepeatedPassword', 6)],
[hasCapitalLetter, capitalLetterMag('RepeatedPassword')],
[isEqual('password'), equalMsg('Password', 'RepeatPassword')],
]
}
const result = spected(validationRules, {password: 'fooBar', repeatPassword: 'fooBarBaz'})
deepEqual({
password: true,
repeatPassword: [equalMsg('Password', 'RepeatPassword')]
}, result) |
Yes, that's a good point. Will update the documentation. Thanks for the valuable input! |
Hi,
How do you validate if two fields are equal, for example fields password and confirm_password?
Can you provide an example?
The text was updated successfully, but these errors were encountered: