Zod .refine() method doesn’t enforce “one of two fields required” rule in Vue 3

I’m working on a Vite/Vue 3 project and using Zod for form validation. I have a form with these two fields :

descriptionActiEconomiques and noga

I need to enforce the rule that at least one of them must be filled out. If neither field is filled, both fields should show an error with the message: “L’un des deux champs ‘Description des Activités Économiques’ ou ‘NOGA’ est requis.”

My full schema look like this:

export const formSchema = [
    // Step Inscription
    z.object({

        // Unite Légale
        typeInscription: z
            .string({
                required_error: 'Champs requis.',
            }),
        ide: z
            .string({
                required_error: 'Champs requis.',
            })
            .regex(/^CHEd{9}$/, {
                message: "Le format doit être 'CHE' suivi de 9 chiffres",
            }),
        nameUniteLocal: z
            .string({
                required_error: 'Champs requis.',
            }),
        formeJuridique: z
            .string({
                required_error: 'Champs requis.',
            }),
        pays: z
            .string({
                required_error: 'Champs requis.',
            }),
        langue: z
            .string({
                required_error: 'Champs requis.',
            }),
        descriptionActiEconomiques: z
            .string()
            .optional(),
        noga: z
            .string()
            .optional(),

        // Adresse
        npa: z
            .string({
                required_error: 'Champs requis.',
            })
            .min(4, 'NPA non valide.')
            .max(4, 'NPA non valide.'),
        localite: z
            .string({
                required_error: 'Champs requis.',
            }),
        rue: z
            .string({
                required_error: 'Champs requis.',
            }),
        adresseUniteNumero: z
            .string({
                required_error: 'Champs requis.',
            }),
        uniteCasePostaleNumero: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),
        uniteCasePostaleNpa: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),
        uniteCasePostaleLocalite: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),

        // Contact
        numeroTelephone: z
            .union([s1,s2]),
        emailEntreprise: z
            .string({
                required_error: 'Champs requis.',
            })
            .email({
                message: 'Email non valide.'
            }),

        // Autre
        remarque: z
            .string()
            .optional(),
        attachedFile: z
            .string()
            .optional()
    }).refine((data) => data.descriptionActiEconomiques || data.noga, {
        message: "L'un des deux champs 'Description des Activités Économiques' ou 'NOGA' est requis.",
        path: ['descriptionActiEconomiques', 'noga'],
    }),

    // Step Responsable
    z.object({

        // Responsable
        responsablePersonneId: z
            .string()
            .optional(),
        responsableSexe: z
            .string()
            .optional(),
        responsablePrenom: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNom: z
            .string({
                required_error: 'Champs requis.',
            }),

        // Contact
        responsableNumeroTelephone: z
            .union([s1,s2]),
        responsableEmail: z
            .string({
                required_error: 'Champs requis.',
            })
            .email({
                message: 'Email non valide.'
            }),

        // Adresse
        responsablePays: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNpa: z
            .string({
                required_error: 'Champs requis.',
            })
            .min(4, 'NPA non valide.')
            .max(4, 'NPA non valide.'),
        responsableLocalite: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableRue: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNumeroDeRue: z
            .string({
                required_error: 'Champs requis.',
            }),
    }),
]

Like you see I tried to add :

.refine((data) => data.descriptionActiEconomiques || data.noga, {
        message: "L'un des deux champs 'Description des Activités Économiques' ou 'NOGA' est requis.",
        path: ['descriptionActiEconomiques', 'noga'],
    })

But dont’ work…

Expected Behavior

  • If both descriptionActiEconomiques and noga are left empty, the validation should fail, and an error should show for both fields with the message: “L’un des deux champs ‘Description des Activités Économiques’ ou ‘NOGA’ est requis.”

Actual Behavior

  • No error message appears when both fields are empty.
  • No error in the console too.

Any ideas on why this validation rule isn’t working or how I can fix this?

Thanks in advance!

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Zod .refine() method doesn’t enforce “one of two fields required” rule in Vue 3

