Fix/email validation regex - #1026
Conversation
Fix the email validation function by correcting the regex escape pattern. The previous implementation used an escaped `$` in a raw string, which caused the regex to match a literal `$` character instead of the end-of-string anchor. Update the regex pattern to properly validate email formats.
There was a problem hiding this comment.
Code Review
This pull request corrects a bug in the email validation regular expression within EmailFunction by fixing an incorrectly escaped end anchor, and introduces comprehensive unit tests to verify its behavior. The reviewer recommends optimizing performance by declaring the RegExp as a static final field to prevent recompiling it on every function invocation.
| final Object? value = args['value']; | ||
| if (value is! String) return false; | ||
| final emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+\$'); | ||
| final emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+$'); |
There was a problem hiding this comment.
Instantiating a RegExp on every invocation of executeSync is inefficient because compiling a regular expression is a relatively expensive operation. Since EmailFunction has a const constructor, you cannot declare a non-static instance field. Instead, consider declaring the RegExp as a static final field inside the EmailFunction class (or as a private top-level variable in the file) to compile it only once.
Fix the email validation function by correcting the regex escape pattern.
The previous implementation used an escaped
$in a raw string, which caused the regex to match a literal$character instead of the end-of-string anchor.Update the regex pattern to properly validate email formats.
Description
This PR fixes an issue in the email validation function where the regular expression pattern did not correctly handle the end-of-string anchor.
The previous implementation incorrectly escaped the
$character inside a Dart raw string. As a result, the regex engine treated it as a literal character instead of the end-of-string matcher, causing valid email addresses to fail validation.This change updates the regex pattern to use the correct
$anchor and ensures email validation behaves as expected.Related Issues
N/A
Testing
Pre-launch Checklist