Yup accepts a confirmPassword that does not match, or never checks it
validation passes when confirmPassword is different from passwordShort answer
Compare the field to its sibling with oneOf([ref('password')]). On its own, oneOf lets undefined pass, so an omitted confirmPassword is valid. Add required() to close that gap, and check that the object schema wraps both fields.
Fix it fast
1Find out which cause you have
Validate three payloads against your schema: matching, different and missing. The result of each tells you which cause applies.
node -e " const { object, string, ref } = require('yup'); const s = object({ password: string().required(), confirmPassword: string().oneOf([ref('password')]) }); for (const v of [{password:'a',confirmPassword:'a'},{password:'a',confirmPassword:'b'},{password:'a'}]) s.isValid(v).then(r => console.log(JSON.stringify(v), r)); "2Match what you see, then jump to its fix
What does the error look like?
validation passes when confirmPassword is different from passwordvalidation passes when confirmPassword is missing from the request
Cause 1 of 2
undefined passes oneOf
1Check
The Yup docs state that undefined does not fail oneOf even when it is not in the list. The first step prints true for the payload with no confirmPassword.
grep -n "confirmPassword" src/schema.js2Fix
Chain required() after oneOf. For strings, required also rejects an empty string.
confirmPassword: string()
.oneOf([ref('password')], 'Passwords must match')
.required('Confirm your password'),3Verify
Run the three payloads again. Only the matching one should be true.
# run the first-step command againConfirmed by github.com
Cause 2 of 2
The rule compares against a literal instead of a reference
1Check
A plain string in oneOf is compared as a fixed value. A reference to the sibling field is built with ref('password'), and it is resolved at validation time.
grep -n "oneOf" src/schema.js2Fix
Import ref and give oneOf the reference, not the field name or a variable captured earlier.
import { object, string, ref } from 'yup';
const schema = object({
password: string().required(),
confirmPassword: string().oneOf([ref('password')]).required(),
});3Verify
Validate a matching pair and a different pair.
# run the first-step command againConfirmed by github.com
How do I stop it coming back?
- Write a test with matching, different and missing confirmPassword for every sign-up schema.
- Validate the whole object, not each field alone, so ref can see its sibling.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix