firstName: yup .string() .test( 'len', 'can be empty or with string at least 2 characters and not more than 10', (val) => val != undefined && (val.length == 0 || (val.length >= 2 && val.length <= 10) ) ) en este caso, la longitud min 2 y max 10 funciona, pero cuando está vacío se marca con un error, lo intenté con val.length == 0
arreglado por cheque indefinido separado
firstName: yup .string() .test( 'len', 'can be empty or with string at least 2 characters and not more than 10', (val) => { if (val == undefined) { return true; } return ((val.length == 0 || (val.length >= 2 && val.length <= 10))) } ),hola se puede hacer asi
const yup = require("yup"); const firstName = ""; const schema = yup .string() .test( "len", "can be empty or with string at least 2 characters and not more than 10", (val) => { if (val === undefined) { return true; } return val.length === 0 || (val.length >= 2 && val.length <= 10); } ); schema .validate(firstName) .then((response) => console.log(response)) .catch((err) => console.log(err));