@doku-dev/doku-fragment
Version:
A new Angular UI library that moving away from Bootstrap and built from scratch.
106 lines (101 loc) • 3.56 kB
JavaScript
class DokuRegex {
}
DokuRegex.SafeString = new RegExp(/^[a-zA-Z0-9.\-/+,=_:'@% ]*$/);
/**
* Reference: https://owasp.org/www-community/OWASP_Validation_Regex_Repository
*/
DokuRegex.Email = new RegExp(/^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/);
DokuRegex.HexColor = new RegExp(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);
/**
* Reference: https://github.com/angular/angular/blob/main/packages/forms/src/validators.ts#L18
*/
function isEmptyInputValue(value) {
/**
* Check if the object is a string or array before evaluating the length attribute.
* This avoids falsely rejecting objects that contain a custom length attribute.
* For example, the object {id: 1, length: 0, width: 0} should not be returned as empty.
*/
return (value == null || ((typeof value === 'string' || Array.isArray(value)) && value.length === 0));
}
class DokuValidators {
/**
* Validator that requires the control's value pass a safe string validation test.
*
* @usageNotes
*
* ### Validate that the field matches a valid DOKU safe string pattern
*
* ```typescript
* const control = new FormControl('bad$', DokuValidators.safeString);
*
* console.log(control.errors); // {safeString: true}
* ```
*
* @returns An error map with the `safeString` property
* if the validation check fails, otherwise `null`.
*/
static safeString(control) {
return safeStringValidator(control);
}
/**
* Validator that requires the control's value pass an email validation test.
*
* @usageNotes
*
* ### Validate that the field matches a valid DOKU email pattern
*
* ```typescript
* const control = new FormControl('bad@', DokuValidators.email);
*
* console.log(control.errors); // {email: true}
* ```
*
* @returns An error map with the `email` property
* if the validation check fails, otherwise `null`.
*/
static email(control) {
return emailValidator(control);
}
/**
* Validator that requires the control's value pass an hexadecimal color validation test.
*
* @usageNotes
*
* ### Validate that the field matches a valid DOKU hex color pattern
*
* ```typescript
* const control = new FormControl('#1234', DokuValidators.hexColor);
*
* console.log(control.errors); // {hexColor: true}
* ```
*
* @returns An error map with the `hexColor` property
* if the validation check fails, otherwise `null`.
*/
static hexColor(control) {
return hexColorValidator(control);
}
}
function safeStringValidator(control) {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
return DokuRegex.SafeString.test(control.value) ? null : { safeString: true };
}
function emailValidator(control) {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
return DokuRegex.Email.test(control.value) ? null : { email: true };
}
function hexColorValidator(control) {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
return DokuRegex.HexColor.test(control.value) ? null : { hexColor: true };
}
/**
* Generated bundle index. Do not edit.
*/
export { DokuRegex, DokuValidators };
//# sourceMappingURL=doku-dev-doku-fragment-validators.mjs.map