Skip to content

Commit 9667883

Browse files
authored
Merge pull request #2 from CodingCarlos/develop
New models
2 parents dc5437c + cbfac16 commit 9667883

File tree

12 files changed

+387
-71
lines changed

12 files changed

+387
-71
lines changed

README.md

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,22 @@ Validators are just functions. It will be executed for each property, and return
4242
- [Length](#length)
4343
- [Value](#value)
4444
- [Custom function](#custom-function)
45+
- [Date](#date)
46+
- [Model](#model)
4547

46-
## Type
47-
If the data has not the specifed type, validation will fail, and that field will not be added to final validated data.
48-
49-
```javascript
50-
{
51-
type: String // Check if data has a JS type (uses typeof)
52-
}
53-
```
48+
## Type
49+
If the data has not the specifed type, validation will fail, and that field will not be added to final validated data.
50+
51+
```javascript
52+
{
53+
type: String // Check if data has a JS type (uses typeof)
54+
}
55+
```
5456

55-
## Length
56-
Check data length. It uses default .length param, so it's valid for so much types.
57-
58-
```javascript
57+
## Length
58+
Check data length. It uses default .length param, so it's valid for so much types.
59+
60+
```javascript
5961
{
6062
length: {
6163
eq: Number, // Exact allowed value
@@ -65,8 +67,8 @@ Validators are just functions. It will be executed for each property, and return
6567
}
6668
```
6769

68-
## Value
69-
Check data length. It uses default .length param, so it's valid for so much types.
70+
## Value
71+
Check data length. It uses default .length param, so it's valid for so much types.
7072

7173
```javascript
7274
{
@@ -89,7 +91,7 @@ Use a custom function to check at your own criteria. The only necessary thing is
8991
```
9092

9193
The function might look like this:
92-
```
94+
```javascript
9395
function is42(value) {
9496
var allowed = [42, '42', 'cuarenta y dos', 'fourty two', 'the answer to the life the universe and everything'];
9597
if(allowedValues.indexOf(value) === -1) {
@@ -98,3 +100,58 @@ function is42(value) {
98100
return true;
99101
}
100102
```
103+
104+
## Date
105+
Check if data is a JavaScript Date. Just need to set a boolean `date` parameter, like:
106+
107+
```javascript
108+
{
109+
data: Boolean // Check if data is a JS Date
110+
}
111+
```
112+
> _Remember that JavaScript dates has type `Object`_
113+
114+
115+
## Model
116+
Yes, models can also validate (and modelate) other models. It's just neccessary the model to exists. To add that model validation, just set a property "model", with the string value of the model name:
117+
118+
```javascript
119+
{
120+
model: String // Check if data is of a defined model
121+
}
122+
```
123+
An example of when and how to use it, would be a geopoint:
124+
```javascript
125+
var coords = Modelate('Coords').set({
126+
lat: {
127+
type: 'number'
128+
},
129+
lon: {
130+
type: 'number'
131+
}
132+
});
133+
134+
var geopoint = Modelate('Geopoint').set({
135+
coords: {
136+
model: 'Coords'
137+
},
138+
name: {
139+
type: 'String',
140+
length: {
141+
max: 140
142+
}
143+
}
144+
});
145+
146+
// Now, you can validate Geopoint objects ^^
147+
var myGeopoint = {
148+
name: 'Batman Symbol'
149+
coords: {
150+
lat: 26.357896,
151+
long: 127.783809
152+
}
153+
}
154+
155+
var batman = geopoint.modelate(myGeopoint);
156+
console.log(batman);
157+
```

examples/chat.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Just a first basic test */
2+
var Modelate = require('../index');
3+
4+
// Models
5+
var userModel = {
6+
name: {
7+
type: 'string',
8+
length: { // For now, length do nothng.
9+
max: 10,
10+
min: 1
11+
},
12+
value: {
13+
eq: 'Paco'
14+
}
15+
},
16+
surname: {
17+
type: 'string',
18+
length: { // For now, length do nothng.
19+
max: 3,
20+
min: 1
21+
},
22+
value: {
23+
contains: 'os'
24+
}
25+
},
26+
age: {
27+
type: 'number',
28+
value: {
29+
max: 95,
30+
min: 18
31+
}
32+
},
33+
dni: {
34+
type: 'string',
35+
value: {
36+
max: 95,
37+
min: 18
38+
},
39+
func: function startOnFive(value) {
40+
if(value.charAt(0) === '5') {
41+
return true;
42+
}
43+
44+
return false;
45+
}
46+
}
47+
};
48+
var User = Modelate('User').set(userModel);
49+
50+
// Modelate a user
51+
var myUserData = {
52+
name: 'Paco',
53+
surname: 'santos',
54+
age: 19,
55+
dni: '51402430A',
56+
unexpected: 'property'
57+
};
58+
// var myUser = User.modelate(myUserData);
59+
// console.log(myUser);
60+
61+
var messageModel = {
62+
user: {
63+
type: 'object',
64+
model: 'User'
65+
},
66+
date: {
67+
date: true
68+
},
69+
message: {
70+
type: 'string',
71+
length: {
72+
min: 1,
73+
max: 255
74+
}
75+
}
76+
};
77+
var Message = Modelate('Message').set(messageModel);
78+
79+
// Modelate a message
80+
var myMessageData = {
81+
message: 'Using models inside models. Crazy.',
82+
date: new Date(),
83+
user: myUserData
84+
};
85+
var myMessage = Message.modelate(myMessageData);
86+
console.log(myMessage);

test.js renamed to examples/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Just a first basic test */
2-
var Modelate = require('./index');
2+
var Modelate = require('../index');
33

44
var model = {
55
name: {
@@ -44,13 +44,13 @@ var model = {
4444
}
4545
}
4646
};
47-
var user = Modelate('User').model(model);
47+
var user = Modelate('User').set(model);
4848

4949
var data = {
5050
name: 'Paco',
5151
surname: 'santos',
5252
age: 19,
53-
dni: '41402430A'
53+
dni: '51402430A'
5454
};
5555
var result = user.modelate(data);
5656

index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22
const model = require('./lib/model');
33
const modelate = require('./lib/modelate');
44

5-
const models = model.models;
6-
7-
5+
/**
6+
* Modelate instance.
7+
*/
88
function Modelate(name) {
99
const self = this;
1010

1111
self.modelName = name;
12-
self.model = model.add;
12+
self.model = model.get;
13+
self.set = model.add;
1314
self.modelate = modelate;
1415

15-
models[name] = {};
16-
1716
return this;
1817
}
1918

2019

2120
module.exports = Modelate;
22-
/*{
23-
add: function () { },
24-
remove: null,
25-
list: null,
26-
modelate: null
27-
};*/
28-

lib/model.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ const models = {};
33
function model(data) {
44
// ToDo: Validate model
55
// ToDo: Check if model already exists to merge instead of set
6-
models[this.name] = data;
6+
7+
models[this.modelName] = data;
78

89
return this;
910
}
1011

12+
function get() {
13+
return models[this.modelName];
14+
}
15+
16+
1117
module.exports = {
1218
models: models,
13-
add: model
19+
add: model,
20+
get: get
1421
};

lib/modelate.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
const util = require('./util');
22

33
const models = require('./model').models;
4-
const validate = require('./validate');
4+
const validate = require('./validate').check;
55

6-
function modelate(data) {
7-
const model = util.clone(models[this.name]);
6+
/**
7+
* Create a new modelate, validating all properties
8+
* Options might be:
9+
* - model: {Object} A model to use (instead of this.modelName)
10+
* @param data {Object} The data to validate
11+
* @param opts {Object} Options for the modelate.
12+
*/
13+
function modelate(data, opts) {
14+
if (!this.modelName && opts && !opts.model) {
15+
return {};
16+
}
17+
18+
const model = opts && opts.model || models[this.modelName];
19+
const result = util.clone(model);
820

9-
for(let prop in model) {
10-
if(validate(data[prop], model[prop])) {
11-
model[prop] = data[prop];
21+
for (let prop in model) {
22+
if (validate(data[prop], model[prop])) {
23+
result[prop] = data[prop];
1224
} else {
13-
delete model[prop];
25+
delete result[prop];
1426
}
1527
}
1628

17-
return model;
29+
return result;
1830
}
1931

2032
module.exports = modelate;

0 commit comments

Comments
 (0)