Skip to content

Commit

Permalink
fix(designer): simplify the email validation regex (#4626)
Browse files Browse the repository at this point in the history
  • Loading branch information
hartra344 committed Apr 16, 2024
1 parent 3843354 commit f5c8485
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
3 changes: 1 addition & 2 deletions libs/designer/src/lib/core/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const regex = {
datetime:
/^(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])(?:[\sT])([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d|60))?(\.\d+)?(([Zz])|([+|-]([01]\d|2[0-3])))?$/,
double: /^(?:[-+])?([0-9]*(\.[0-9]+([eE](?:[-+])[0-9]+)?)?)$/,
email:
/^([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|"(!#-[^-~ \t]|(\\[\t -~]))+")@([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|\[[\t -Z^-~]*])$/,
email: /.+@.+/,
integer: /^(?:[-+])?([0-9]+)$/,
phone: /^(\+)?(?:[0-9]{5,15})$/,
url: /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))?)(?::\d{2,5})?(?:\/\S*)?$/i,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { replaceTemplatePlaceholders } from '../operations';
import { test, describe, expect } from 'vitest';
describe('replaceTemplatePlaceholders', () => {
test('should replace placeholders with corresponding values', () => {
const placeholderValues = { name: 'John', age: '25' };
const template = 'Hello, {NAME}. You are {AGE} years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, John. You are 25 years old.');
});

test('should handle missing placeholder values by leaving the placeholder', () => {
const placeholderValues = { name: 'John' };
const template = 'Hello, {NAME}. You are {AGE} years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, John. You are {AGE} years old.');
});

test('should handle null placeholder values by replacing with an empty string', () => {
const placeholderValues = { name: null, age: '25' };
const template = 'Hello, {NAME}. You are {AGE} years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, . You are 25 years old.');
});

test('should handle undefined placeholder values by replacing with an empty string', () => {
const placeholderValues = { name: undefined, age: '25' };
const template = 'Hello, {NAME}. You are {AGE} years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, . You are 25 years old.');
});

test('should handle empty templates by returning an empty string', () => {
const placeholderValues = { name: 'John', age: '25' };
const template = '';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('');
});

test('should handle templates without placeholders by returning the original template', () => {
const placeholderValues = { name: 'John', age: '25' };
const template = 'Hello, John. You are 25 years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe(template);
});

test('should handle templates with nested Placeholders', () => {
const placeholderValues = { name: 'John', age: '25' };
const template = 'Hello, {{NAME}}. You are 25 years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, {John}. You are 25 years old.');
});

test('should handle templates with double nested Placeholders', () => {
const placeholderValues = { name: 'John', age: '25' };
const template = 'Hello, {{NAME} you are {AGE}}. You are 25 years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, {John you are 25}. You are 25 years old.');
});

test('should handle templates with placeholders being case insensitive', () => {
const placeholderValues = { nAme: 'John', AGE: '25' };
const template = 'Hello, {{name} you are {AgE}}. You are 25 years old.';
const result = replaceTemplatePlaceholders(placeholderValues, template);
expect(result).toBe('Hello, {John you are 25}. You are 25 years old.');
});
});

0 comments on commit f5c8485

Please sign in to comment.