-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize validation error lookup in VisualEditor #952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,12 +136,24 @@ export const VisualEditor: React.FC<VisualEditorProps> = ({ | |
| [fields, categories] | ||
| ); | ||
|
|
||
| // ⚡ Bolt optimization: Group validation errors by field in a single O(M) pass | ||
| // instead of filtering the array inside a callback for every single field O(N*M). | ||
| const fieldErrorsMap = useMemo(() => { | ||
| return validationErrors.reduce((acc, error) => { | ||
| if (!acc[error.field]) { | ||
| acc[error.field] = []; | ||
| } | ||
| acc[error.field].push(error); | ||
| return acc; | ||
| }, {} as Record<string, ValidationRule[]>); | ||
| }, [validationErrors]); | ||
|
|
||
| // Get field errors | ||
| const getFieldErrors = useCallback( | ||
| (fieldName: string): ValidationRule[] => { | ||
| return validationErrors.filter((error) => error.field === fieldName); | ||
| return fieldErrorsMap[fieldName] || []; | ||
| }, | ||
| [validationErrors] | ||
| [fieldErrorsMap] | ||
|
Comment on lines
152
to
+156
|
||
| ); | ||
|
|
||
| // Check if form has unsaved changes | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a plain object literal as a lookup table can break (and can be security-sensitive) when
error.fieldmatches an Object prototype key (e.g.__proto__,constructor,toString). In those casesacc[error.field]may not be an array and.pushcan throw or mutate the prototype. Consider usingObject.create(null)for the accumulator (or aMap) so arbitrary field names are safe.