From 1e6edecd0089c3f75989fde122f438d61eed7305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Olivera?= Date: Sun, 6 Jan 2019 17:03:04 -0300 Subject: [PATCH] Add interface Rules --- types/validatorjs/index.d.ts | 7 ++++ types/validatorjs/validatorjs-tests.ts | 56 +++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/types/validatorjs/index.d.ts b/types/validatorjs/index.d.ts index 1a41866d0f..a084866805 100644 --- a/types/validatorjs/index.d.ts +++ b/types/validatorjs/index.d.ts @@ -6,6 +6,13 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace Validator { + interface TypeCheckingRule { + [attribute: string]: Array; + } + interface Rules { + [attribute: string]: string | Array | Rules; + } + interface ValidationErrors { [field: string]: string; } diff --git a/types/validatorjs/validatorjs-tests.ts b/types/validatorjs/validatorjs-tests.ts index 4ab3e28159..9c4255c757 100644 --- a/types/validatorjs/validatorjs-tests.ts +++ b/types/validatorjs/validatorjs-tests.ts @@ -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' };