From 2255eb6c6bf7f835135374ad401f43664cd5d0c8 Mon Sep 17 00:00:00 2001 From: Alvaro Saburido Date: Sat, 21 Nov 2020 09:09:30 +0100 Subject: [PATCH] fix(validators): fix pattern validation --- dev/typescript/App.vue | 1 - src/core/factories.ts | 1 - src/core/models.ts | 2 +- src/core/utils/validators.ts | 47 ++++++++++++++++++++++-------------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/dev/typescript/App.vue b/dev/typescript/App.vue index fdb65bf..bea2b15 100644 --- a/dev/typescript/App.vue +++ b/dev/typescript/App.vue @@ -230,7 +230,6 @@ export default defineComponent({ value: string; disabled?: boolean; }[]; - form.fields.name.value = 'Alvaro'; } catch (e) { console.error(e); } diff --git a/src/core/factories.ts b/src/core/factories.ts index 0e98bd4..ef17811 100644 --- a/src/core/factories.ts +++ b/src/core/factories.ts @@ -11,7 +11,6 @@ import { SelectInput, CustomInput, FormControl, - InputType, } from './models'; const EMPTY_CONTROL = { diff --git a/src/core/models.ts b/src/core/models.ts index 4234930..a351f43 100644 --- a/src/core/models.ts +++ b/src/core/models.ts @@ -33,7 +33,7 @@ export type BindingObject = { [key: string]: any; }; -interface ValidatorFn { +export interface ValidatorFn { (control: FormControl | undefined): ValidationErrors | null; } diff --git a/src/core/utils/validators.ts b/src/core/utils/validators.ts index 61f022a..b342d04 100644 --- a/src/core/utils/validators.ts +++ b/src/core/utils/validators.ts @@ -1,4 +1,9 @@ -import { FormControl, InputType, ValidationErrors } from '../models'; +import { + FormControl, + InputType, + ValidationErrors, + ValidatorFn, +} from '../models'; export const isEmptyInputValue = (value: string | number | boolean): boolean => value == null || value === ''; @@ -80,27 +85,33 @@ export const maxLength = (maxLength: number) => ( : null; }; -export const pattern = (pattern: string) => ( - control: FormControl, -): ValidationErrors | null => { - if (isEmptyInputValue(control.value)) { - return null; // don't validate empty values to allow optional controls - } - - let regexStr: string | RegExp; - regexStr = ''; +export const pattern = (pattern: string): ValidatorFn => { + if (!pattern) return null; + let regex: RegExp; + let regexStr: string; + if (typeof pattern === 'string') { + regexStr = ''; - if (pattern.charAt(0) !== '^') regexStr += '^'; + if (pattern.charAt(0) !== '^') regexStr += '^'; - regexStr += pattern; + regexStr += pattern; - if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$'; + if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$'; - const regex = new RegExp(regexStr); - const value = `${control.value}`; - return regex.test(value) - ? { pattern: { requiredPattern: regexStr, actualValue: value } } - : null; + regex = new RegExp(regexStr); + } else { + regexStr = pattern; + regex = pattern; + } + return (control: FormControl) => { + if (isEmptyInputValue(control.value)) { + return null; // don't validate empty values to allow optional controls + } + const value = control.value; + return regex.test(value as string) + ? null + : { pattern: { requiredPattern: regexStr, actualValue: value } }; + }; }; export default {