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

Commit

Permalink
fix(validators): fix pattern validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Nov 21, 2020
1 parent ed1ec39 commit 2255eb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
1 change: 0 additions & 1 deletion dev/typescript/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export default defineComponent({
value: string;
disabled?: boolean;
}[];
form.fields.name.value = 'Alvaro';
} catch (e) {
console.error(e);
}
Expand Down
1 change: 0 additions & 1 deletion src/core/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SelectInput,
CustomInput,
FormControl,
InputType,
} from './models';

const EMPTY_CONTROL = {
Expand Down
2 changes: 1 addition & 1 deletion src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type BindingObject = {
[key: string]: any;
};

interface ValidatorFn {
export interface ValidatorFn {
(control: FormControl<InputType> | undefined): ValidationErrors | null;
}

Expand Down
47 changes: 29 additions & 18 deletions src/core/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -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 === '';
Expand Down Expand Up @@ -80,27 +85,33 @@ export const maxLength = (maxLength: number) => (
: null;
};

export const pattern = (pattern: string) => (
control: FormControl<InputType>,
): 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<InputType>) => {
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 {
Expand Down

0 comments on commit 2255eb6

Please sign in to comment.