Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Yup accepts a confirmPassword that does not match, or never checks it

Errorvalidation passes when confirmPassword is different from password

Short 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.

  • Applies to Node.js 22, yup 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
    "
  2. 2Match what you see, then jump to its fix

    1. 1The payload without confirmPassword is validundefined passes oneOfFix
    2. 2Every confirmPassword is rejected, or any value equal to some fixed string passesThe rule compares against a literal instead of a referenceFix

What does the error look like?

  • validation passes when confirmPassword is different from password
  • validation 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.js

2Fix

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 again

Confirmed by github.com

Did this fix it?

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.js

2Fix

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 again

Confirmed by github.com

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.