Skip to content

Commit 163d565

Browse files
authored
Merge pull request #12 from CodingCarlos/develop
Core tested and some validators. Also Travis to Master.
2 parents 0c22523 + 96c5b53 commit 163d565

File tree

8 files changed

+277
-6
lines changed

8 files changed

+277
-6
lines changed

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files": [
3+
"lib/",
4+
"index.js"
5+
]
6+
}

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "node"

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
A data modeling tool for NodeJS. It's 100% database agnostic, and 100% customizable.
99

10+
# How to install
11+
Use NPM to install the package:
12+
```
13+
npm install modelate --save
14+
```
15+
1016
# How does it work?
1117
Create a model, and turn your objects into that model.
1218

1319
```javascript
14-
var Modelate = require('./index');
20+
var Modelate = require('modelate');
1521

1622
var model = {
1723
name: {
@@ -181,14 +187,14 @@ And now you can run tests ^^
181187
## Test coverage:
182188
Master version might not have the last test updates. Check out the `develop` branch to see the last updates. Also, the actual test coverage (in `develop` branch) is fully referenced in #3 issue.
183189
184-
- [ ] Core
190+
- [x] Core
185191
- [x] Exported module
186192
- [x] Model
187193
- [x] Modelate
188-
- [ ] Validate
194+
- [x] Validate
189195
- [ ] Validators
190-
- [ ] Type
191-
- [ ] Length
196+
- [x] Type
197+
- [x] Length
192198
- [ ] Value
193199
- [ ] Custom function
194200
- [ ] Date

lib/validators/length.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function isValid(data, model) {
1515
return true;
1616
}
1717

18+
// ToDo: Number length (toString?)
19+
// ToDo: Object length (keys?)
20+
1821
// Data hasn't the exact eq lenght
1922
if (model.length.eq && data.length !== model.length.eq) {
2023
return false;

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.1",
3+
"version": "0.2.2",
44
"description": "A simple data modeling tool for NodeJS",
55
"main": "index.js",
66
"scripts": {

spec/validate.spec.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// const util = require('../lib/util');
2+
const validate = require('../lib/validate');
3+
4+
// Test data
5+
const validData = '';
6+
const invalidData = '';
7+
const validatorDefault = {
8+
name: 'default',
9+
check: function (data, model) {
10+
return (data === validData);
11+
}
12+
};
13+
14+
const validators = {
15+
clear: function () {
16+
validate.validators = [];
17+
},
18+
add: function (validator) {
19+
if (validator) {
20+
validate.validators.push(validator);
21+
} else {
22+
validate.validators.push(validatorDefault);
23+
}
24+
},
25+
remove: function (validator) {
26+
if(typeof validator === 'string') {
27+
remValidator(validator);
28+
} else if (validator && typeof validator.name === 'string') {
29+
remValidator(validator.name);
30+
}
31+
}
32+
};
33+
34+
function remValidator(name) {
35+
for (var i = validate.validators.length - 1; i >= 0; i--) {
36+
if(validate.validators[i].name === name) {
37+
validate.validators.splice(i, 1);
38+
}
39+
}
40+
}
41+
42+
// Start testing
43+
describe(' - Validate', () => {
44+
it('shall be an object', () => {
45+
expect(typeof validate).toEqual('object');
46+
});
47+
48+
it('shall have a "validators" property', () => {
49+
expect(validate.validators).toBeDefined();
50+
});
51+
52+
it('shall have a "check" property', () => {
53+
expect(validate.check).toBeDefined();
54+
});
55+
56+
57+
describe('.validators', () => {
58+
it('shall be an array', () => {
59+
expect(typeof validate.validators).toEqual('object');
60+
expect(Array.isArray(validate.validators)).toEqual(true);
61+
});
62+
63+
it('shall have some validations by default', () => {
64+
expect(validate.validators.length > 0).toEqual(true);
65+
});
66+
});
67+
68+
describe('.check', () => {
69+
it('shall be a function', () => {
70+
expect(typeof validate.check).toEqual('function');
71+
});
72+
73+
it('shall validate valid data', () => {
74+
validators.clear();
75+
validators.add();
76+
expect(validate.check(validData, {})).toEqual(true);
77+
validators.clear();
78+
});
79+
80+
it('shall not validate invalid data', () => {
81+
validators.clear();
82+
validators.add();
83+
expect(validate.check(invalidData, {})).toEqual(true);
84+
validators.clear();
85+
});
86+
});
87+
});

spec/validators/length.spec.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var valid = require('../../lib/validators/length');
2+
3+
const str = 'Hello world';
4+
const strLen = str.length;
5+
const arr = ['Hello world', 'other thing'];
6+
const arrLen = arr.length;
7+
8+
const model = {
9+
length: {}
10+
};
11+
12+
13+
describe(' - Length validator', () => {
14+
it('shall be a function', () => {
15+
expect(typeof valid).toEqual('function');
16+
});
17+
18+
it('shall validate everything when empty model set', () => {
19+
expect(valid(str, model)).toEqual(true);
20+
expect(valid(arr, model)).toEqual(true);
21+
});
22+
23+
describe('equal filter', () => {
24+
describe('strings', () => {
25+
it('shall validate string with string.length', () => {
26+
const eqModel = { length: { eq: strLen } };
27+
expect(valid(str, eqModel)).toEqual(true);
28+
});
29+
30+
it('shall NOT validate string with string.length - 1', () => {
31+
const eqModel = { length: { eq: (strLen - 1) } };
32+
expect(valid(str, eqModel)).toEqual(false);
33+
});
34+
35+
it('shall NOT validate string with string.length + 1', () => {
36+
const eqModel = { length: { eq: (strLen + 1) } };
37+
expect(valid(str, eqModel)).toEqual(false);
38+
});
39+
});
40+
41+
describe('arrays', () => {
42+
it('shall validate array with array.length', () => {
43+
const eqModel = { length: { eq: arrLen } };
44+
expect(valid(arr, eqModel)).toEqual(true);
45+
});
46+
47+
it('shall NOT validate array with array.length - 1', () => {
48+
const eqModel = { length: { eq: (arrLen - 1) } };
49+
expect(valid(arr, eqModel)).toEqual(false);
50+
});
51+
52+
it('shall NOT validate array with array.length + 1', () => {
53+
const eqModel = { length: { eq: (arrLen + 1) } };
54+
expect(valid(arr, eqModel)).toEqual(false);
55+
});
56+
});
57+
});
58+
59+
describe('max filter', () => {
60+
describe('strings', () => {
61+
it('shall validate string with max string.length', () => {
62+
const eqModel = { length: { max: strLen } };
63+
expect(valid(str, eqModel)).toEqual(true);
64+
});
65+
66+
it('shall NOT validate string with max string.length - 1', () => {
67+
const eqModel = { length: { max: (strLen - 1) } };
68+
expect(valid(str, eqModel)).toEqual(false);
69+
});
70+
71+
it('shall validate string with max string.length + 1', () => {
72+
const eqModel = { length: { max: (strLen + 1) } };
73+
expect(valid(str, eqModel)).toEqual(true);
74+
});
75+
});
76+
77+
describe('arrays', () => {
78+
it('shall validate array with max array.length', () => {
79+
const eqModel = { length: { max: arrLen } };
80+
expect(valid(arr, eqModel)).toEqual(true);
81+
});
82+
83+
it('shall NOT validate array with max array.length - 1', () => {
84+
const eqModel = { length: { max: (arrLen - 1) } };
85+
expect(valid(arr, eqModel)).toEqual(false);
86+
});
87+
88+
it('shall validate array with max array.length + 1', () => {
89+
const eqModel = { length: { max: (arrLen + 1) } };
90+
expect(valid(arr, eqModel)).toEqual(true);
91+
});
92+
});
93+
});
94+
95+
describe('min filter', () => {
96+
describe('strings', () => {
97+
it('shall validate string with min string.length', () => {
98+
const eqModel = { length: { min: strLen } };
99+
expect(valid(str, eqModel)).toEqual(true);
100+
});
101+
102+
it('shall validate string with min string.length - 1', () => {
103+
const eqModel = { length: { min: (strLen - 1) } };
104+
expect(valid(str, eqModel)).toEqual(true);
105+
});
106+
107+
it('shall NOT validate string with min string.length + 1', () => {
108+
const eqModel = { length: { min: (strLen + 1) } };
109+
expect(valid(str, eqModel)).toEqual(false);
110+
});
111+
});
112+
113+
describe('arrays', () => {
114+
it('shall validate array with min array.length', () => {
115+
const eqModel = { length: { min: arrLen } };
116+
expect(valid(arr, eqModel)).toEqual(true);
117+
});
118+
119+
it('shall validate array with min array.length - 1', () => {
120+
const eqModel = { length: { min: (arrLen - 1) } };
121+
expect(valid(arr, eqModel)).toEqual(true);
122+
});
123+
124+
it('shall NOT validate array with min array.length + 1', () => {
125+
const eqModel = { length: { min: (arrLen + 1) } };
126+
expect(valid(arr, eqModel)).toEqual(false);
127+
});
128+
});
129+
});
130+
131+
});

spec/validators/type.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var valid = require('../../lib/validators/type');
2+
3+
const str = 'Hello world';
4+
const obj = {hello: 'world'};
5+
const num = 42;
6+
7+
const types = {
8+
string: str,
9+
object: obj,
10+
number: num
11+
};
12+
13+
const typesKeys = Object.keys(types);
14+
15+
describe(' - Type validator', () => {
16+
it('shall be a function', () => {
17+
expect(typeof valid).toEqual('function');
18+
});
19+
20+
for (let key in typesKeys) {
21+
it('shall validate '+ typesKeys[key] +' when no model set', () => {
22+
expect(valid(types[key], {})).toEqual(true);
23+
});
24+
}
25+
26+
for (let validType in typesKeys) {
27+
let model = {type: typesKeys[validType]};
28+
for (let check in typesKeys) {
29+
it('shall only validate '+ typesKeys[validType] +' when model set, and '+ typesKeys[check] +' given', () => {
30+
expect(valid(types[typesKeys[check]], model)).toEqual( (validType === check) );
31+
});
32+
}
33+
}
34+
35+
});

0 commit comments

Comments
 (0)