Skip to content

Commit

Permalink
fix(zodResolver): remove duplicated code (#330)
Browse files Browse the repository at this point in the history
* fix(zodResolver): remove duplicated code

* fix: native validation arrays
  • Loading branch information
jorisre authored Jan 18, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 91f009b commit 4ab4ec1
Showing 2 changed files with 23 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/validateFieldsNatively.ts
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ export const validateFieldsNatively = <TFieldValues>(
if (field && field.ref && 'reportValidity' in field.ref) {
setCustomValidity(field.ref, fieldPath, errors)
} else if (field.refs) {
field.refs.forEach((ref: HTMLInputElement, i) => setCustomValidity(ref, fieldPath + '.' + i, errors))
field.refs.forEach((ref: HTMLInputElement) => setCustomValidity(ref, fieldPath, errors))
}
}
};
85 changes: 22 additions & 63 deletions zod/src/zod.ts
Original file line number Diff line number Diff line change
@@ -2,58 +2,17 @@ import {
appendErrors,
FieldError,
FieldErrors,
set,
get,
Field,
ResolverOptions,
} from 'react-hook-form';
import { z } from 'zod';
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
import type { Resolver } from './types';

// Native validation (web only)
export const validateFieldsNatively = <TFieldValues>(
errors: Record<string, FieldError>,
options: ResolverOptions<TFieldValues>,
): void => {
for (const fieldPath in options.fields) {
const field = options.fields[fieldPath];

if (field && field.ref && 'reportValidity' in field.ref) {
const error = get(errors, fieldPath) as FieldError | undefined;

field.ref.setCustomValidity((error && error.message) || '');

field.ref.reportValidity();
}
}
};

const toNestError = <TFieldValues>(
errors: Record<string, FieldError>,
options: ResolverOptions<TFieldValues>,
): FieldErrors<TFieldValues> => {
options.shouldUseNativeValidation && validateFieldsNatively(errors, options);

const fieldErrors = {} as FieldErrors<TFieldValues>;
for (const path in errors) {
const field = get(options.fields, path) as Field['_f'] | undefined;

set(
fieldErrors,
path,
Object.assign(errors[path], { ref: field && field.ref }),
);
}

return fieldErrors;
};

const parseErrorSchema = (
zodErrors: z.ZodIssue[],
validateAllFieldCriteria: boolean,
) => {
const errors: Record<string, FieldError> = {};
for (; zodErrors.length; ) {
for (; zodErrors.length;) {
const error = zodErrors[0];
const { code, message, path } = error;
const _path = path.join('.');
@@ -100,31 +59,31 @@ const parseErrorSchema = (

export const zodResolver: Resolver =
(schema, schemaOptions, resolverOptions = {}) =>
async (values, _, options) => {
try {
const data = await schema[
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
](values, schemaOptions);
async (values, _, options) => {
try {
const data = await schema[
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
](values, schemaOptions);

options.shouldUseNativeValidation && validateFieldsNatively({}, options);
options.shouldUseNativeValidation && validateFieldsNatively({}, options);

return {
errors: {} as FieldErrors,
values: data,
};
} catch (error: any) {
return {
values: {},
errors: error.isEmpty
? {}
: toNestError(
return {
errors: {} as FieldErrors,
values: data,
};
} catch (error: any) {
return {
values: {},
errors: error.isEmpty
? {}
: toNestError(
parseErrorSchema(
error.errors,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
options.criteriaMode === 'all',
),
options,
),
};
}
};
};
}
};

0 comments on commit 4ab4ec1

Please sign in to comment.