Skip to content

Commit

Permalink
perf(typanion): reduce bundle size (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisre authored Aug 4, 2021
1 parent e36e69c commit 4309d1c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions typanion/src/typanion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import type { FieldErrors } from 'react-hook-form';
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
import type { Resolver } from './types';

const parseErrors = (
errors: string[],
parsedErrors: FieldErrors = {},
_path = '',
) => {
const parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {
return errors.reduce((acc, error) => {
const [_key, _message] = error.split(':')
const key = _key.slice(1)
const message = _message.trim()
const e = error.split(':');

acc[key] = {
message,
acc[e[0].slice(1)] = {
message: e[1].trim(),
};

return acc;
Expand All @@ -22,16 +16,26 @@ const parseErrors = (

export const typanionResolver: Resolver =
(validator, validatorOptions = {}) =>
(values, _, options) => {
const rawErrors: string[] = []
const isValid = validator(values, {errors: rawErrors, ...validatorOptions})
const parsedErrors = parseErrors(rawErrors)
(values, _, options) => {
const rawErrors: string[] = [];
const isValid = validator(
values,
Object.assign(
{},
{
errors: rawErrors,
},
validatorOptions,
),
);
const parsedErrors = parseErrors(rawErrors);

if (!isValid) {
return { values: {}, errors: toNestError(parsedErrors, options) };
}

options.shouldUseNativeValidation && validateFieldsNatively(parsedErrors, options);
if (isValid) {
options.shouldUseNativeValidation &&
validateFieldsNatively(parsedErrors, options);

return { values, errors: {} };
};
}

return { values: {}, errors: toNestError(parsedErrors, options) };
};

0 comments on commit 4309d1c

Please sign in to comment.