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
|location |`String`| Location of the field (`body`/`params`/`query`) only mandatory for higher order objects (direclty under `body`/`params`/`query`) default `body`|
73
-
|children |`Object[]`|Array of Child validator objects, only applicable if the field is `Array` or `Object`|
74
-
|isArray |`Boolean`|The value is `Array` or not (default `false`)|
75
-
|isObject |`Boolean`|The value is `Object` or not (default `false`)|
76
-
|isRequired |`Boolean`|The value is mandatory or not (default `false`)|
77
-
|isNumber |`Boolean`|The value is `Number` or not (default `false`)|
78
-
|range |`Object`|Object `{min : 1, max : 10}` describes minimum and maximum value of a Number field|
79
-
|isEmail |`Boolean`|The value is `Email` or not (default `false`)|
80
-
|isBoolean |`Boolean`|The value is `Boolean` or not (default `false`)|
81
-
|isDate |`Boolean`|The value is `Date` or not (default `false`)|
|mobileNumber |`Object`|Object `{countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}}` ,describes characteristics of mobile number, length is the length range of mobile number excluding country code |
84
-
|length |`Object`|Object `{min : 1, max : 10}` describes minimum and maximum length|
85
-
|includes |`Object[]`|Value must be one of the element in the array|
86
-
|excludes |`Object[]`|Value must not be one of the element in the array|
87
-
|message |`String`|Error message thrown in case of test fails default : Invalid Field Error|
88
-
89
-
#### Supported date formats
90
-
If `isDate` is true you can pass `format`, Only supported the following
75
+
## Getting Started
76
+
### Defining a Field
77
+
Use `param(<field Name>)` to define a field. It should end with `end()`
78
+
```js
79
+
param('userName').isRequired().end()
80
+
```
81
+
Defines a field `userName` which is mandatory.
82
+
#### Available Options
83
+
##### isRequired()
84
+
Field is mandatory
85
+
##### isArray()
86
+
Expects array
87
+
##### isObject()
88
+
Expects object
89
+
##### isNumber()
90
+
Expects number
91
+
##### isEmail()
92
+
Expects email
93
+
##### isBoolean()
94
+
Expects boolean value
95
+
##### isDate()
96
+
Expects a date with default format `YYYY-MM-DD`
97
+
##### dateFormat(format)
98
+
*`format`*Mandatory* String
99
+
specify date format, supported
91
100
```
92
101
YYYY-MM-DD
93
102
DD-MM-YYYY'
@@ -96,96 +105,115 @@ YYYY/MM/DD'
96
105
DD/MM/YYYY'
97
106
MM/DD/YYYY'
98
107
```
99
-
#### Nested Objets
100
-
In case of `Object` or `Array`, `isArray` or `isObject` must be true
101
-
if json structure is
108
+
##### minimumNumber(min)
109
+
*`min`*Mandatory* Number
110
+
Expects number and must be greater than or equal to `min`
111
+
##### maximumNumber(max)
112
+
*`max`*Mandatory* Number
113
+
Expects number and must be less than or equal to `max`
114
+
##### minimumLength(min)
115
+
*`min`*Mandatory* Number
116
+
Expects number/string and length must be less than or equal to `min`
117
+
##### maximumLength(max)
118
+
*`max`*Mandatory* Number
119
+
Expects number/string and length must be less than or equal to `max`
120
+
##### shouldInclude(inclues)
121
+
*`inclues`*Mandatory* Array
122
+
Expects number/string and must be one of given array `includes`
123
+
##### shouldExclude(excludes)
124
+
*`excludes`*Mandatory* Array
125
+
Expects number/string and must not be one of given array `excludes`
126
+
##### isMobileNumberWithCountryCode(countryCode)
127
+
*`countryCode`*Mandatory* String
128
+
Expects mobile number with or without `countryCode`
129
+
##### isMobileNumberWithCountryCodeMandatory()
130
+
Expects mobile number which should starts with country code set at `isMobileNumberWithCountryCode`
131
+
##### isMobileNumberWithMinimumLength(min)
132
+
*`min`*Mandatory* Number
133
+
Minimum length of mobile number without country code
134
+
##### isMobileNumberWithMaximumLength(max)
135
+
*`max`*Mandatory* Number
136
+
Maximum length of mobile number without country code
137
+
##### addChild(child)
138
+
*`child`*Mandatory* field definition object
139
+
Add a child object for arrays and objects
140
+
##### addChildren(children)
141
+
*`children`*Mandatory* Array of field definition objects
142
+
Add a list of children objects for arrays and objects
143
+
##### sendErrorMessage(message)
144
+
*`message`*Mandatory* String
145
+
Custom message to be send back in case of validation failure
102
146
```js
147
+
// Default message
103
148
{
104
-
"page": {
105
-
"sorted":"True"
106
-
},
107
-
"sort": [{
108
-
"value": [{
109
-
"date":"2019-01-01",
110
-
"length": {"min":"1", "max":"100"}
111
-
}]
112
-
}]
149
+
"error": [
150
+
{
151
+
"location":"body.sort",
152
+
"param":"sort",
153
+
"message":"Invalid Field Error"
154
+
}
155
+
]
113
156
}
114
-
```
115
-
the validator object
116
-
```js
117
-
[
118
-
{param :'page', location :'body', isObject :true, children : [
|mode |`String`| can be `reject` or `forward`, Mandatory field|
137
-
|errorCode |`String`| Error code send in response. default `422` Error|
138
-
|debug |`Boolean`| set `true` to respond back more details on error |
139
-
140
-
#### Mode
141
-
Value can be can be `reject` or `forward`.
142
-
##### Reject
143
-
Response is sent back with http status code provided in `errorCode` property
144
-
```js
157
+
// Custom message
145
158
{
146
159
"error": [
147
160
{
148
161
"location":"body.sort",
149
162
"param":"sort",
150
-
"message":"Invalid Field Error"
163
+
"message":"<Your Custom Error Message>"
151
164
}
152
165
]
153
166
}
154
167
```
155
-
##### Forward
156
-
Error is set to `request.locals.data` and error code to `request.locals.statusCode`. Forward the request to next middleware.
168
+
##### end() :bangbang::bangbang: Mandatory
169
+
Ends a param definition
157
170
158
-
#### debug
159
-
If `debug` is set to `true`, error response will be
171
+
### Creating a validation middleware
172
+
*`validateBody()`*Validate body*
173
+
*`validateParam()`*Validate param*
174
+
*`validateQuery()`*Validate query*
175
+
*`validateHeader()`*Validate header*
176
+
177
+
#### Available Options
178
+
##### isToBeRejected()
179
+
Defines the validation failure event - Server returns http status code set via `sendErrorCode` (default 422), :heavy_exclamation_mark: will not proceed to the next middleware
180
+
Response body
160
181
```js
161
182
{
162
183
"error": [
163
184
{
164
185
"location":"body.sort",
165
186
"param":"sort",
166
-
"message":"Invalid Field Error :: somevalueforsort Must Be A Boolean"
187
+
"message":"Invalid Field Error"
167
188
}
168
189
]
169
190
}
170
191
```
171
-
It will give more idea about the error.
172
-
173
-
## checkService
192
+
##### isToBeForwarded()
193
+
Defines the validation failure event - Error is set to `request.locals.data` and error code to `request.locals.statusCode`, :white_check_mark: will proceed to the next middleware
0 commit comments