Skip to content

Commit

Permalink
Revert "fix: move back to in-build set and remove lodash.set (#685)"
Browse files Browse the repository at this point in the history
This reverts commit 5754c47.
  • Loading branch information
bluebill1049 committed Jul 3, 2024
1 parent e8e0f80 commit 4b9acb7
Showing 1 changed file with 1 addition and 47 deletions.
48 changes: 1 addition & 47 deletions src/toNestErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,9 @@ import {
FieldValues,
InternalFieldName,
} from 'react-hook-form';
import set from 'lodash.set';
import { validateFieldsNatively } from './validateFieldsNatively';

export const isDateObject = (value: unknown): value is Date => value instanceof Date;

export const isNullOrUndefined = (value: unknown): value is null | undefined => value == null;

export const isObjectType = (value: unknown): value is object =>
typeof value === 'object';

export const isObject = <T extends object>(value: unknown): value is T =>
!isNullOrUndefined(value) &&
!Array.isArray(value) &&
isObjectType(value) &&
!isDateObject(value);

export const isKey = (value: string) => /^\w*$/.test(value);

const compact = <TValue>(value: TValue[]) =>
Array.isArray(value) ? value.filter(Boolean) : [];

const stringToPath = (input: string): string[] =>
compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));

const set = (object: FieldValues, path: string, value?: unknown) => {
let index = -1;
const tempPath = isKey(path) ? [path] : stringToPath(path);
const length = tempPath.length;
const lastIndex = length - 1;

while (++index < length) {
const key = tempPath[index];
let newValue = value;

if (index !== lastIndex) {
const objValue = object[key];
newValue =
isObject(objValue) || Array.isArray(objValue)
? objValue
: !isNaN(+tempPath[index + 1])
? []
: {};
}
object[key] = newValue;
object = object[key];
}
return object;
};


export const toNestErrors = <TFieldValues extends FieldValues>(
errors: FieldErrors,
options: ResolverOptions<TFieldValues>,
Expand Down

0 comments on commit 4b9acb7

Please sign in to comment.