Skip to content

Commit 8b285a1

Browse files
authored
Merge pull request #16 from CodingCarlos/develop
Add lot of stuff for 0.2.3
2 parents 163d565 + 758be80 commit 8b285a1

File tree

18 files changed

+717
-35
lines changed

18 files changed

+717
-35
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.

examples/chat.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* Just a first basic test */
22
var Modelate = require('../index');
3+
// In production, just change the require for:
4+
// var Modelate = require('modelate');
35

46
// Models
57
var userModel = {
68
name: {
79
type: 'string',
8-
length: { // For now, length do nothng.
10+
length: {
911
max: 10,
1012
min: 1,
1113
},
@@ -15,7 +17,7 @@ var userModel = {
1517
},
1618
surname: {
1719
type: 'string',
18-
length: { // For now, length do nothng.
20+
length: {
1921
max: 3,
2022
min: 1,
2123
},

examples/user.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* Just a first basic test */
22
var Modelate = require('../index');
3+
// In production, just change the require for:
4+
// var Modelate = require('modelate');
35

46
var model = {
57
name: {
68
type: 'string',
7-
length: { // For now, length do nothng.
9+
length: {
810
max: 10,
911
min: 1,
1012
},
@@ -14,7 +16,7 @@ var model = {
1416
},
1517
surname: {
1618
type: 'string',
17-
length: { // For now, length do nothng.
19+
length: {
1820
max: 3,
1921
min: 1,
2022
},

lib/validators/date.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ function isValid(data, model) {
1414
if (data instanceof Date) {
1515
return true;
1616
}
17+
1718
// Try to parse data to Date;
1819
var date = Date.parse(data);
19-
if (!isNaN()) {
20+
if (!isNaN(date)) {
2021
data = new Date(date);
2122
return true;
2223
}
2324

24-
2525
return false;
2626
}
2727

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/func.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function isValid(data, model) {
1111
return true;
1212
}
1313

14+
if(typeof model.func !== 'function') {
15+
return false;
16+
}
17+
1418
return model.func(data);
1519
}
1620

lib/validators/model.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function isValid(data, model) {
1313
return true;
1414
}
1515

16+
if(!models[model.model]) {
17+
return false;
18+
}
19+
1620
var validate = module.parent.parent.exports;
1721
var valid = validate(data, { model: models[model.model] });
1822

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: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var util = require('../util');
2+
13
/**
24
* Value validator
35
*
@@ -7,6 +9,7 @@
79
* max: Number, // Maximum allowed value
810
* min: Number, // Minimum allowed value
911
* contains: String || Number // The value contains
12+
* list: Array // List of exact allowed values
1013
* }
1114
* }
1215
*/
@@ -18,28 +21,21 @@ function isValid(data, model) {
1821

1922
// Data is different than equals
2023
if (model.value.eq) {
21-
if (typeof (data) === typeof (model.value.eq) && data !== model.value.eq) {
22-
// Same data type and different values
24+
if(!equals(data, model.value.eq)) {
2325
return false;
2426
}
25-
// Different types: Try to convert one to other.
26-
var parsed = data;
27-
var type = typeof (model.value.eq);
28-
switch (type.toLowerCase()) {
29-
case 'string':
30-
parsed = String(data);
31-
break;
32-
case 'number':
33-
parsed = Number(data);
34-
break;
35-
case 'boolean':
36-
parsed = Boolean(data);
37-
break;
38-
}
27+
}
3928

40-
if (data !== model.value.eq) {
29+
if (model.value.list) {
30+
if(!Array.isArray(model.value.list)) {
4131
return false;
4232
}
33+
34+
for (var i = model.value.list.length - 1; i >= 0; i--) {
35+
if(!equals(data, model.value.list[i])) {
36+
return false;
37+
}
38+
}
4339
}
4440

4541
// Data is higher than max
@@ -53,7 +49,41 @@ function isValid(data, model) {
5349
}
5450

5551
// Data does not contain something
56-
if (model.value.contains && data.indexOf(model.value.contains) === -1) {
52+
if (model.value.contains) {
53+
var testData = data;
54+
if(typeof data === 'number') {
55+
testData = data.toString();
56+
}
57+
58+
return (testData.indexOf(model.value.contains) !== -1);
59+
}
60+
61+
return true;
62+
}
63+
64+
function equals(data, expected) {
65+
// Same data type and different values
66+
if (typeof (data) === typeof (expected) && data !== expected) {
67+
// If object, return util.compare. Else, return false.
68+
return (typeof data === 'object') ? util.equal(data, expected) : false;
69+
}
70+
71+
// Different types: Try to convert one to other.
72+
var parsed = data;
73+
var type = typeof expected;
74+
switch (type.toLowerCase()) {
75+
case 'string':
76+
parsed = String(data);
77+
break;
78+
case 'number':
79+
parsed = Number(data);
80+
break;
81+
case 'boolean':
82+
parsed = Boolean(data);
83+
break;
84+
}
85+
86+
if (data !== expected) {
5787
return false;
5888
}
5989

0 commit comments

Comments
 (0)