Skip to content

Commit cbfac16

Browse files
committed
Document new validations ^^
1 parent 8bb97a4 commit cbfac16

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

README.md

Lines changed: 71 additions & 14 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
{
@@ -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+
```

0 commit comments

Comments
 (0)