-
Notifications
You must be signed in to change notification settings - Fork 10
Issue#48 support field validation constraint #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
c23edfe
afe30c5
383f086
ffa208c
51a15f4
ee2b9d7
32a1aba
839fc7d
57cd0dd
a1c1b25
a2177ff
abbba9a
39b50f5
78de057
d46af05
206482c
99cf986
0ec9d97
391b56d
a023482
151d8da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| import { ValidationEngine } from './validationEngine'; | ||
| import { | ||
| ValidationConstraints, | ||
| FieldValidationFunction, | ||
| FormValidationFunction, | ||
| FieldValidationResult, | ||
| FormValidationResult, | ||
| FieldValidationConstraint, | ||
| ValidationFilters, | ||
| } from './entities'; | ||
| import { consts } from './consts'; | ||
|
|
||
|
|
@@ -13,35 +16,64 @@ interface FormValidation { | |
| isValidationInProgress(): boolean; | ||
| isFormDirty(): boolean; | ||
| isFormPristine(): boolean; | ||
| addFieldValidation(key: string, validation: (value: string, vm: any) => FieldValidationResult, filter?: any): FormValidation; | ||
| addFieldValidationAsync(key: string, validation: (value: string, vm: any) => Promise<FieldValidationResult>, filter?: any): FormValidation; | ||
| } | ||
|
|
||
| export class BaseFormValidation implements FormValidation { | ||
| private validationEngine: ValidationEngine; | ||
|
|
||
| constructor(validationConstraints: ValidationConstraints) { | ||
| this.validationEngine = new ValidationEngine(); | ||
| this.parseValidationConstraints(validationConstraints); | ||
| } | ||
|
|
||
| private parseValidationConstraints(validationConstraints: ValidationConstraints) { | ||
| if (validationConstraints && typeof validationConstraints === 'object') { | ||
| if (validationConstraints.global && validationConstraints.global instanceof Array) { | ||
| this.addFormValidationFunctions(validationConstraints.global); | ||
| const { global, fields } = validationConstraints; | ||
| if (global && global instanceof Array) { | ||
| this.parseFormValidations(global); | ||
| } | ||
| if (fields && typeof fields === 'object') { | ||
| this.parseAllFieldsValidations(fields); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private addFormValidationFunctions(validationFunctions: FormValidationFunction[]) { | ||
| private parseFieldValidations(constraint: string, fieldValidationConstraints: FieldValidationConstraint[]) { | ||
|
||
| if (fieldValidationConstraints instanceof Array) { | ||
| fieldValidationConstraints.forEach((fieldValidationConstraint) => { | ||
| if (fieldValidationConstraint && typeof fieldValidationConstraint === 'object') { | ||
| this.addFieldValidation(constraint, fieldValidationConstraint); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to field |
||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private parseAllFieldsValidations(fields: { [key: string]: FieldValidationConstraint[] }) { | ||
| for (let field in fields) { | ||
| this.parseFieldValidations(field, fields[field]); | ||
| } | ||
| } | ||
|
|
||
| private parseFormValidations(validationFunctions: FormValidationFunction[]) { | ||
|
||
| validationFunctions.forEach((validationFunction: FormValidationFunction) => { | ||
| if (typeof validationFunction === 'function') { | ||
| this.validationEngine.addFormValidation(validationFunction); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| validateField(vm: any, key: string, value: any, filter?: any): Promise<FieldValidationResult> { | ||
| return this.validationEngine.validateSingleField(vm, key, value, filter); | ||
| private addFieldValidation(constraint: string, validationConstraint: FieldValidationConstraint): FormValidation { | ||
| this.validationEngine.addFieldValidation( | ||
| constraint, | ||
|
||
| validationConstraint.validator, | ||
| validationConstraint.eventFilters, | ||
| validationConstraint.customParams | ||
| ); | ||
| return this; | ||
| } | ||
|
|
||
| validateField(vm: any, key: string, value: any, filter?: ValidationFilters): Promise<FieldValidationResult> { | ||
| return this.validationEngine.triggerFieldValidation(vm, key, value, filter); | ||
| } | ||
|
|
||
| validateForm(vm: any): Promise<FormValidationResult> { | ||
|
|
@@ -59,18 +91,8 @@ export class BaseFormValidation implements FormValidation { | |
| isFormPristine(): boolean { | ||
| return this.validationEngine.isFormPristine(); | ||
| } | ||
|
|
||
| addFieldValidation(key: string, validationFunction: (value: string, vm: any) => FieldValidationResult): FormValidation { | ||
| this.validationEngine.addFieldValidation(key, validationFunction); | ||
| return this; | ||
| } | ||
|
|
||
| addFieldValidationAsync(key: string, validationFunction: (value: string, vm: any) => Promise<FieldValidationResult>): FormValidation { | ||
| this.validationEngine.addFieldValidationAsync(key, validationFunction); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| export function createFormValidation(validationConstraints: ValidationConstraints): FormValidation { | ||
| return new BaseFormValidation(validationConstraints); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| export interface ValidationFilters { | ||
| [key: string]: boolean; | ||
| } | ||
|
|
||
| export class FieldValidation { | ||
| public validationFn: (value, vm) => Promise<FieldValidationResult>; | ||
| public filter: any; | ||
| validationFn: (value, vm, customParams) => Promise<FieldValidationResult>; | ||
| filters: ValidationFilters; | ||
| customParams: any; | ||
| } | ||
|
|
||
| export class FieldValidationResult { | ||
|
|
@@ -19,8 +24,8 @@ export class FieldValidationResult { | |
|
|
||
| export class FormValidationResult { | ||
| succeeded: boolean; | ||
| fieldErrors: Array<FieldValidationResult>; | ||
| formGlobalErrors: Array<FieldValidationResult>; | ||
| fieldErrors: FieldValidationResult[]; | ||
| formGlobalErrors: FieldValidationResult[]; | ||
|
|
||
| constructor() { | ||
| this.succeeded = false; | ||
|
|
@@ -34,6 +39,23 @@ export interface FormValidationFunction { | |
| (vm: any): ValidationResult; | ||
| } | ||
|
|
||
| export interface ValidationConstraints extends Object { | ||
| export interface SyncValidationFunction { | ||
| (value: any, vm: any, customParams: any): ValidationResult; | ||
| } | ||
|
|
||
| export interface AsyncFieldValidationFunction { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need defined
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have enough if we only define this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this interface |
||
| (value: any, vm: any, customParams: any): Promise<FieldValidationResult>; | ||
| } | ||
|
|
||
| export type FieldValidationFunction = SyncValidationFunction | AsyncFieldValidationFunction; | ||
|
|
||
| export interface FieldValidationConstraint { | ||
| validator: FieldValidationFunction; | ||
| eventFilters?: ValidationFilters; | ||
| customParams?: any; | ||
| } | ||
|
|
||
| export interface ValidationConstraints { | ||
| global?: FormValidationFunction[]; | ||
| fields?: { [key: string]: FieldValidationConstraint[] }; | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,25 @@ | ||
| import { FieldValidation } from './entities'; | ||
| import { FieldValidation, ValidationFilters } from './entities'; | ||
|
|
||
| class FieldValidationEventFilter { | ||
|
|
||
| public filter(fieldValidations: Array<FieldValidation>, filter: any): Array<FieldValidation> { | ||
| let result = new Array<FieldValidation>(); | ||
| public filter(fieldValidations: FieldValidation[], filters: ValidationFilters): FieldValidation[] { | ||
| let result: FieldValidation[] = []; | ||
|
|
||
| if (filter) { | ||
| result = fieldValidations.filter((element) => { | ||
| return this.matchFilterOr(element, filter) | ||
| }); | ||
| if (filters) { | ||
| result = fieldValidations.filter((fieldValidation) => | ||
| this.matchsAnyFilter(fieldValidation, filters) | ||
| ); | ||
| } else { | ||
| result = fieldValidations; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private matchFilterOr(itemFilter, globalFilter) { | ||
| let result: boolean = false; | ||
|
|
||
| for (var property in globalFilter) { | ||
| if (this.propertyMatched(itemFilter, property, globalFilter)) { | ||
| result = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private propertyMatched(item: any, property: any, globalFilter: any): boolean { | ||
| return (globalFilter.hasOwnProperty(property) && | ||
| globalFilter[property] == item.filter[property]); | ||
| private matchsAnyFilter(fieldValidation: FieldValidation, filters: ValidationFilters) { | ||
|
||
| return Object.keys(filters).some(filter => | ||
| filters[filter] === fieldValidation.filters[filter] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type => validateField(vm: any, key: string, value: any, filter?: ValidationFilter): Promise;