mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-13 21:01:16 +08:00
# Pull Request Template ## Description This PR fixes multiple issues related to regex patterns and validation for custom attributes. 1. Fixed regex patterns being double-escaped when saving from Add and Edit flows 2. Fixed regex validation not being enforced in the widget pre-chat form 3. Minor UI improvements in the Add/Edit custom attribute dialog Fixes [CW-6625](https://linear.app/chatwoot/issue/CW-6625/bug-report-custom-attribute-regex-validation-not-working-in-ui), https://github.com/chatwoot/chatwoot/issues/13771 ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? **Loom video** **Before** https://www.loom.com/share/14f1983a8bc84f9fabc3663afd83cd50 **After** https://www.loom.com/share/867c0484741140c1944fcbd43914c9c0 ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
126 lines
4.5 KiB
JavaScript
126 lines
4.5 KiB
JavaScript
/**
|
|
* Checks if a string is a valid E.164 phone number format.
|
|
* @param {string} value - The phone number to validate.
|
|
* @returns {boolean} True if the number is in E.164 format, false otherwise.
|
|
*/
|
|
export const isPhoneE164 = value => !!value.match(/^\+[1-9]\d{1,14}$/);
|
|
|
|
/**
|
|
* Validates a phone number after removing the dial code.
|
|
* @param {string} value - The full phone number including dial code.
|
|
* @param {string} dialCode - The dial code to remove before validation.
|
|
* @returns {boolean} True if the number (without dial code) is valid, false otherwise.
|
|
*/
|
|
export const isPhoneNumberValid = (value, dialCode) => {
|
|
const number = value.replace(dialCode, '');
|
|
return !!number.match(/^[0-9]{1,14}$/);
|
|
};
|
|
|
|
/**
|
|
* Checks if a string is either a valid E.164 phone number or empty.
|
|
* @param {string} value - The phone number to validate.
|
|
* @returns {boolean} True if the number is in E.164 format or empty, false otherwise.
|
|
*/
|
|
export const isPhoneE164OrEmpty = value => isPhoneE164(value) || value === '';
|
|
|
|
/**
|
|
* Validates a phone number with dial code, requiring at least 5 digits.
|
|
* @param {string} value - The full phone number including dial code.
|
|
* @returns {boolean} True if the number is valid, false otherwise.
|
|
*/
|
|
export const isPhoneNumberValidWithDialCode = value => {
|
|
const number = value.replace(/^\+/, ''); // Remove the '+' sign
|
|
return !!number.match(/^[1-9]\d{4,}$/); // Validate the phone number with minimum 5 digits
|
|
};
|
|
|
|
/**
|
|
* Checks if a string starts with a plus sign.
|
|
* @param {string} value - The string to check.
|
|
* @returns {boolean} True if the string starts with '+', false otherwise.
|
|
*/
|
|
export const startsWithPlus = value => value.startsWith('+');
|
|
|
|
/**
|
|
* Checks if a string is a valid URL (starts with 'http') or is empty.
|
|
* @param {string} [value=''] - The string to check.
|
|
* @returns {boolean} True if the string is a valid URL or empty, false otherwise.
|
|
*/
|
|
export const shouldBeUrl = (value = '') =>
|
|
value ? value.startsWith('http') : true;
|
|
|
|
/**
|
|
* Validates a password for complexity requirements.
|
|
* @param {string} value - The password to validate.
|
|
* @returns {boolean} True if the password meets all requirements, false otherwise.
|
|
*/
|
|
export const isValidPassword = value => {
|
|
const containsUppercase = /[A-Z]/.test(value);
|
|
const containsLowercase = /[a-z]/.test(value);
|
|
const containsNumber = /[0-9]/.test(value);
|
|
const containsSpecialCharacter = /[!@#$%^&*()_+\-=[\]{}|'"/\\.,`<>:;?~]/.test(
|
|
value
|
|
);
|
|
return (
|
|
containsUppercase &&
|
|
containsLowercase &&
|
|
containsNumber &&
|
|
containsSpecialCharacter
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Checks if a string consists only of digits.
|
|
* @param {string} value - The string to check.
|
|
* @returns {boolean} True if the string contains only digits, false otherwise.
|
|
*/
|
|
export const isNumber = value => /^\d+$/.test(value);
|
|
|
|
/**
|
|
* Validates a domain name.
|
|
* @param {string} value - The domain name to validate.
|
|
* @returns {boolean} True if the domain is valid or empty, false otherwise.
|
|
*/
|
|
export const isDomain = value => {
|
|
if (value !== '') {
|
|
const domainRegex = /^([\p{L}0-9]+(-[\p{L}0-9]+)*\.)+[a-z]{2,}$/gmu;
|
|
return domainRegex.test(value);
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Creates a RegExp object from a string representation of a regular expression.
|
|
* @param {string} regexPatternValue - The string representation of the regex (e.g., '/pattern/flags').
|
|
* @returns {RegExp} A RegExp object created from the input string.
|
|
*/
|
|
export const getRegexp = regexPatternValue => {
|
|
let lastSlash = regexPatternValue.lastIndexOf('/');
|
|
return new RegExp(
|
|
regexPatternValue.slice(1, lastSlash),
|
|
regexPatternValue.slice(lastSlash + 1)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Normalises a user-entered regex pattern into canonical `/source/flags` form.
|
|
* Strips `/.../flags` wrapping if the user included it, so `new RegExp` does
|
|
* not double-escape the slashes on save.
|
|
*
|
|
* @param {string} pattern - Raw pattern string from the form.
|
|
* @returns {?string} Canonical `/source/flags` string, or null when empty.
|
|
*/
|
|
export const normalizeRegexPattern = pattern => {
|
|
if (!pattern) return null;
|
|
const match = pattern.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
const source = match ? match[1] : pattern;
|
|
const flags = match ? match[2] : '';
|
|
return new RegExp(source, flags).toString();
|
|
};
|
|
|
|
/**
|
|
* Checks if a string is a valid slug (letters, numbers, hyphens only, no spaces or other symbols).
|
|
* @param {string} value - The slug to validate.
|
|
* @returns {boolean} True if the slug is valid, false otherwise.
|
|
*/
|
|
export const isValidSlug = value => /^[a-zA-Z0-9-]+$/.test(value);
|