UNPKG

@doku-dev/doku-fragment

Version:

A new Angular UI library that moving away from Bootstrap and built from scratch.

1 lines 5.75 kB
{"version":3,"file":"doku-dev-doku-fragment-validators.mjs","sources":["../../../../projects/doku-fragment/validators/src/regex.ts","../../../../projects/doku-fragment/validators/src/helper.ts","../../../../projects/doku-fragment/validators/src/validator.ts","../../../../projects/doku-fragment/validators/src/doku-dev-doku-fragment-validators.ts"],"sourcesContent":["export class DokuRegex {\n static SafeString = new RegExp(/^[a-zA-Z0-9.\\-/+,=_:'@% ]*$/);\n\n /**\n * Reference: https://owasp.org/www-community/OWASP_Validation_Regex_Repository\n */\n static Email = new RegExp(\n /^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$/\n );\n\n static HexColor = new RegExp(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);\n}\n","/**\n * Reference: https://github.com/angular/angular/blob/main/packages/forms/src/validators.ts#L18\n */\nexport function isEmptyInputValue(value: any): boolean {\n /**\n * Check if the object is a string or array before evaluating the length attribute.\n * This avoids falsely rejecting objects that contain a custom length attribute.\n * For example, the object {id: 1, length: 0, width: 0} should not be returned as empty.\n */\n return (\n value == null || ((typeof value === 'string' || Array.isArray(value)) && value.length === 0)\n );\n}\n","import { AbstractControl, ValidationErrors } from '@angular/forms';\nimport { isEmptyInputValue } from './helper';\nimport { DokuRegex } from './regex';\n\nexport class DokuValidators {\n /**\n * Validator that requires the control's value pass a safe string validation test.\n *\n * @usageNotes\n *\n * ### Validate that the field matches a valid DOKU safe string pattern\n *\n * ```typescript\n * const control = new FormControl('bad$', DokuValidators.safeString);\n *\n * console.log(control.errors); // {safeString: true}\n * ```\n *\n * @returns An error map with the `safeString` property\n * if the validation check fails, otherwise `null`.\n */\n static safeString(control: AbstractControl): ValidationErrors | null {\n return safeStringValidator(control);\n }\n\n /**\n * Validator that requires the control's value pass an email validation test.\n *\n * @usageNotes\n *\n * ### Validate that the field matches a valid DOKU email pattern\n *\n * ```typescript\n * const control = new FormControl('bad@', DokuValidators.email);\n *\n * console.log(control.errors); // {email: true}\n * ```\n *\n * @returns An error map with the `email` property\n * if the validation check fails, otherwise `null`.\n */\n static email(control: AbstractControl): ValidationErrors | null {\n return emailValidator(control);\n }\n\n /**\n * Validator that requires the control's value pass an hexadecimal color validation test.\n *\n * @usageNotes\n *\n * ### Validate that the field matches a valid DOKU hex color pattern\n *\n * ```typescript\n * const control = new FormControl('#1234', DokuValidators.hexColor);\n *\n * console.log(control.errors); // {hexColor: true}\n * ```\n *\n * @returns An error map with the `hexColor` property\n * if the validation check fails, otherwise `null`.\n */\n static hexColor(control: AbstractControl): ValidationErrors | null {\n return hexColorValidator(control);\n }\n}\n\nfunction safeStringValidator(control: AbstractControl): ValidationErrors | null {\n if (isEmptyInputValue(control.value)) {\n return null; // don't validate empty values to allow optional controls\n }\n return DokuRegex.SafeString.test(control.value) ? null : { safeString: true };\n}\n\nfunction emailValidator(control: AbstractControl): ValidationErrors | null {\n if (isEmptyInputValue(control.value)) {\n return null; // don't validate empty values to allow optional controls\n }\n return DokuRegex.Email.test(control.value) ? null : { email: true };\n}\n\nfunction hexColorValidator(control: AbstractControl): ValidationErrors | null {\n if (isEmptyInputValue(control.value)) {\n return null; // don't validate empty values to allow optional controls\n }\n return DokuRegex.HexColor.test(control.value) ? null : { hexColor: true };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":"MAAa,SAAS,CAAA;;AACb,SAAA,CAAA,UAAU,GAAG,IAAI,MAAM,CAAC,6BAA6B,CAAC,CAAC;AAE9D;;AAEG;AACI,SAAA,CAAA,KAAK,GAAG,IAAI,MAAM,CACvB,8EAA8E,CAC/E,CAAC;AAEK,SAAA,CAAA,QAAQ,GAAG,IAAI,MAAM,CAAC,oCAAoC,CAAC;;ACVpE;;AAEG;AACG,SAAU,iBAAiB,CAAC,KAAU,EAAA;AAC1C;;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