Add interface Rules

This commit is contained in:
Matías Olivera
2019-01-06 17:20:06 -03:00
parent 19d40360f8
commit 1e6edecd00
2 changed files with 62 additions and 1 deletions
+7
View File
@@ -6,6 +6,13 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace Validator {
interface TypeCheckingRule {
[attribute: string]: Array<any>;
}
interface Rules {
[attribute: string]: string | Array<string | TypeCheckingRule> | Rules;
}
interface ValidationErrors {
[field: string]: string;
}
+55 -1
View File
@@ -1,6 +1,60 @@
import Validator = require('validatorjs');
var rules: any = {
/**
* Validation rules
*/
function objectRules() {
const data: any = { name: 'John' };
const rules: Validator.Rules = { name: 'required' };
const validation = new Validator(data, rules);
}
function nestedRules() {
const data: any = { name: 'John', bio: { age: 28 } };
const nested: Validator.Rules = {
name: 'required',
bio: { age: 'min:18' }
};
const flattened: Validator.Rules = {
name: 'required',
'bio.age': 'min:18'
};
const validation1 = new Validator(data, nested);
const validation2 = new Validator(data, flattened);
}
function wildcardRules() {
const data: any = {
users: [{ name: 'John', bio: { age: 28 } }]
};
const rules: Validator.Rules = {
'users.*.name': 'required',
'users.*.bio.age': 'min:18'
};
const validation = new Validator(data, rules);
}
function arrayRules() {
const data: any = { name: 'John' };
const rules: Validator.Rules = { name: ['required', 'string'] };
const validation = new Validator(data, rules);
}
function typeCheckingRules() {
const data: any = { age: 30, name: '' };
const rules: Validator.Rules = {
age: ['required', { in: [29, 30] }],
name: [{ required_if: ['age', 30] }]
};
const validation = new Validator(data, rules);
}
var rules: Validator.Rules = {
foo: 'required'
};