Skip to content

Commit

Permalink
fix(validator-zod): validator now surfaces union type errors (pablo-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-grass committed May 20, 2024
1 parent b1a1cc0 commit 243fa1c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
59 changes: 33 additions & 26 deletions packages/validator-zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,49 @@ import type {
Extender,
} from '@felte/common';
import { _update } from '@felte/common';
import type { ZodError, AnyZodObject } from 'zod';
import type { ZodError, ZodSchema } from 'zod';

type ZodSchema = {
parseAsync: AnyZodObject['parseAsync'];
};

export type ValidatorConfig = {
schema: ZodSchema;
export type ValidatorConfig<Data extends Obj = Obj> = {
schema: ZodSchema<Data>;
level?: 'error' | 'warning';
};

export function validateSchema<Data extends Obj>(
schema: ZodSchema
): ValidationFunction<Data> {
function shapeErrors(errors: ZodError): AssignableErrors<Data> {
return errors.issues.reduce((err, value) => {
/* istanbul ignore next */
if (!value.path) return err;
return _update(
err,
value.path.join('.'),
(currentValue: undefined | string[]) => {
if (!currentValue || !Array.isArray(currentValue))
return [value.message];
return [...currentValue, value.message];
}
);
}, {} as AssignableErrors<Data>);
}
return async function validate(
values: Data
): Promise<AssignableErrors<Data> | undefined> {
try {
await schema.parseAsync(values);
} catch (error) {
return shapeErrors(error as ZodError<any>);
async function walk(
error: ZodError,
err: AssignableErrors<Data>
): Promise<AssignableErrors<Data>> {
for (const issue of error.issues) {
if (issue.code === 'invalid_union') {
for (const unionError of issue.unionErrors) {
err = await walk(unionError, err);
}
} else {
if (!issue.path) continue;

const updater = (currentValue?: string[]) => {
if (!currentValue || !Array.isArray(currentValue)) {
return [issue.message];
}
return [...currentValue, issue.message];
};
err = _update(err, issue.path.join('.'), updater);
}
}

return err;
}

const result = await schema.safeParseAsync(values);
if (!result.success) {
let err = {} as AssignableErrors<Data>;
err = await walk(result.error, err);
return err;
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/validator-zod/tests/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('Validator zod', () => {
});
const mockData = { elems: [null, { name: '' }] };

const { validate, errors } = createForm<typeof mockData>({
const { validate, errors } = createForm({
initialValues: mockData,
onSubmit: vi.fn(),
extend: validator({ schema }),
Expand All @@ -327,7 +327,7 @@ describe('Validator zod', () => {
});
});

test.skip('should surface union type errors', async () => {
test('should surface union type errors', async () => {
async function t(schema: ZodSchema, initialValues: object) {
const { validate, errors } = createForm({
initialValues,
Expand Down

0 comments on commit 243fa1c

Please sign in to comment.