You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/validators.md
+99-26Lines changed: 99 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,86 +1,159 @@
1
1
# Validators
2
2
3
-
Every time you set a property for an instance of your model, an internal type validator checks that the value is valid. If not an error is thrown. But how to add a custom validator? You need to provide your custom validator in the schema definition. For example, if you want to check age to be a number greater than zero:
3
+
## Built-in type validators
4
+
5
+
Every time you save a field value for an instance of your model, an internal type validator checks whether the value is valid according to the defined data type of the field. A validation error is thrown if the given data type does not match the data type of the field.
6
+
7
+
For example let's have a field type defined as `int` like the following:
4
8
5
9
```js
6
10
7
11
exportdefault {
8
-
//... other properties hidden for clarity
12
+
//... other fields hidden for clarity
13
+
age: {
14
+
type :"int"
15
+
}
16
+
}
17
+
18
+
```
19
+
20
+
So now you cannot for example save a value of type decimal or string for age field. For non integer type input, a validation error will be returned in error callback or a validation error will be thrown if no callback is defined:
21
+
22
+
```js
23
+
24
+
var john =newmodels.instance.Person({
25
+
//... other fields hidden for clarity
26
+
age:32.5
27
+
});
28
+
john.save(function(err){
29
+
// invalid value error will be returned in callback
30
+
if(err) {
31
+
console.log(err);
32
+
return;
33
+
}
34
+
});
35
+
36
+
//... trying with string will also fail
37
+
john.age='32';
38
+
john.save(); // invalid value error will be thrown
39
+
40
+
```
41
+
42
+
## Disabling Built-in type validation
43
+
44
+
If you want to disable the built-in data type validation checks, you may overwrite the behaviour by setting a rule `type_validation: false` for the field:
45
+
46
+
```js
47
+
48
+
exportdefault {
49
+
//... other fields hidden for clarity
9
50
age: {
10
51
type :"int",
11
-
rule:function(value){ return value >0; }
52
+
rule : {
53
+
type_validation:false// Won't have validation error even if the value is not an integer
54
+
}
12
55
}
13
56
}
14
57
15
58
```
16
59
17
-
your validator must return a boolean. If someone will try to assign `john.age = -15;` an error will be thrown.
18
-
You can also provide a message for validation error in this way
60
+
So now you may use a string and by default it will be converted to integer by cassandra driver using it's automatic safe type conversion system for prepared queries:
61
+
62
+
```js
63
+
64
+
john.age='32';
65
+
john.save(); // will be successfully converted by driver to int
66
+
67
+
john.age='abc'
68
+
john.save(); // will throw db error for invalid data
69
+
70
+
```
71
+
72
+
## Required fields
73
+
74
+
If a field value is not set and no default value is provided, then the validators will not be executed. So if you want to have `required` fields, then you need to set the `required` flag to true like the following:
19
75
20
76
```js
21
77
22
78
exportdefault {
23
-
//... other properties hidden for clarity
79
+
//... other fields hidden for clarity
24
80
age: {
25
81
type :"int",
26
82
rule : {
27
-
validator:function(value){ return value >0; },
28
-
message :'Age must be greater than 0'
83
+
required:true// If age is undefined or null, then throw validation error
29
84
}
30
85
}
31
86
}
32
87
33
88
```
34
89
35
-
then the error will have your message. Message can also be a function; in that case it must return a string:
90
+
## Custom validators
91
+
92
+
You may also add a custom validator on top of existing type validators? You need to provide your custom validator in the schema definition rule. For example, if you want to check age to be a number greater than zero:
93
+
94
+
```js
95
+
96
+
exportdefault {
97
+
//... other fields hidden for clarity
98
+
age: {
99
+
type :"int",
100
+
rule:function(value){ return value >0; }
101
+
}
102
+
}
103
+
104
+
```
105
+
106
+
your validator must return a boolean. If someone will try to assign `john.age = -15;` an error will be thrown.
107
+
You can also provide a message for validation error:
36
108
37
109
```js
38
110
39
111
exportdefault {
40
-
//... other properties hidden for clarity
112
+
//... other fields hidden for clarity
41
113
age: {
42
114
type :"int",
43
115
rule : {
44
116
validator:function(value){ return value >0; },
45
-
message:function(value){ return'Age must be greater than 0. You provided '+ value; }
117
+
message :'Age must be greater than 0'
46
118
}
47
119
}
48
120
}
49
121
50
122
```
51
123
52
-
The error message will be `Age must be greater than 0. You provided -15`
53
-
54
-
Note that default values _are_ validated if defined either by value or as a javascript function. Defaults defined as DB functions, on the other hand, are never validated in the model as they are retrieved _after_ the corresponding data has entered the DB.
55
-
If you need to exclude defaults from being checked you can pass an extra flag:
124
+
then the error will have your message. Message can also be a function; in that case it must return a string:
56
125
57
126
```js
58
127
59
128
exportdefault {
60
-
//... other properties hidden for clarity
61
-
email: {
62
-
type :"text",
63
-
default :"<enter your email here>",
129
+
//... other fields hidden for clarity
130
+
age: {
131
+
type :"int",
64
132
rule : {
65
-
validator:function(value){ /* code to check that value matches an email pattern*/ },
66
-
ignore_default:true
133
+
validator:function(value){ returnvalue >0; },
134
+
message:function(value){ return'Age must be greater than 0. You provided '+ value; }
67
135
}
68
136
}
69
137
}
70
138
71
139
```
72
140
73
-
If a field value is not set and no default value is provided, then the validators will not be executed. So if you want to have `required` fields, then you need to set the `required` flag to true like the following:
141
+
The error message will be `Age must be greater than 0. You provided -15`
142
+
143
+
Note that default values are validated if defined either by value or as a javascript function. Defaults defined as DB functions, on the other hand, are never validated in the model as they are retrieved after the corresponding data has entered the DB.
144
+
145
+
If you need to exclude defaults from being checked you can pass an extra flag:
74
146
75
147
```js
76
148
77
149
exportdefault {
78
-
//... other properties hidden for clarity
150
+
//... other fields hidden for clarity
79
151
email: {
80
152
type :"text",
153
+
default :"no email provided",
81
154
rule : {
82
155
validator:function(value){ /* code to check that value matches an email pattern*/ },
83
-
required:true// If email is undefined or null, then throw validation error
156
+
ignore_default:true
84
157
}
85
158
}
86
159
}
@@ -89,9 +162,9 @@ export default {
89
162
90
163
You may also add multiple validators with a different validation message for each. Following is an example of using multiple validators:
0 commit comments