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

213
Views
Yup not validating when conditions are met

I have additional fields that only gets shown if a checkbox is selected (Boolean value is true). These additional fields also need validation but using the .when() method doesn't seem to react to the checkbox being true as a condition. The validation part of my code looks like:

  const schema = useMemo(
    () =>
      yup
        .object()
        .shape({
          type: yup
            .string()
            .label(translate('Forms.EmailForm.type.label'))
            .required(),
          person: yup
            .object()
            .shape({
              firstName: yup
                .string()
                .required()
                .trim()
                .min(1)
                .max(255)
                .label(
                  translate(
                    'Forms.EmailForm.firstName.label',
                  ),
                ),
              hasAddress: yup.boolean(),
              address: yup.boolean().when('hasAddress', {
                is: true,
                then: yup
                  .object()
                  .shape({
                    firstLine: yup
                      .string()
                      .required()
                      .max(255)
                      .label(
                        translate(
                          'Forms.EmailForm.address.firstLine.label',
                        ),
                      ),
                    secondLine: yup
                      .string()
                      .max(255)
                      .label(
                        translate(
                          'Forms.EmailForm.address.secondLine.label',
                        ),
                      ),
                  }),
              }),
            }),
        }),
    [],
  );

  const form = useForm<EmailForm>({
    resolver: yupResolver(schema),
    defaultValues: {
      type: 'PERSON',
    },
  });

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!