Skip to content

Commit

Permalink
feat: add object rule type
Browse files Browse the repository at this point in the history
  • Loading branch information
ckvv committed Sep 30, 2021
1 parent f123301 commit caeaef0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ example
}
}
```

### object
If type is `object`, There are the following rules
+ `rule`- An object that validate the properties ot the object

example
```js
{
people: {
type: 'object',
rule: {
name: 'string',
age: {
isRequired: false,
type: 'int',
min: 1,
max: 200
}
}
}
}
```
### custom rule

definition custom rule, example
Expand Down
67 changes: 43 additions & 24 deletions __test__/parameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,6 @@ import { Parameter } from '../src/parameter';
describe(__filename, () => {
test('Parameter', async () => {
const parameter = new Parameter({});
console.log(parameter.validate({
password: {
type: 'string',
regexp: /.{6}/
},
role: {
type: 'enum',
enum: ['admin', 'user']
},
name: {
type: 'string',
min: 2,
max: 20
},
age: {
type: 'int',
min: 1,
max: 200
}
}, {
password: 'h',
name: 'x',
age: -10
}));
expect(parameter.validate({
password: {
type: 'string',
Expand Down Expand Up @@ -236,4 +212,47 @@ describe(__filename, () => {
someNumber: 2
})).toEqual(null);
});

test('Parameter object', async () => {
const parameter = new Parameter();
expect(parameter.validate({
people: {
type: 'object',
rule: {
name: 'string',
age: {
isRequired: false,
type: 'int',
min: 1,
max: 200
}
}
}
}, {
people: {
name: 'xiao hong',
age: 45
}
})).toEqual(null);

expect(parameter.validate({
people: {
type: 'object',
rule: {
name: 'string',
age: {
isRequired: false,
type: 'int',
min: 1,
max: 200
}
}
}
}, {
people: {
name: 'xiao hong',
age: 'xxx'
}
})).toEqual([{ code: 'invalid', field: 'people', message: [{ code: 'invalid', field: 'age', message: 'should be a integer' }] }]);
});
});
13 changes: 11 additions & 2 deletions src/checkers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const DEF_CHECKERS: {
boolean: checkFunction,
enum: checkFunction,
custom: checkFunction,
array: checkFunction
array: checkFunction,
object: checkFunction,
[key:string]: checkFunction,
} = <any>{};

Expand Down Expand Up @@ -141,12 +142,20 @@ const checkArray = (rule:Rule, value: any) => {
return errors.length ? errors : null;
};

const checkObject = function (this:any, rule:Rule, value: any) {
if (typeof value !== 'object') {
return 'should be a object';
}
return rule.rule ? this.validate(rule.rule, value) : null;
};

Object.assign(DEF_CHECKERS, {
number: checkNumber,
int: checkInt,
string: checkString,
boolean: checkBoolean,
enum: checkEnum,
custom: checkCustom,
array: checkArray
array: checkArray,
object: checkObject
});

0 comments on commit caeaef0

Please sign in to comment.