mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-03 19:51:53 +00:00
Add interface Rules
This commit is contained in:
Vendored
+7
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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'
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user