Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit 7e56018

Browse files
Novo readme.me
1 parent 923b43f commit 7e56018

1 file changed

Lines changed: 105 additions & 153 deletions

File tree

README.md

Lines changed: 105 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,129 @@
11
# node-schema-validator
22
A package for objects validations using javascript.
33

4-
(Works only with Node 7 or superior)
5-
```js
6-
/* On your controller */
7-
8-
const params = {
9-
companyName: 'GitHub',
10-
contributors: 100,
11-
listOfContributors: ['people1', 'people2', 'people3']
12-
};
134

14-
try {
15-
scope.isValid(params);
16-
} catch (error) {
17-
// Error handling
18-
}
19-
```
5+
##Demo
206

217
```js
22-
/* On your scope */
8+
const {Scope, InvalidParam} = require('node-schema-validator');
239

24-
const Scope = require('node-schema-validator');
10+
try {
11+
const schema = new Scope();
2512

26-
module.exports = {
27-
isValid
28-
};
13+
const params = {
14+
name: 'Mandruva of the world',
15+
teams: [
16+
{
17+
name: 'SafaWar',
18+
members: [
19+
{
20+
name: 'Safadão',
21+
nick: 'Safawar'
22+
}
23+
]
24+
},
25+
{
26+
name: 'Didia FC',
27+
members: [
28+
{
29+
name: 'Didi',
30+
nick: 'didia'
31+
}
32+
]
33+
},
34+
{
35+
name: 'Paneleira',
36+
members: [
37+
{
38+
name: 'Panela',
39+
nick: 'srPanela'
40+
}
41+
]
42+
},
43+
{
44+
name: 'Ladraozera',
45+
members: [
46+
{
47+
name: 'Ladraozinho',
48+
nick: 'ladron'
49+
}
50+
]
51+
}
52+
]
53+
};
2954

30-
async function isValid(params) {
31-
const validation = {
32-
companyName: {
33-
required: true,
55+
const scope = {
56+
name: {
3457
type: String,
35-
maxLength: 40,
36-
minLength: 6
58+
maxLength: 32,
59+
required: true
3760
},
38-
contributors: {
39-
required: true,
40-
type: Number,
41-
minNumber: 30,
42-
maxNumber: 100
43-
},
44-
listOfContributors: {
45-
required: true,
61+
teams: {
4662
type: Array,
47-
// here we tell the validator what type of data we are going to handle on this array
48-
rules: {
49-
type: String,
50-
minLength: 4
63+
maxSize: 16,
64+
minSize: 4,
65+
childs: {
66+
name: {
67+
type: String,
68+
maxLength: 32,
69+
required: true
70+
},
71+
members: {
72+
type: Array,
73+
minSize: 1,
74+
maxSize: 3,
75+
childs: {
76+
name: {
77+
type: String,
78+
required: true,
79+
maxLength: 32
80+
},
81+
nick: {
82+
type: String,
83+
required: true,
84+
maxLength: 12
85+
}
86+
}
87+
}
5188
}
52-
}
89+
},
5390
};
5491

55-
try {
56-
new Scope(params, validation, /* { status: 400 } optional */);
57-
}
58-
catch(ex) {
59-
// error
92+
const errors = schema.init(params, scope);
93+
94+
} catch (err) {
95+
if (err instanceof InvalidParam) {
96+
console.log('Invalid request', err);
6097
}
6198
}
6299
```
63100

64-
## Installation
65-
66-
```bash
67-
$ npm install node-schema-validator
68-
```
69-
## How it works
70-
71-
* Validates each property of an object with specific settings.
72-
73-
## Parameters
74-
75-
You should put those properties on your validation settings:
101+
##Response exemplo
76102

77-
### isEqual: 'value'
78-
when you want to accept more than one value.
79-
e.g.:
80103
```js
81-
const validation = {
82-
name: {
83-
isEqual: 'someone'
104+
[
105+
{
106+
message: 'teams it\'s smaller than 4 <array>',
107+
type: 'minSize',
108+
attribute: 'teams'
84109
}
85-
}
110+
]
86111
```
87-
### isEqual: ['value1', 'value2']
88-
when you want to accept more than one value.
89-
e.g.:
90-
```js
91-
const validation = {
92-
name: {
93-
isEqual: ['name1', 'name2']
94-
}
95-
}
96-
```
97-
### isCpf: true
98-
When it should be a valid CPF Or Cnpj.
99-
e.g.:
100-
```js
101-
const validation = {
102-
cpf: {
103-
isCpf: true
104-
}
105-
}
106112

107-
const validation = {
108-
cnpj: {
109-
isCnpj: true
110-
}
111-
}
112-
```
113-
### isEmail: true
114-
When it should be a valid Email.
115-
e.g.:
116-
```js
117-
const validation = {
118-
email: {
119-
isEmail: true
120-
}
121-
}
122-
```
123-
### validation: [Array]
124-
When the property is an Array of objects that each item should be also validated.
125-
e.g.:
126-
```js
127-
const validation = {
128-
users: {
129-
type: Array
130-
items: {
131-
firstName: {
132-
required: true,
133-
type: String,
134-
},
135-
lastName: {
136-
type: String
137-
},
138-
age: {
139-
type: Number,
140-
minNumber: 18,
141-
maxNumber: 55
142-
}
143-
}
144-
}
145-
}
146-
```
147-
### validation: [Object]
148-
When the property is an Object that should also be validated.
149-
e.g.:
150-
```js
151-
const validation = {
152-
barbecue: {
153-
items: {
154-
peoples: {
155-
required: true,
156-
type: Array,
157-
items: {
158-
name: {
159-
required: true,
160-
type: String
161-
},
162-
age: {
163-
required: true,
164-
type: Number,
165-
minNumber: 18,
166-
maxNumber: 55
167-
}
168-
}
169-
},
170-
date: {
171-
type: Date,
172-
required: true
173-
}
174-
}
175-
}
176-
}
177-
```
113+
##Functions
114+
115+
#### Required
116+
#### minSize
117+
#### maxSize
118+
#### minLength
119+
#### maxLength
120+
#### minNumber
121+
#### maxNumber
122+
#### type
123+
#### pattern
124+
#### isEqual
125+
#### isCpf
126+
#### isCnpj
127+
#### isEmail
128+
#### Function
129+

0 commit comments

Comments
 (0)