I’m creating an form with three fields. The first (driver) is required, so it’s easy to do it with yup. But for the others 2 fields (fiscalNote & reqNumber) at least one of them should be required. Example: if “fiscalNote” is empty “reqNumber” should be required, and if “redNumber” is empty “fiscalNote” should be required.
I’ve tried a lot of things, but I’m getting every types of errors, like: “Error: Cyclic dependency, node was:”reqNumber”, js engine: hermes”.
I also tried add an Array with the fields name in the yup shape but I think the newest versions don’t has supports. That is my code now:
import * as yup from "yup"
import { yupResolver } from "@hookform/resolvers/yup"
type FirstStepFields = {
driver: string,
fiscalNote?: string,
reqNumber?: string
}
const firstStepFieldsSchema = yup.object().shape({
driver: yup.string().required("Error message"),
fiscalNote: yup.string().notRequired().when('reqNumber', {
is: (reqNumber: string) => !reqNumber || reqNumber.length === 0,
then: (schema) => schema.required("Error message")
}),
reqNumber: yup.string().notRequired().when('fiscalNote', {
is: (fiscalNote: string) => !fiscalNote || fiscalNote.length === 0,
then: (schema) => schema.required("Insira uma Nota Fiscal ou um N° de Requisição")
}),
})
const { control, handleSubmit, formState: {errors} } = useForm<FirstStepFields>({
resolver: yupResolver<FirstStepFields>(firstStepFieldsSchema)
})
yup version:
"yup": "^1.4.0",
"react": "18.2.0",
"react-native": "0.73.6",