UNPKG

@hookform/resolvers

Version:

React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype and Typanion

93 lines (74 loc) 3.04 kB
import { zodResolver } from '..'; import { schema, validData, invalidData, fields } from './__fixtures__/data'; const shouldUseNativeValidation = false; describe('zodResolver', () => { it('should return values from zodResolver when validation pass & raw=true', async () => { const parseAsyncSpy = vi.spyOn(schema, 'parseAsync'); const result = await zodResolver(schema, undefined, { raw: true, })(validData, undefined, { fields, shouldUseNativeValidation, }); expect(parseAsyncSpy).toHaveBeenCalledTimes(1); expect(result).toEqual({ errors: {}, values: validData }); }); it('should return parsed values from zodResolver with `mode: sync` when validation pass', async () => { const parseSpy = vi.spyOn(schema, 'parse'); const parseAsyncSpy = vi.spyOn(schema, 'parseAsync'); const result = await zodResolver(schema, undefined, { mode: 'sync', })(validData, undefined, { fields, shouldUseNativeValidation }); expect(parseSpy).toHaveBeenCalledTimes(1); expect(parseAsyncSpy).not.toHaveBeenCalled(); expect(result.errors).toEqual({}); expect(result).toMatchSnapshot(); }); it('should return a single error from zodResolver when validation fails', async () => { const result = await zodResolver(schema)(invalidData, undefined, { fields, shouldUseNativeValidation, }); expect(result).toMatchSnapshot(); }); it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => { const parseSpy = vi.spyOn(schema, 'parse'); const parseAsyncSpy = vi.spyOn(schema, 'parseAsync'); const result = await zodResolver(schema, undefined, { mode: 'sync', })(invalidData, undefined, { fields, shouldUseNativeValidation }); expect(parseSpy).toHaveBeenCalledTimes(1); expect(parseAsyncSpy).not.toHaveBeenCalled(); expect(result).toMatchSnapshot(); }); it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => { const result = await zodResolver(schema)(invalidData, undefined, { fields, criteriaMode: 'all', shouldUseNativeValidation, }); expect(result).toMatchSnapshot(); }); it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => { const result = await zodResolver(schema, undefined, { mode: 'sync' })( invalidData, undefined, { fields, criteriaMode: 'all', shouldUseNativeValidation, }, ); expect(result).toMatchSnapshot(); }); it('should throw any error unrelated to Zod', async () => { const schemaWithCustomError = schema.refine(() => { throw Error('custom error'); }); const promise = zodResolver(schemaWithCustomError)(validData, undefined, { fields, shouldUseNativeValidation, }); await expect(promise).rejects.toThrow('custom error'); }); });