File size: 1,068 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import type { FieldError, FieldErrors, FieldValues } from '../types';
import get from '../utils/get';
import isKey from '../utils/isKey';
export default function schemaErrorLookup<T extends FieldValues = FieldValues>(
errors: FieldErrors<T>,
_fields: FieldValues,
name: string,
): {
error?: FieldError;
name: string;
} {
const error = get(errors, name);
if (error || isKey(name)) {
return {
error,
name,
};
}
const names = name.split('.');
while (names.length) {
const fieldName = names.join('.');
const field = get(_fields, fieldName);
const foundError = get(errors, fieldName);
if (field && !Array.isArray(field) && name !== fieldName) {
return { name };
}
if (foundError && foundError.type) {
return {
name: fieldName,
error: foundError,
};
}
if (foundError && foundError.root && foundError.root.type) {
return {
name: `${fieldName}.root`,
error: foundError.root,
};
}
names.pop();
}
return {
name,
};
}
|