Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
how to evaluate the field itself as optional without adding additional boolean value for yup evaluate shape on an optional field?

I saw available solution like Yup: How to validate field only when it exists? it requires adding additional boolean to the existing shape, which I do not want to do.

Is there way I can simply evaluate the field itself?

I created a sandbox: https://codesandbox.io/s/yup-date-validation-test-optional-self-check-906iz?file=/src/index.js

import * as yup from "yup";

const optionalCheck = yup.string().when("nickName", {
  is: (value) => value?.length,
  then: (rule) =>
    rule.matches(
      /^[aA-zZ\s]+$/,
      `this can only have characters, no digits or symbols.`
    )
});

const schema = yup.object().shape({
  nickName: optionalCheck,
  firstName: yup.string().required("required")
});

schema
  .validate({ nickName: "11-11-1111", firstName: "aaa" }, { abortEarly: false })
  .catch((err) => {
    console.log(JSON.stringify(err, null, 4));
    console.log("-----");
    console.log(err.message);
  })
  .then(() => console.log("valid1"));

I will always get Error: Cyclic dependency, node was:"nickName"

Any suggestions?

UPDATE

I found another solution! now it works! Yup validation for a non-required field

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!