I’m working on a Vite/Vue 3 project and using Zod for form validation. I have a form with these two fields :

descriptionActiEconomiques and noga

I need to enforce the rule that at least one of them must be filled out. If neither field is filled, both fields should show an error with the message: “L’un des deux champs ‘Description des Activités Économiques’ ou ‘NOGA’ est requis.”

My full schema look like this:

export const formSchema = [
    // Step Inscription
    z.object({

        // Unite Légale
        typeInscription: z
            .string({
                required_error: 'Champs requis.',
            }),
        ide: z
            .string({
                required_error: 'Champs requis.',
            })
            .regex(/^CHEd{9}$/, {
                message: "Le format doit être 'CHE' suivi de 9 chiffres",
            }),
        nameUniteLocal: z
            .string({
                required_error: 'Champs requis.',
            }),
        formeJuridique: z
            .string({
                required_error: 'Champs requis.',
            }),
        pays: z
            .string({
                required_error: 'Champs requis.',
            }),
        langue: z
            .string({
                required_error: 'Champs requis.',
            }),
        descriptionActiEconomiques: z
            .string()
            .optional(),
        noga: z
            .string()
            .optional(),

        // Adresse
        npa: z
            .string({
                required_error: 'Champs requis.',
            })
            .min(4, 'NPA non valide.')
            .max(4, 'NPA non valide.'),
        localite: z
            .string({
                required_error: 'Champs requis.',
            }),
        rue: z
            .string({
                required_error: 'Champs requis.',
            }),
        adresseUniteNumero: z
            .string({
                required_error: 'Champs requis.',
            }),
        uniteCasePostaleNumero: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),
        uniteCasePostaleNpa: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),
        uniteCasePostaleLocalite: z
            .string({
                required_error: 'Champs requis.',
            })
            .optional(),

        // Contact
        numeroTelephone: z
            .union([s1,s2]),
        emailEntreprise: z
            .string({
                required_error: 'Champs requis.',
            })
            .email({
                message: 'Email non valide.'
            }),

        // Autre
        remarque: z
            .string()
            .optional(),
        attachedFile: z
            .string()
            .optional()
    }).refine((data) => data.descriptionActiEconomiques || data.noga, {
        message: "L'un des deux champs 'Description des Activités Économiques' ou 'NOGA' est requis.",
        path: ['descriptionActiEconomiques', 'noga'],
    }),

    // Step Responsable
    z.object({

        // Responsable
        responsablePersonneId: z
            .string()
            .optional(),
        responsableSexe: z
            .string()
            .optional(),
        responsablePrenom: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNom: z
            .string({
                required_error: 'Champs requis.',
            }),

        // Contact
        responsableNumeroTelephone: z
            .union([s1,s2]),
        responsableEmail: z
            .string({
                required_error: 'Champs requis.',
            })
            .email({
                message: 'Email non valide.'
            }),

        // Adresse
        responsablePays: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNpa: z
            .string({
                required_error: 'Champs requis.',
            })
            .min(4, 'NPA non valide.')
            .max(4, 'NPA non valide.'),
        responsableLocalite: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableRue: z
            .string({
                required_error: 'Champs requis.',
            }),
        responsableNumeroDeRue: z
            .string({
                required_error: 'Champs requis.',
            }),
    }),
]

Like you see I tried to add :

.refine((data) => data.descriptionActiEconomiques || data.noga, {
        message: "L'un des deux champs 'Description des Activités Économiques' ou 'NOGA' est requis.",
        path: ['descriptionActiEconomiques', 'noga'],
    })

But dont’ work…

Expected Behavior

  • If both descriptionActiEconomiques and noga are left empty, the validation should fail, and an error should show for both fields with the message: “L’un des deux champs ‘Description des Activités Économiques’ ou ‘NOGA’ est requis.”

Actual Behavior

  • No error message appears when both fields are empty.
  • No error in the console too.

Any ideas on why this validation rule isn’t working or how I can fix this?

Thanks in advance!

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật