From 4796fb19a394b97fc485754649d63694b592c67d Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Mon, 9 Dec 2019 20:30:54 +0300 Subject: [PATCH 01/15] Init custom validation --- src/core/validation/validation.service.js | 22 ++++++----- .../example-validation-basic.component.html | 39 +------------------ .../example-validation-basic.component.ts | 4 ++ src/lib/plugins/validation/rule.component.ts | 3 +- .../plugins/validation/validator.component.ts | 8 +++- src/plugin/validation/validator.view.js | 6 +++ 6 files changed, 32 insertions(+), 50 deletions(-) diff --git a/src/core/validation/validation.service.js b/src/core/validation/validation.service.js index ce91ed744..7cdf96a71 100644 --- a/src/core/validation/validation.service.js +++ b/src/core/validation/validation.service.js @@ -1,14 +1,16 @@ import * as LIVR from 'livr'; -export const { Validator } = LIVR; +export const {Validator} = LIVR; -function toLIVR(rules, key) { +function toLIVRFormat(rules, key) { const validationRules = []; + const customRules = []; rules.forEach(rule => { if (rule.key === key) { for (let name of Object.keys(rule)) { - if (name !== 'key' && name !== 'for') { - validationRules.push({ + if (!['for', 'key'].includes(key)) { + const rulesArray = key === 'customFunction' ? customRules : validationRules; + rulesArray.push({ [name]: rule[name] }); } @@ -16,18 +18,20 @@ function toLIVR(rules, key) { } }); return { - hasRules: validationRules.length > 0, - rules: { [key]: validationRules } + customRules: {[key]: customRules}, + rules: {[key]: validationRules}, + hasRules: validationRules.length + customRules.length > 0, + hasLIVRRules: validationRules.length > 0, }; } export function hasRules(rules, key) { - return toLIVR(rules, key).hasRules; + return toLIVRFormat(rules, key).hasRules; } export function createValidator(rules, key) { - if (arguments.length === 2) { - const settings = toLIVR(rules, key); + if (rules && key) { + const settings = toLIVRFormat(rules, key); return new Validator(settings.rules); } diff --git a/src/examples/validation-basic/example-validation-basic.component.html b/src/examples/validation-basic/example-validation-basic.component.html index 1f9a68ae8..43e67d926 100644 --- a/src/examples/validation-basic/example-validation-basic.component.html +++ b/src/examples/validation-basic/example-validation-basic.component.html @@ -33,46 +33,9 @@ - - - - - - - - - - - - - - - - diff --git a/src/examples/validation-basic/example-validation-basic.component.ts b/src/examples/validation-basic/example-validation-basic.component.ts index 9f643c20c..f4a875071 100644 --- a/src/examples/validation-basic/example-validation-basic.component.ts +++ b/src/examples/validation-basic/example-validation-basic.component.ts @@ -19,4 +19,8 @@ export class ExampleValidationBasicComponent { constructor(dataService: DataService) { this.rows = dataService.getAtoms(); } + + checkNumber(v) { + return v > 5; + } } diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index f9d655144..7d496c278 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -40,6 +40,7 @@ export class RuleComponent implements OnChanges { @Input('isoDate') iso_date?: string; @Input('equalToField') equal_to_field?: string; @Input('listOf') list_of?: string; + @Input('customFunction') customFunction?: Function = null; @ViewChild(TemplateRef) templateRef: TemplateRef; @@ -49,7 +50,7 @@ export class RuleComponent implements OnChanges { constructor( private plugin: PluginService, - private templateHost: TemplateHostService + private templateHost: TemplateHostService, ) { this.templateHost.key = () => `rule`; } diff --git a/src/lib/plugins/validation/validator.component.ts b/src/lib/plugins/validation/validator.component.ts index b3e563a97..7d01fe974 100644 --- a/src/lib/plugins/validation/validator.component.ts +++ b/src/lib/plugins/validation/validator.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { AfterViewInit, Component, Input, OnInit } from '@angular/core'; import { ValidatorView } from 'ng2-qgrid/plugin/validation/validator.view'; import { PluginService } from '../plugin.service'; import { TemplateHostService } from '../../template/template-host.service'; @@ -8,7 +8,7 @@ import { TemplateHostService } from '../../template/template-host.service'; templateUrl: './validator.component.html', providers: [TemplateHostService, PluginService] }) -export class ValidatorComponent implements OnInit { +export class ValidatorComponent implements OnInit, AfterViewInit { @Input() value: string; @Input() key: string; @Input() type: string; @@ -26,4 +26,8 @@ export class ValidatorComponent implements OnInit { const view = new ValidatorView(model, this); this.context = { $implicit: view }; } + + ngAfterViewInit() { + console.log(this.context.$implicit.value); + } } diff --git a/src/plugin/validation/validator.view.js b/src/plugin/validation/validator.view.js index a1dbd123c..6b24f23f2 100644 --- a/src/plugin/validation/validator.view.js +++ b/src/plugin/validation/validator.view.js @@ -7,6 +7,12 @@ export class ValidatorView { this.context = context; this.oldErrors = []; + // this.customValidator + // 'customFunction' + + // if (this.customFunction) { + // console.log(this.customFunction(1), this.customFunction(2), this.customFunction(4), this.customFunction(8)); + // } if (hasRules(this.rules, this.key)) { this.validator = createValidator(this.rules, this.key); } From d6781d0b93445c213b689363a68b2eb10cbd56b3 Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Tue, 10 Dec 2019 23:16:30 +0300 Subject: [PATCH 02/15] custom validation --- src/core/edit/edit.cell.view.js | 7 +- src/core/validation/validation.service.d.ts | 7 -- src/core/validation/validation.service.js | 39 --------- src/core/validation/validator.builder.d.ts | 18 +++++ src/core/validation/validator.builder.js | 81 +++++++++++++++++++ .../plugins/query-builder/schema/validator.ts | 4 +- src/lib/plugins/validation/rule.component.ts | 2 +- .../plugins/validation/validator.component.ts | 10 +-- src/plugin/validation/validator.view.d.ts | 2 +- src/plugin/validation/validator.view.js | 22 ++--- 10 files changed, 118 insertions(+), 74 deletions(-) delete mode 100644 src/core/validation/validation.service.d.ts delete mode 100644 src/core/validation/validation.service.js create mode 100644 src/core/validation/validator.builder.d.ts create mode 100644 src/core/validation/validator.builder.js diff --git a/src/core/edit/edit.cell.view.js b/src/core/edit/edit.cell.view.js index 6947524c9..f8dc442cd 100644 --- a/src/core/edit/edit.cell.view.js +++ b/src/core/edit/edit.cell.view.js @@ -6,7 +6,8 @@ import { getFactory as valueFactory } from '../services/value'; import { getFactory as labelFactory } from '../services/label'; import { parseFactory } from '../services/convert'; import { Td } from '../dom/td'; -import * as validationService from '../validation/validation.service'; +import * as validationService from '../validation/validator.builder'; +import {ValidatorBuilder} from "../validation/validator.builder"; export class EditCellView { constructor(model, table, shortcut) { @@ -148,7 +149,7 @@ export class EditCellView { if (canEdit) { const context = this.contextFactory(cell, this.value, this.label, this.tag); const key = context.column.key; - const validator = validationService.createValidator(model.validation().rules, key); + const validator = new ValidatorBuilder(model.validation().rules, key); return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value }); } return false; @@ -183,7 +184,7 @@ export class EditCellView { if (canEdit) { const context = this.contextFactory(cell, this.value, this.label, this.tag); const key = context.column.key; - const validator = validationService.createValidator(model.validation().rules, key); + const validator = new ValidatorBuilder(model.validation().rules, key); return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value }); } diff --git a/src/core/validation/validation.service.d.ts b/src/core/validation/validation.service.d.ts deleted file mode 100644 index e2ff1e002..000000000 --- a/src/core/validation/validation.service.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export declare interface Validator { - validate(value: any): boolean; - getErrors(): Array; -} - -export declare function hasRules(rules: any, key: string): boolean; -export declare function createValidator(rules: any, key?: string): Validator; diff --git a/src/core/validation/validation.service.js b/src/core/validation/validation.service.js deleted file mode 100644 index 7cdf96a71..000000000 --- a/src/core/validation/validation.service.js +++ /dev/null @@ -1,39 +0,0 @@ -import * as LIVR from 'livr'; - -export const {Validator} = LIVR; - -function toLIVRFormat(rules, key) { - const validationRules = []; - const customRules = []; - rules.forEach(rule => { - if (rule.key === key) { - for (let name of Object.keys(rule)) { - if (!['for', 'key'].includes(key)) { - const rulesArray = key === 'customFunction' ? customRules : validationRules; - rulesArray.push({ - [name]: rule[name] - }); - } - } - } - }); - return { - customRules: {[key]: customRules}, - rules: {[key]: validationRules}, - hasRules: validationRules.length + customRules.length > 0, - hasLIVRRules: validationRules.length > 0, - }; -} - -export function hasRules(rules, key) { - return toLIVRFormat(rules, key).hasRules; -} - -export function createValidator(rules, key) { - if (rules && key) { - const settings = toLIVRFormat(rules, key); - return new Validator(settings.rules); - } - - return new Validator(rules); -} diff --git a/src/core/validation/validator.builder.d.ts b/src/core/validation/validator.builder.d.ts new file mode 100644 index 000000000..ddb0e9944 --- /dev/null +++ b/src/core/validation/validator.builder.d.ts @@ -0,0 +1,18 @@ +export declare interface Validator { + validate(value: any): boolean; + getErrors(): { [key: string]: string }; +} + + +export declare interface ValidatorBuilder extends Validator { + rules: any; + livrRules: any; + errors: any; + livrValidator: Validator; + hasLivrRules: boolean; + hasCustomRules: boolean; + registerRules(rules: any, key: string): void; + hasRules(): boolean; +} + +export declare function createValidator(rules: any): Validator; diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js new file mode 100644 index 000000000..7519b546d --- /dev/null +++ b/src/core/validation/validator.builder.js @@ -0,0 +1,81 @@ +import * as LIVR from "livr"; + +export const {Validator: LivrValidator} = LIVR; + + +export function createLivrValidator(rules) { + return new LivrValidator(rules); +} + +export class ValidatorBuilder { + constructor(rules, key) { + this.rules = rules; + this.livrRules = null; + this.errors = null; + this.livrValidator = null; + this.hasLivrRules = false; + this.hasCustomRules = false; + + this.registerRules(rules, key); + } + + registerRules(rules, key) { + const livrRules = []; + const keyRules = rules.filter(r => r.key === key); + keyRules.forEach(rule => { + for (let name of Object.keys(rule)) { + if (name === 'customValidation') { + this.customRule = {[key]: rule[name]}; + this.hasCustomRules = !!this.customRule; + } else if (!['for', 'key'].includes(name)) { + livrRules.push({ + [name]: rule[name] + }); + } + } + }); + + this.hasLivrRules = livrRules.length > 0; + if (this.hasLivrRules) { + this.livrRules = {[key]: livrRules}; + this.livrValidator = new LivrValidator({[key]: livrRules}); + } + } + + get hasRules() { + return this.hasLivrRules || this.hasCustomRules; + } + + validate(data) { + let isValid = true; + + if (this.hasLivrRules) { + isValid = this.livrValidator.validate(data); + if (!isValid) { + this.errors = this.livrValidator.getErrors(); + } + } + + if (this.hasCustomRules) { + Object.keys(data) + .forEach(k => { + const isValidCustom = this.customRule[k].validationFunction(data[k]); + if (!isValidCustom) { + this.errors = this.errors || {}; + // this.errors[k] = 'CUSTOM_ERROR'; + this.errors[k] = this.customRule[k].validationMessage; + } + + isValid = isValid && isValidCustom; + }); + } + + return isValid; + } + + getErrors() { + return this.errors; + } + + +} diff --git a/src/lib/plugins/query-builder/schema/validator.ts b/src/lib/plugins/query-builder/schema/validator.ts index 551aa8dbf..6a49c8752 100644 --- a/src/lib/plugins/query-builder/schema/validator.ts +++ b/src/lib/plugins/query-builder/schema/validator.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { AppError } from 'ng2-qgrid/core/infrastructure/error'; import { isArray } from 'ng2-qgrid/core/utility/kit'; import { Column, QueryBuilderService, ColumnMap } from '../query-builder.service'; -import { createValidator } from 'ng2-qgrid/core/validation/validation.service'; +import { createLivrValidator } from 'ng2-qgrid/core/validation/validator.builder'; export class Validator { private columnMap: ColumnMap; @@ -56,7 +56,7 @@ export class Validator { } const target = { [id]: value }; - const validator = createValidator(schema); + const validator = createLivrValidator(schema); const isValid = validator.validate(target); if (isValid) { return trueResult; diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index 7d496c278..6ee2ae467 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -40,7 +40,7 @@ export class RuleComponent implements OnChanges { @Input('isoDate') iso_date?: string; @Input('equalToField') equal_to_field?: string; @Input('listOf') list_of?: string; - @Input('customFunction') customFunction?: Function = null; + @Input('customValidation') customValidation?; @ViewChild(TemplateRef) templateRef: TemplateRef; diff --git a/src/lib/plugins/validation/validator.component.ts b/src/lib/plugins/validation/validator.component.ts index 7d01fe974..a6fa8665e 100644 --- a/src/lib/plugins/validation/validator.component.ts +++ b/src/lib/plugins/validation/validator.component.ts @@ -1,4 +1,4 @@ -import { AfterViewInit, Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; import { ValidatorView } from 'ng2-qgrid/plugin/validation/validator.view'; import { PluginService } from '../plugin.service'; import { TemplateHostService } from '../../template/template-host.service'; @@ -8,7 +8,7 @@ import { TemplateHostService } from '../../template/template-host.service'; templateUrl: './validator.component.html', providers: [TemplateHostService, PluginService] }) -export class ValidatorComponent implements OnInit, AfterViewInit { +export class ValidatorComponent implements OnInit { @Input() value: string; @Input() key: string; @Input() type: string; @@ -16,7 +16,7 @@ export class ValidatorComponent implements OnInit, AfterViewInit { constructor( private plugin: PluginService, - private templateHost: TemplateHostService + private templateHost: TemplateHostService, ) { this.templateHost.key = () => `validator`; } @@ -26,8 +26,4 @@ export class ValidatorComponent implements OnInit, AfterViewInit { const view = new ValidatorView(model, this); this.context = { $implicit: view }; } - - ngAfterViewInit() { - console.log(this.context.$implicit.value); - } } diff --git a/src/plugin/validation/validator.view.d.ts b/src/plugin/validation/validator.view.d.ts index 2e6d36ff8..15385f187 100644 --- a/src/plugin/validation/validator.view.d.ts +++ b/src/plugin/validation/validator.view.d.ts @@ -1,5 +1,5 @@ import { Model } from '../../core/infrastructure/model'; -import { Validator } from '../../core/validation/validation.service'; +import { Validator } from '../../core/validation/validator.builder'; export declare class ValidatorView { constructor(model: Model, context: any); diff --git a/src/plugin/validation/validator.view.js b/src/plugin/validation/validator.view.js index 6b24f23f2..92b6ef643 100644 --- a/src/plugin/validation/validator.view.js +++ b/src/plugin/validation/validator.view.js @@ -1,5 +1,5 @@ import { isString, isEqual } from '../../core/utility/kit'; -import { hasRules, createValidator } from '../../core/validation/validation.service'; +import { ValidatorBuilder } from '../../core/validation/validator.builder'; export class ValidatorView { constructor(model, context) { @@ -7,15 +7,7 @@ export class ValidatorView { this.context = context; this.oldErrors = []; - // this.customValidator - // 'customFunction' - - // if (this.customFunction) { - // console.log(this.customFunction(1), this.customFunction(2), this.customFunction(4), this.customFunction(8)); - // } - if (hasRules(this.rules, this.key)) { - this.validator = createValidator(this.rules, this.key); - } + this.validator = new ValidatorBuilder(this.rules, this.key); } get errors() { @@ -55,11 +47,13 @@ export class ValidatorView { } stringify(errors) { - const customErrors = []; - let rules = {}; - for (const rule of this.validator.livrRules[this.key]) { - rules = Object.assign(rules, rule); + if (!this.validator.hasRules){ + return []; + } else if (!this.validator.hasLivrRules){ + return errors; } + const customErrors = []; + const rules = {...this.validator.livrRules[this.key]}; for (const error of errors) { switch (error) { From ef3866dfb6a77dd2730dcb3d9b3cd037a07c2641 Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Tue, 10 Dec 2019 23:17:25 +0300 Subject: [PATCH 03/15] custom validation example --- .../example-validation-basic.component.html | 46 ++++++++++++++++++- .../example-validation-basic.component.ts | 9 +++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/examples/validation-basic/example-validation-basic.component.html b/src/examples/validation-basic/example-validation-basic.component.html index 43e67d926..fa2b0c98a 100644 --- a/src/examples/validation-basic/example-validation-basic.component.html +++ b/src/examples/validation-basic/example-validation-basic.component.html @@ -29,13 +29,57 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/examples/validation-basic/example-validation-basic.component.ts b/src/examples/validation-basic/example-validation-basic.component.ts index f4a875071..cec343df8 100644 --- a/src/examples/validation-basic/example-validation-basic.component.ts +++ b/src/examples/validation-basic/example-validation-basic.component.ts @@ -20,7 +20,14 @@ export class ExampleValidationBasicComponent { this.rows = dataService.getAtoms(); } + checkNumberCustom() { + return { + validationMessage: 'Should be > 1800 and even', + validationFunction: this.checkNumber + }; + } + checkNumber(v) { - return v > 5; + return v > 1800 && v % 2 === 0; } } From 592ada68641b88407aaeaf2db4e2b2933498493b Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Tue, 10 Dec 2019 23:22:58 +0300 Subject: [PATCH 04/15] clean up code style --- src/core/validation/validator.builder.js | 5 ++--- src/lib/plugins/validation/rule.component.ts | 2 +- src/lib/plugins/validation/validator.component.ts | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index 7519b546d..3c6446487 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -1,6 +1,6 @@ -import * as LIVR from "livr"; +import * as LIVR from 'livr'; -export const {Validator: LivrValidator} = LIVR; +export const { Validator: LivrValidator } = LIVR; export function createLivrValidator(rules) { @@ -62,7 +62,6 @@ export class ValidatorBuilder { const isValidCustom = this.customRule[k].validationFunction(data[k]); if (!isValidCustom) { this.errors = this.errors || {}; - // this.errors[k] = 'CUSTOM_ERROR'; this.errors[k] = this.customRule[k].validationMessage; } diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index 6ee2ae467..d2839013f 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -50,7 +50,7 @@ export class RuleComponent implements OnChanges { constructor( private plugin: PluginService, - private templateHost: TemplateHostService, + private templateHost: TemplateHostService ) { this.templateHost.key = () => `rule`; } diff --git a/src/lib/plugins/validation/validator.component.ts b/src/lib/plugins/validation/validator.component.ts index a6fa8665e..b3e563a97 100644 --- a/src/lib/plugins/validation/validator.component.ts +++ b/src/lib/plugins/validation/validator.component.ts @@ -16,7 +16,7 @@ export class ValidatorComponent implements OnInit { constructor( private plugin: PluginService, - private templateHost: TemplateHostService, + private templateHost: TemplateHostService ) { this.templateHost.key = () => `validator`; } From 361cbcf2d0dcd2592f1c71c6e0b6fb4889e37daa Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Wed, 11 Dec 2019 10:30:25 +0300 Subject: [PATCH 05/15] add spaces --- src/core/edit/edit.cell.view.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/edit/edit.cell.view.js b/src/core/edit/edit.cell.view.js index f8dc442cd..c098a03f3 100644 --- a/src/core/edit/edit.cell.view.js +++ b/src/core/edit/edit.cell.view.js @@ -6,8 +6,7 @@ import { getFactory as valueFactory } from '../services/value'; import { getFactory as labelFactory } from '../services/label'; import { parseFactory } from '../services/convert'; import { Td } from '../dom/td'; -import * as validationService from '../validation/validator.builder'; -import {ValidatorBuilder} from "../validation/validator.builder"; +import { ValidatorBuilder } from "../validation/validator.builder"; export class EditCellView { constructor(model, table, shortcut) { From 60b5c491d5a488d3b2682c1a699be3f1c7b1287b Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Wed, 11 Dec 2019 16:28:09 +0300 Subject: [PATCH 06/15] use livr register rule method --- src/core/edit/edit.cell.view.js | 6 +- src/core/validation/validator.builder.d.ts | 13 ++-- src/core/validation/validator.builder.js | 67 +++++-------------- .../example-validation-basic.component.html | 2 +- .../example-validation-basic.component.ts | 12 ++-- src/lib/plugins/validation/rule.component.ts | 2 +- src/plugin/validation/validator.view.js | 13 ++-- 7 files changed, 39 insertions(+), 76 deletions(-) diff --git a/src/core/edit/edit.cell.view.js b/src/core/edit/edit.cell.view.js index c098a03f3..5682f1132 100644 --- a/src/core/edit/edit.cell.view.js +++ b/src/core/edit/edit.cell.view.js @@ -148,7 +148,8 @@ export class EditCellView { if (canEdit) { const context = this.contextFactory(cell, this.value, this.label, this.tag); const key = context.column.key; - const validator = new ValidatorBuilder(model.validation().rules, key); + const validatorBuilder = new ValidatorBuilder(model.validation().rules, key); + const { validator } = validatorBuilder; return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value }); } return false; @@ -183,7 +184,8 @@ export class EditCellView { if (canEdit) { const context = this.contextFactory(cell, this.value, this.label, this.tag); const key = context.column.key; - const validator = new ValidatorBuilder(model.validation().rules, key); + const validatorBuilder = new ValidatorBuilder(model.validation().rules, key); + const { validator } = validatorBuilder; return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value }); } diff --git a/src/core/validation/validator.builder.d.ts b/src/core/validation/validator.builder.d.ts index ddb0e9944..782862a9e 100644 --- a/src/core/validation/validator.builder.d.ts +++ b/src/core/validation/validator.builder.d.ts @@ -1,18 +1,15 @@ export declare interface Validator { validate(value: any): boolean; - getErrors(): { [key: string]: string }; + getErrors(): Array; } -export declare interface ValidatorBuilder extends Validator { - rules: any; - livrRules: any; - errors: any; - livrValidator: Validator; - hasLivrRules: boolean; +export declare interface ValidatorBuilder { + validator: Validator; + hasCommonRules: boolean; hasCustomRules: boolean; registerRules(rules: any, key: string): void; hasRules(): boolean; } -export declare function createValidator(rules: any): Validator; +export declare function createLivrValidator(rules: any): Validator; diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index 3c6446487..74b95c814 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -2,79 +2,48 @@ import * as LIVR from 'livr'; export const { Validator: LivrValidator } = LIVR; - export function createLivrValidator(rules) { return new LivrValidator(rules); } export class ValidatorBuilder { constructor(rules, key) { - this.rules = rules; - this.livrRules = null; - this.errors = null; - this.livrValidator = null; - this.hasLivrRules = false; + this.validator = null; + this.hasCommonRules = false; this.hasCustomRules = false; this.registerRules(rules, key); } - registerRules(rules, key) { - const livrRules = []; - const keyRules = rules.filter(r => r.key === key); + registerRules(srcRules, key) { + const rules = []; + const keyRules = srcRules.filter(r => r.key === key); keyRules.forEach(rule => { for (let name of Object.keys(rule)) { - if (name === 'customValidation') { + if (name === 'custom_validation') { this.customRule = {[key]: rule[name]}; - this.hasCustomRules = !!this.customRule; - } else if (!['for', 'key'].includes(name)) { - livrRules.push({ + this.hasCustomRules = true; + } + if (!['for', 'key'].includes(name)) { + rules.push({ [name]: rule[name] }); } } }); - this.hasLivrRules = livrRules.length > 0; - if (this.hasLivrRules) { - this.livrRules = {[key]: livrRules}; - this.livrValidator = new LivrValidator({[key]: livrRules}); - } - } - - get hasRules() { - return this.hasLivrRules || this.hasCustomRules; - } - - validate(data) { - let isValid = true; - - if (this.hasLivrRules) { - isValid = this.livrValidator.validate(data); - if (!isValid) { - this.errors = this.livrValidator.getErrors(); - } - } - + this.hasCommonRules = rules.length > 0; + const livrRules = this.hasCommonRules ? {[key]: rules} : {}; + this.validator = new LivrValidator(livrRules); if (this.hasCustomRules) { - Object.keys(data) - .forEach(k => { - const isValidCustom = this.customRule[k].validationFunction(data[k]); - if (!isValidCustom) { - this.errors = this.errors || {}; - this.errors[k] = this.customRule[k].validationMessage; - } - - isValid = isValid && isValidCustom; - }); + this.validator.registerRules({ + custom_validation:() => this.customRule[key] + }) } - - return isValid; } - getErrors() { - return this.errors; + get hasRules() { + return this.hasCommonRules || this.hasCustomRules; } - } diff --git a/src/examples/validation-basic/example-validation-basic.component.html b/src/examples/validation-basic/example-validation-basic.component.html index fa2b0c98a..2fbf3abca 100644 --- a/src/examples/validation-basic/example-validation-basic.component.html +++ b/src/examples/validation-basic/example-validation-basic.component.html @@ -78,7 +78,7 @@ + [customValidation]="checkNumber"> diff --git a/src/examples/validation-basic/example-validation-basic.component.ts b/src/examples/validation-basic/example-validation-basic.component.ts index cec343df8..f6e0aa00b 100644 --- a/src/examples/validation-basic/example-validation-basic.component.ts +++ b/src/examples/validation-basic/example-validation-basic.component.ts @@ -20,14 +20,10 @@ export class ExampleValidationBasicComponent { this.rows = dataService.getAtoms(); } - checkNumberCustom() { - return { - validationMessage: 'Should be > 1800 and even', - validationFunction: this.checkNumber - }; - } - checkNumber(v) { - return v > 1800 && v % 2 === 0; + const isValid = v > 1800 && v % 2 === 0; + if (!isValid) { + return 'Should be > 1800 and even'; + } } } diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index d2839013f..14b1db807 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -40,7 +40,7 @@ export class RuleComponent implements OnChanges { @Input('isoDate') iso_date?: string; @Input('equalToField') equal_to_field?: string; @Input('listOf') list_of?: string; - @Input('customValidation') customValidation?; + @Input('customValidation') custom_validation?; @ViewChild(TemplateRef) templateRef: TemplateRef; diff --git a/src/plugin/validation/validator.view.js b/src/plugin/validation/validator.view.js index 92b6ef643..fec37841c 100644 --- a/src/plugin/validation/validator.view.js +++ b/src/plugin/validation/validator.view.js @@ -7,7 +7,8 @@ export class ValidatorView { this.context = context; this.oldErrors = []; - this.validator = new ValidatorBuilder(this.rules, this.key); + const validatorBuilder = new ValidatorBuilder(this.rules, this.key); + this.validator = validatorBuilder.validator; } get errors() { @@ -47,13 +48,11 @@ export class ValidatorView { } stringify(errors) { - if (!this.validator.hasRules){ - return []; - } else if (!this.validator.hasLivrRules){ - return errors; - } const customErrors = []; - const rules = {...this.validator.livrRules[this.key]}; + let rules = {}; + for (const rule of this.validator.livrRules[this.key]) { + rules = Object.assign(rules, rule); + } for (const error of errors) { switch (error) { From 929f8eb1cf160fd2265de4e10c377aafb41adc1d Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Wed, 11 Dec 2019 16:42:40 +0300 Subject: [PATCH 07/15] quotes --- src/core/edit/edit.cell.view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/edit/edit.cell.view.js b/src/core/edit/edit.cell.view.js index 5682f1132..283bfd4e4 100644 --- a/src/core/edit/edit.cell.view.js +++ b/src/core/edit/edit.cell.view.js @@ -6,7 +6,7 @@ import { getFactory as valueFactory } from '../services/value'; import { getFactory as labelFactory } from '../services/label'; import { parseFactory } from '../services/convert'; import { Td } from '../dom/td'; -import { ValidatorBuilder } from "../validation/validator.builder"; +import { ValidatorBuilder } from '../validation/validator.builder'; export class EditCellView { constructor(model, table, shortcut) { From 71e4f90298f1224ed0db60577bf7c1aa313dbf38 Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Wed, 11 Dec 2019 16:46:57 +0300 Subject: [PATCH 08/15] rename createLivrValidator to createValidator --- src/core/validation/validator.builder.d.ts | 2 +- src/core/validation/validator.builder.js | 2 +- src/lib/plugins/query-builder/schema/validator.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/validation/validator.builder.d.ts b/src/core/validation/validator.builder.d.ts index 782862a9e..59b630265 100644 --- a/src/core/validation/validator.builder.d.ts +++ b/src/core/validation/validator.builder.d.ts @@ -12,4 +12,4 @@ export declare interface ValidatorBuilder { hasRules(): boolean; } -export declare function createLivrValidator(rules: any): Validator; +export declare function createValidator(rules: any): Validator; diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index 74b95c814..9f15bf929 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -2,7 +2,7 @@ import * as LIVR from 'livr'; export const { Validator: LivrValidator } = LIVR; -export function createLivrValidator(rules) { +export function createValidator(rules) { return new LivrValidator(rules); } diff --git a/src/lib/plugins/query-builder/schema/validator.ts b/src/lib/plugins/query-builder/schema/validator.ts index 6a49c8752..c41baf4ab 100644 --- a/src/lib/plugins/query-builder/schema/validator.ts +++ b/src/lib/plugins/query-builder/schema/validator.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { AppError } from 'ng2-qgrid/core/infrastructure/error'; import { isArray } from 'ng2-qgrid/core/utility/kit'; import { Column, QueryBuilderService, ColumnMap } from '../query-builder.service'; -import { createLivrValidator } from 'ng2-qgrid/core/validation/validator.builder'; +import { createValidator } from 'ng2-qgrid/core/validation/validator.builder'; export class Validator { private columnMap: ColumnMap; @@ -56,7 +56,7 @@ export class Validator { } const target = { [id]: value }; - const validator = createLivrValidator(schema); + const validator = createValidator(schema); const isValid = validator.validate(target); if (isValid) { return trueResult; From b762c92dc93d5abae54317ae04a3d80dfc9e58e6 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Wed, 12 Feb 2020 13:20:26 +0300 Subject: [PATCH 09/15] change version of grid --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index dd4632b57..228ef78ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ng2-qgrid", - "version": "7.4.0", + "version": "7.5.1", "lockfileVersion": 1, "requires": true, "dependencies": { From 9f966e28b09bdf723df8dd22866d04c92a1c9905 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Wed, 12 Feb 2020 14:28:23 +0300 Subject: [PATCH 10/15] remove unnecessary empty line --- src/core/validation/validator.builder.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/validation/validator.builder.d.ts b/src/core/validation/validator.builder.d.ts index 59b630265..799272774 100644 --- a/src/core/validation/validator.builder.d.ts +++ b/src/core/validation/validator.builder.d.ts @@ -3,7 +3,6 @@ export declare interface Validator { getErrors(): Array; } - export declare interface ValidatorBuilder { validator: Validator; hasCommonRules: boolean; From f700b8fbfda34c412f4f8f47fe0f9b2aa3e27a9a Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Wed, 12 Feb 2020 14:41:15 +0300 Subject: [PATCH 11/15] rename custom validation binding name --- src/core/validation/validator.builder.js | 4 ++-- .../example-validation-basic.component.html | 2 +- src/lib/plugins/validation/rule.component.ts | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index 9f15bf929..afea2f091 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -20,7 +20,7 @@ export class ValidatorBuilder { const keyRules = srcRules.filter(r => r.key === key); keyRules.forEach(rule => { for (let name of Object.keys(rule)) { - if (name === 'custom_validation') { + if (name === 'test') { this.customRule = {[key]: rule[name]}; this.hasCustomRules = true; } @@ -37,7 +37,7 @@ export class ValidatorBuilder { this.validator = new LivrValidator(livrRules); if (this.hasCustomRules) { this.validator.registerRules({ - custom_validation:() => this.customRule[key] + test:() => this.customRule[key] }) } } diff --git a/src/examples/validation-basic/example-validation-basic.component.html b/src/examples/validation-basic/example-validation-basic.component.html index 2fbf3abca..f2a04aec9 100644 --- a/src/examples/validation-basic/example-validation-basic.component.html +++ b/src/examples/validation-basic/example-validation-basic.component.html @@ -78,7 +78,7 @@ + [test]="checkNumber"> diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index 14b1db807..4a61ea7a4 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -40,7 +40,9 @@ export class RuleComponent implements OnChanges { @Input('isoDate') iso_date?: string; @Input('equalToField') equal_to_field?: string; @Input('listOf') list_of?: string; - @Input('customValidation') custom_validation?; + + // Custom validation rules + @Input('test') test?; @ViewChild(TemplateRef) templateRef: TemplateRef; @@ -52,7 +54,7 @@ export class RuleComponent implements OnChanges { private plugin: PluginService, private templateHost: TemplateHostService ) { - this.templateHost.key = () => `rule`; + this.templateHost.key = () => 'rule'; } ngOnChanges(changes: SimpleChanges) { From bdee661c4327b65b7a8503458bef5629fcc5f384 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Wed, 12 Feb 2020 14:46:16 +0300 Subject: [PATCH 12/15] add function type to test input --- src/lib/plugins/validation/rule.component.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/plugins/validation/rule.component.ts b/src/lib/plugins/validation/rule.component.ts index 4a61ea7a4..60b21cb4e 100644 --- a/src/lib/plugins/validation/rule.component.ts +++ b/src/lib/plugins/validation/rule.component.ts @@ -11,6 +11,9 @@ export class RuleComponent implements OnChanges { @Input() for: string; @Input() key: string; + // Custom validation rules + @Input() test?: Function; + // Common Rules @Input() required?: string; @Input('notEmptyList') not_empty_list?: string; @@ -41,9 +44,6 @@ export class RuleComponent implements OnChanges { @Input('equalToField') equal_to_field?: string; @Input('listOf') list_of?: string; - // Custom validation rules - @Input('test') test?; - @ViewChild(TemplateRef) templateRef: TemplateRef; context: { $implicit: RuleComponent } = { From 54583652a6571a31f8c933d3b628196bc2bb79c6 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Fri, 14 Feb 2020 17:37:07 +0300 Subject: [PATCH 13/15] validator fetch --- src/core/validation/validator.builder.d.ts | 1 + src/core/validation/validator.builder.js | 2 ++ src/lib/plugins/validation/validator.component.ts | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/validation/validator.builder.d.ts b/src/core/validation/validator.builder.d.ts index 799272774..500ffa5ba 100644 --- a/src/core/validation/validator.builder.d.ts +++ b/src/core/validation/validator.builder.d.ts @@ -5,6 +5,7 @@ export declare interface Validator { export declare interface ValidatorBuilder { validator: Validator; + fetch: () => void; hasCommonRules: boolean; hasCustomRules: boolean; registerRules(rules: any, key: string): void; diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index afea2f091..b05c28ae0 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -1,4 +1,5 @@ import * as LIVR from 'livr'; +import { Fetch } from '../infrastructure/fetch'; export const { Validator: LivrValidator } = LIVR; @@ -11,6 +12,7 @@ export class ValidatorBuilder { this.validator = null; this.hasCommonRules = false; this.hasCustomRules = false; + this.fetch = new Fetch(this.validator); this.registerRules(rules, key); } diff --git a/src/lib/plugins/validation/validator.component.ts b/src/lib/plugins/validation/validator.component.ts index b3e563a97..f75cd55c0 100644 --- a/src/lib/plugins/validation/validator.component.ts +++ b/src/lib/plugins/validation/validator.component.ts @@ -18,7 +18,7 @@ export class ValidatorComponent implements OnInit { private plugin: PluginService, private templateHost: TemplateHostService ) { - this.templateHost.key = () => `validator`; + this.templateHost.key = () => 'validator'; } ngOnInit() { From 752afb18507947b62f2a801caf15099df4e92079 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Fri, 14 Feb 2020 18:00:38 +0300 Subject: [PATCH 14/15] validator fetch --- src/core/validation/validator.builder.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/validation/validator.builder.js b/src/core/validation/validator.builder.js index b05c28ae0..24e7c4954 100644 --- a/src/core/validation/validator.builder.js +++ b/src/core/validation/validator.builder.js @@ -12,7 +12,7 @@ export class ValidatorBuilder { this.validator = null; this.hasCommonRules = false; this.hasCustomRules = false; - this.fetch = new Fetch(this.validator); + this.fetch = this.fetchFactory(); this.registerRules(rules, key); } @@ -44,6 +44,10 @@ export class ValidatorBuilder { } } + fetchFactory() { + return new Fetch(this.validator); + } + get hasRules() { return this.hasCommonRules || this.hasCustomRules; } From c124d288dd32e6e82f8f93cff7e731296bbb3c37 Mon Sep 17 00:00:00 2001 From: Ekaterina Tokareva Date: Tue, 25 Feb 2020 17:15:25 +0300 Subject: [PATCH 15/15] version updates --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 77fa130f0..24fbbd344 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ng2-qgrid", - "version": "7.5.1", + "version": "8.0.0", "lockfileVersion": 1, "requires": true, "dependencies": {