Skip to content

Commit 435704e

Browse files
authored
Merge branch 'develop' into modifiers
2 parents 2920a26 + ec5df24 commit 435704e

File tree

11 files changed

+293
-14
lines changed

11 files changed

+293
-14
lines changed

README.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ Validators are just functions. It will be executed for each property, and return
5656
- [Custom function](#custom-function)
5757
- [Date](#date)
5858
- [Model](#model)
59+
- [Email](#email)
60+
- [Regex](#regex)
5961

6062
## Type
6163
If the data has not the specifed type, validation will fail, and that field will not be added to final validated data.
6264

65+
It uses typeof, so arrays will be considered valid objects. Also, there is a type "array", wich ensures data is an array.
66+
6367
```javascript
6468
{
65-
type: String // Check if data has a JS type (uses typeof)
69+
type: String // Check if data has a JS type
6670
}
6771
```
6872

@@ -118,7 +122,7 @@ Check if data is a JavaScript Date. Just need to set a boolean `date` parameter,
118122
119123
```javascript
120124
{
121-
data: Boolean // Check if data is a JS Date
125+
date: Boolean // Check if data is a JS Date
122126
}
123127
```
124128
> _Remember that JavaScript dates has type `Object`_
@@ -168,6 +172,48 @@ var batman = geopoint.modelate(myGeopoint);
168172
console.log(batman);
169173
```
170174
175+
## Email
176+
Check if data is an email. Might be just a boolean, or an object to check domains:
177+
178+
```javascript
179+
{
180+
email: Boolean // Check if data is an email
181+
}
182+
```
183+
184+
Also, to check domains:
185+
```javascript
186+
{
187+
email: {
188+
domain: 'gmail.com'
189+
}
190+
}
191+
```
192+
193+
Or, to enable more than one valid domains:
194+
```javascript
195+
{
196+
email: {
197+
domain: ['gmail.com', 'google.com']
198+
}
199+
}
200+
```
201+
202+
## Regex
203+
Test a regular expression over the data.
204+
```javascript
205+
{
206+
regex: Regex // Check if data is regex valid
207+
}
208+
```
209+
An example might be:
210+
```javascript
211+
{
212+
regex: /[a]/
213+
}
214+
```
215+
This will validate `asd` but not `sdf`
216+
171217
# Tests
172218
173219
I'm currently adding tests to this code. I'm using Jasmine, and saving tests in `/spec` folder.
@@ -195,10 +241,10 @@ Master version might not have the last test updates. Check out the `develop` bra
195241
- [ ] Validators
196242
- [x] Type
197243
- [x] Length
198-
- [ ] Value
199-
- [ ] Custom function
200-
- [ ] Date
201-
- [ ] Model
244+
- [x] Value
245+
- [x] Custom function
246+
- [x] Date
247+
- [ ] Model (Need help with this #15)
202248
203249
# Contribute
204250
You can use this code as you like. If you find a bug, or want to ask for a feature, just open an issue, and we'll do our best. If you can fix it, do a pull request to dev branch, and we promise to review it as fast as possible to merge it.

lib/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// Add validator names here
3-
const validators = ['type', 'length', 'value', 'func', 'model', 'date', 'value'];
3+
const validators = ['type', 'length', 'value', 'func', 'model', 'date', 'email', 'regex'];
44

55
// Turn validator names to validator instances.
66
for (let i = 0; i < validators.length; i++) {

lib/validators/email.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Email validator
3+
*
4+
* {
5+
* email: {
6+
* domain: String, Array
7+
* }
8+
* }
9+
*/
10+
11+
function isValid(data, model) {
12+
if (!model.email) {
13+
return true;
14+
}
15+
16+
if(typeof model.email === 'object' && typeof model.email.domain !== 'undefined') {
17+
if (isEmail(data)) {
18+
return validateDomain(data, model.email.domain);
19+
} else {
20+
return false;
21+
}
22+
}
23+
24+
return isEmail(data);
25+
}
26+
27+
function isEmail(data) {
28+
// regex from http://emailregex.com/
29+
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
30+
return emailRegex.test(data);
31+
}
32+
33+
function validateDomain(data, domain) {
34+
const dataDomain = data.split('@')[1];
35+
36+
if (typeof domain === 'string') {
37+
return dataDomain.toUpperCase() === domain.toUpperCase();
38+
} else if (Array.isArray(domain)) {
39+
for (var i = domain.length - 1; i >= 0; i--) {
40+
if(domain[i].toUpperCase() === dataDomain.toUpperCase()) {
41+
return true;
42+
}
43+
}
44+
return false;
45+
}
46+
47+
return false;
48+
}
49+
50+
module.exports = isValid;

lib/validators/regex.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Regex validator
3+
*
4+
* {
5+
* regex: Regex
6+
* }
7+
*/
8+
9+
function isValid(data, model) {
10+
if (!model.regex) {
11+
return true;
12+
}
13+
14+
if(typeof model.regex === 'object' && typeof model.regex.test === 'function') {
15+
return model.regex.test(data);
16+
}
17+
18+
return false;
19+
}
20+
21+
22+
module.exports = isValid;

lib/validators/type.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ function isValid(data, model) {
1111
return true;
1212
}
1313

14-
if ((typeof (data)).toUpperCase() === model.type.toUpperCase()) {
14+
if ((typeof data).toUpperCase() === model.type.toUpperCase()) {
1515
return true;
16+
} else if (model.type.toUpperCase() === 'ARRAY') {
17+
return Array.isArray(data);
1618
}
1719

1820
return false;

lib/validators/value.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function equals(data, expected) {
7070

7171
// Different types: Try to convert one to other.
7272
var parsed = data;
73-
var type = typeof (expected);
73+
var type = typeof expected;
7474
switch (type.toLowerCase()) {
7575
case 'string':
7676
parsed = String(data);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modelate",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "A simple data modeling tool for NodeJS",
55
"main": "index.js",
66
"scripts": {

spec/validators/email.spec.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
var valid = require('../../lib/validators/email');
2+
3+
const email = {
4+
example: '[email protected]',
5+
6+
google: '[email protected]',
7+
};
8+
const invalidEmail = {
9+
string: 'emailexamplecom',
10+
notAt: 'emailexample.com',
11+
notDot: 'email@example',
12+
notDomain: '[email protected]',
13+
notName: '@example.com'
14+
};
15+
16+
const modelBool = {
17+
email: true
18+
};
19+
const modelStr = {
20+
email: {
21+
domain: 'gmail.com'
22+
}
23+
};
24+
const modelArr = {
25+
email: {
26+
domain: ['gmail.com', 'google.com']
27+
}
28+
};
29+
30+
31+
describe(' - Email validator', () => {
32+
it('shall be a function', () => {
33+
expect(typeof valid).toEqual('function');
34+
});
35+
36+
it('shall validate everything when empty model set', () => {
37+
// Valid emails
38+
for (let elem in email) {
39+
expect(valid(email[elem], {})).toEqual(true);
40+
}
41+
// Invalid emails
42+
for (let elem in invalidEmail) {
43+
expect(valid(invalidEmail[elem], {})).toEqual(true);
44+
}
45+
});
46+
47+
describe('Just validate emails (boolean)', () => {
48+
describe('shall validate valid emails', () => {
49+
for (let elem in email) {
50+
it('as is ' + email[elem], () => {
51+
expect(valid(email[elem], modelBool)).toEqual(true);
52+
});
53+
}
54+
});
55+
56+
describe('shall not validate invalid emails', () => {
57+
for (let elem in invalidEmail) {
58+
it('as is ' + invalidEmail[elem], () => {
59+
expect(valid(invalidEmail[elem], modelBool)).toEqual(false);
60+
});
61+
}
62+
});
63+
});
64+
65+
describe('Validate email single domain (string)', () => {
66+
describe('shall validate valid emails with valid domain', () => {
67+
for (let elem in email) {
68+
if (elem === 'gmail') {
69+
it('as is ' + email[elem], () => {
70+
expect(valid(email[elem], modelStr)).toEqual(true);
71+
});
72+
} else {
73+
it('as is ' + email[elem], () => {
74+
expect(valid(email[elem], modelStr)).toEqual(false);
75+
});
76+
}
77+
}
78+
});
79+
80+
describe('shall not validate invalid emails', () => {
81+
for (let elem in invalidEmail) {
82+
it('as is ' + invalidEmail[elem], () => {
83+
expect(valid(invalidEmail[elem], modelStr)).toEqual(false);
84+
});
85+
}
86+
});
87+
});
88+
89+
describe('Validate email multiple domain (array)', () => {
90+
describe('shall validate valid emails with valid domains', () => {
91+
for (let elem in email) {
92+
if (elem === 'gmail' || elem === 'google') {
93+
it('as is ' + email[elem], () => {
94+
expect(valid(email[elem], modelArr)).toEqual(true);
95+
});
96+
} else {
97+
it('as is ' + email[elem], () => {
98+
expect(valid(email[elem], modelArr)).toEqual(false);
99+
});
100+
}
101+
}
102+
});
103+
104+
describe('shall not validate invalid emails', () => {
105+
for (let elem in invalidEmail) {
106+
it('as is ' + invalidEmail[elem], () => {
107+
expect(valid(invalidEmail[elem], modelArr)).toEqual(false);
108+
});
109+
}
110+
});
111+
});
112+
113+
});

spec/validators/model.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@
99
* Lot of things to think about.
1010
*/
1111

12-
const valid = require('../../lib/validators/model');
12+
// const valid = require('../../lib/validators/model');
13+
14+
const validate = {
15+
valid: require('../../lib/validators/model')
16+
};
17+
const valid = validate.valid;
1318

1419
describe(' - Model validator', () => {
1520
it('shall be a function', () => {
1621
expect(typeof valid).toEqual('function');
1722
});
1823

24+
it('shall return true if no model passed', () => {
25+
const resp = valid({name: ''}, {});
26+
expect(resp).toEqual(true);
27+
});
28+
1929
it('shall not validate a inexistent model', () => {
2030
const resp = valid({name: ''}, {model: 'MyTestModel'});
2131
expect(resp).toEqual(false);
2232
});
23-
2433
});

spec/validators/regex.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var valid = require('../../lib/validators/regex');
2+
3+
const regex = /[a]/;
4+
const validData = 'asd';
5+
const invalidData = 'sdf';
6+
7+
const model = {
8+
regex: regex
9+
};
10+
11+
describe(' - Regex validator', () => {
12+
it('shall be a function', () => {
13+
expect(typeof valid).toEqual('function');
14+
});
15+
16+
it('shall validate everything when no model set', () => {
17+
expect(valid(validData, {})).toEqual(true);
18+
expect(valid(invalidData, {})).toEqual(true);
19+
});
20+
21+
it('shall validate valid patterns when model set', () => {
22+
expect(valid(validData, model)).toEqual(true);
23+
});
24+
25+
it('shall not validate invalid patterns when model set', () => {
26+
expect(valid(invalidData, model)).toEqual(false);
27+
});
28+
});

0 commit comments

Comments
 (0)