feat: improves conditional logic performance and edge cases
This commit is contained in:
@@ -34,6 +34,7 @@ type Arguments = {
|
||||
unflattenLocales: boolean
|
||||
unflattenLocaleActions: (() => void)[]
|
||||
docWithLocales?: Record<string, any>
|
||||
skipValidation?: boolean
|
||||
}
|
||||
|
||||
const traverseFields = (args: Arguments): void => {
|
||||
@@ -64,6 +65,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
unflattenLocaleActions,
|
||||
unflattenLocales,
|
||||
docWithLocales = {},
|
||||
skipValidation,
|
||||
} = args;
|
||||
|
||||
fields.forEach((field) => {
|
||||
@@ -187,11 +189,14 @@ const traverseFields = (args: Arguments): void => {
|
||||
fullData,
|
||||
}));
|
||||
|
||||
const passesCondition = (field.admin?.condition && hook === 'beforeChange') ? field.admin.condition(fullData, data) : true;
|
||||
|
||||
if (fieldHasSubFields(field)) {
|
||||
if (field.name === undefined) {
|
||||
traverseFields({
|
||||
...args,
|
||||
fields: field.fields,
|
||||
skipValidation: !passesCondition,
|
||||
});
|
||||
} else if (fieldIsArrayType(field)) {
|
||||
if (Array.isArray(data[field.name])) {
|
||||
@@ -207,6 +212,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
originalDoc: originalDoc?.[field.name]?.[i],
|
||||
docWithLocales: docWithLocales?.[field.name]?.[i],
|
||||
path: `${path}${field.name}.${i}.`,
|
||||
skipValidation: !passesCondition,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -218,6 +224,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
originalDoc: originalDoc[field.name],
|
||||
docWithLocales: docWithLocales?.[field.name],
|
||||
path: `${path}${field.name}.`,
|
||||
skipValidation: !passesCondition,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -235,6 +242,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
originalDoc: originalDoc?.[field.name]?.[i],
|
||||
docWithLocales: docWithLocales?.[field.name]?.[i],
|
||||
path: `${path}${field.name}.${i}.`,
|
||||
skipValidation: !passesCondition,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -267,6 +275,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
existingData: { [field.name]: existingRowCount },
|
||||
field,
|
||||
path,
|
||||
skipValidation: skipValidation || !passesCondition,
|
||||
}));
|
||||
} else {
|
||||
validationPromises.push(() => validationPromise({
|
||||
@@ -276,6 +285,7 @@ const traverseFields = (args: Arguments): void => {
|
||||
existingData: originalDoc,
|
||||
field,
|
||||
path,
|
||||
skipValidation: skipValidation || !passesCondition,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ type Arguments = {
|
||||
errors: {message: string, field: string}[]
|
||||
newData: Record<string, unknown>
|
||||
existingData: Record<string, unknown>
|
||||
skipValidation?: boolean
|
||||
}
|
||||
|
||||
const validationPromise = async ({
|
||||
@@ -16,8 +17,9 @@ const validationPromise = async ({
|
||||
existingData,
|
||||
field,
|
||||
path,
|
||||
skipValidation,
|
||||
}: Arguments): Promise<string | boolean> => {
|
||||
if (hook !== 'beforeChange') return true;
|
||||
if (hook !== 'beforeChange' || !skipValidation) return true;
|
||||
|
||||
const hasCondition = field.admin && field.admin.condition;
|
||||
const shouldValidate = field.validate && !hasCondition;
|
||||
|
||||
Reference in New Issue
Block a user