Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@
"xlsx": "^0.14.3",
"ng2-qgrid": "*"
}
}
}
8 changes: 5 additions & 3 deletions src/core/edit/edit.cell.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CellEditor } from './edit.cell.editor';
import { getFactory as valueFactory } from '../services/value';
import { getFactory as labelFactory } from '../services/label';
import { parseFactory } from '../services/convert';
import * as validationService from '../validation/validation.service';
import { ValidatorBuilder } from '../validation/validator.builder';

// do not delete this importing it's required in the bundle
// TODO: investigate how to avoid it
Expand Down Expand Up @@ -151,7 +151,8 @@ 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 validatorBuilder = new ValidatorBuilder(model.validation().rules, key);
const { validator } = validatorBuilder;
return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value });
}
return false;
Expand Down Expand Up @@ -186,7 +187,8 @@ 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 validatorBuilder = new ValidatorBuilder(model.validation().rules, key);
const { validator } = validatorBuilder;
return model.edit().commit.canExecute(context) && validator.validate({ [key]: this.value });
}

Expand Down
7 changes: 0 additions & 7 deletions src/core/validation/validation.service.d.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/core/validation/validation.service.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/core/validation/validator.builder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export declare interface Validator {
validate(value: any): boolean;
getErrors(): Array<string>;
}


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;
49 changes: 49 additions & 0 deletions src/core/validation/validator.builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as LIVR from 'livr';

export const { Validator: LivrValidator } = LIVR;

export function createValidator(rules) {
return new LivrValidator(rules);
}

export class ValidatorBuilder {
constructor(rules, key) {
this.validator = null;
this.hasCommonRules = false;
this.hasCustomRules = false;

this.registerRules(rules, 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 === 'custom_validation') {
this.customRule = {[key]: rule[name]};
this.hasCustomRules = true;
}
if (!['for', 'key'].includes(name)) {
rules.push({
[name]: rule[name]
});
}
}
});

this.hasCommonRules = rules.length > 0;
const livrRules = this.hasCommonRules ? {[key]: rules} : {};
this.validator = new LivrValidator(livrRules);
if (this.hasCustomRules) {
this.validator.registerRules({
custom_validation:() => this.customRule[key]
})
}
}

get hasRules() {
return this.hasCommonRules || this.hasCustomRules;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<q-grid-column key="source"
title="Url">
</q-grid-column>
<q-grid-column key="discoveredIn"
title="Should be > 1800 and even">
</q-grid-column>
</q-grid-columns>
<q-grid-validation>
<q-grid-rule for="cell"
Expand Down Expand Up @@ -73,6 +76,10 @@
key="source"
url>
</q-grid-rule>
<q-grid-rule for="cell"
key="discoveredIn"
[customValidation]="checkNumber">
</q-grid-rule>
</q-grid-validation>

</q-grid>
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ export class ExampleValidationBasicComponent {
constructor(dataService: DataService) {
this.rows = dataService.getAtoms();
}

checkNumber(v) {
const isValid = v > 1800 && v % 2 === 0;
if (!isValid) {
return 'Should be > 1800 and even';
}
}
}
1 change: 1 addition & 0 deletions src/lib/infrastructure/component/model.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class ModelComponent extends NgComponent implements OnChanges, OnInit, On
binder = new ModelBinder(this);
commit = noop;


constructor(public root: RootService) {
super();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/query-builder/schema/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { createValidator } from 'ng2-qgrid/core/validation/validator.builder';

export class Validator {
private columnMap: ColumnMap;
Expand Down
1 change: 1 addition & 0 deletions src/lib/plugins/validation/rule.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('customValidation') custom_validation?;

@ViewChild(TemplateRef) templateRef: TemplateRef<any>;

Expand Down
2 changes: 1 addition & 1 deletion src/plugin/validation/validator.view.d.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/plugin/validation/validator.view.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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) {
this.model = model;
this.context = context;

this.oldErrors = [];
if (hasRules(this.rules, this.key)) {
this.validator = createValidator(this.rules, this.key);
}
const validatorBuilder = new ValidatorBuilder(this.rules, this.key);
this.validator = validatorBuilder.validator;
}

get errors() {
Expand Down