Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit c5c8616

Browse files
authored
Merge pull request #193 from asigloo/bugfix/password-validation-stopped-working
Bugfix/password validation stopped working
2 parents de14cf8 + 2255eb6 commit c5c8616

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

dev/typescript/App.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export default defineComponent({
230230
value: string;
231231
disabled?: boolean;
232232
}[];
233-
form.fields.name.value = 'Alvaro';
234233
} catch (e) {
235234
console.error(e);
236235
}

src/core/factories.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
SelectInput,
1212
CustomInput,
1313
FormControl,
14-
InputType,
1514
} from './models';
1615

1716
const EMPTY_CONTROL = {

src/core/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type BindingObject = {
3333
[key: string]: any;
3434
};
3535

36-
interface ValidatorFn {
36+
export interface ValidatorFn {
3737
(control: FormControl<InputType> | undefined): ValidationErrors | null;
3838
}
3939

src/core/utils/validators.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { FormControl, InputType, ValidationErrors } from '../models';
1+
import {
2+
FormControl,
3+
InputType,
4+
ValidationErrors,
5+
ValidatorFn,
6+
} from '../models';
27

38
export const isEmptyInputValue = (value: string | number | boolean): boolean =>
49
value == null || value === '';
@@ -80,27 +85,33 @@ export const maxLength = (maxLength: number) => (
8085
: null;
8186
};
8287

83-
export const pattern = (pattern: string) => (
84-
control: FormControl<InputType>,
85-
): ValidationErrors | null => {
86-
if (isEmptyInputValue(control.value)) {
87-
return null; // don't validate empty values to allow optional controls
88-
}
89-
90-
let regexStr: string | RegExp;
91-
regexStr = '';
88+
export const pattern = (pattern: string): ValidatorFn => {
89+
if (!pattern) return null;
90+
let regex: RegExp;
91+
let regexStr: string;
92+
if (typeof pattern === 'string') {
93+
regexStr = '';
9294

93-
if (pattern.charAt(0) !== '^') regexStr += '^';
95+
if (pattern.charAt(0) !== '^') regexStr += '^';
9496

95-
regexStr += pattern;
97+
regexStr += pattern;
9698

97-
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
99+
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
98100

99-
const regex = new RegExp(regexStr);
100-
const value = `${control.value}`;
101-
return regex.test(value)
102-
? { pattern: { requiredPattern: regexStr, actualValue: value } }
103-
: null;
101+
regex = new RegExp(regexStr);
102+
} else {
103+
regexStr = pattern;
104+
regex = pattern;
105+
}
106+
return (control: FormControl<InputType>) => {
107+
if (isEmptyInputValue(control.value)) {
108+
return null; // don't validate empty values to allow optional controls
109+
}
110+
const value = control.value;
111+
return regex.test(value as string)
112+
? null
113+
: { pattern: { requiredPattern: regexStr, actualValue: value } };
114+
};
104115
};
105116

106117
export default {

0 commit comments

Comments
 (0)