Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda