@@ -13,6 +13,9 @@ It is written in Coffeescript and compiled to Javascript.
1313+ Authorization (optional bcrypt, anonymous)
1414+ CORS (Cross-Origin Resource Sharing)
1515+ HTTP/S (TLS)
16+ + Validation Engine
17+ + Body Parsers (JSON, URL, Multipart)
18+ + JSON Body Parser
1619+ Body Parser
1720+ Accept Parser
1821+ Date Parser
@@ -22,10 +25,13 @@ It is written in Coffeescript and compiled to Javascript.
2225+ Request Expiry
2326+ Throttle
2427+ Conditional Request
28+ + Audit Logger
29+ + Sanitize Path
30+ + Serve Static
2531+ Extensive Routing
2632+ Logging (morgan/custom)
27- + Plugins (extend your api)
28- + Addons (extend sys-api' core)
33+ + Plugins (extend your api instance )
34+ + Addons (extend sys-api's core)
2935
3036---
3137
@@ -37,6 +43,7 @@ It is written in Coffeescript and compiled to Javascript.
3743 * [ Anonymous access] ( #anonymous-access )
3844* [ CORS (Cross-Origin Resource Sharing)] ( #cors-cross-origin-resource-sharing )
3945* [ HTTP/S - TLS] ( #https---tls )
46+ * [ Validation] ( #validation )
4047* [ BodyParser] ( #bodyparser )
4148* [ Additional Restify plugins] ( #additional-restify-plugins )
4249* [ Plugins] ( #plugins )
@@ -186,11 +193,9 @@ api.auth({
186193``` coffeescript
187194api .cors ({
188195 enabled : true ,
189- settings : {
190- origins : [' https://foo.com' , ' http://bar.com' ], # defaults to ['*']
191- credentials : true , # defaults to false
192- headers : [' x-foo' ] # sets expose-headers
193- }
196+ origins : [' https://foo.com' , ' http://bar.com' ], # defaults to ['*']
197+ credentials : true , # defaults to false
198+ headers : [' x-foo' ] # sets expose-headers
194199})
195200```
196201
@@ -223,6 +228,105 @@ api.listen(80, 8443) #API is going to listen on port HTTP(80) and HTTPS(8443)
223228
224229---
225230
231+ ### Validation
232+
233+ The validator is based on `` restify-validation-engine `` and
234+ instead of including `` restify-validation-engine `` as a dependency, <br >
235+ I copied the code and updated it to support new features:
236+
237+ Simply enable the validator by using:
238+
239+ ```
240+ api.validator({
241+ enabled: true
242+ })
243+ ```
244+
245+ To use custom validators simply issue:
246+
247+ ```
248+ api.validator({
249+ enabled: true,
250+ customValidators : {
251+ ......
252+ }
253+ })
254+ ```
255+
256+ After enabling it, you may use the `` validate: `` property in your routes:
257+
258+ ```
259+ api.get({
260+ url: '/test',
261+ validate: {
262+ params: {
263+ name: {
264+ required: true
265+ }
266+ }
267+ }}, r => r.send('Passed validation))
268+ ```
269+
270+ You can find the validation documentation [ at this repository] ( https://github.com/paulvarache/restify-validation-engine )
271+
272+ These are my additional features that the validator-engine supports:
273+
274+ ```
275+ + The validate property now allows an array of scope objects so
276+ that you can fully utilize ES6 spread features:
277+
278+ const validators = {
279+ name: {
280+ required: true
281+ },
282+ other: {
283+ .....
284+ }
285+ }
286+
287+ api.get({
288+ url: '/test',
289+ validate: [
290+ { body: { ...validators }},
291+ { params: { ...... } }
292+ ]}, r => {
293+ r.next
294+ r.send
295+ r.req
296+ r.res
297+ })
298+
299+ + The req object is now forwarded to custom validators.
300+ This allows you to use custom validation functions as middleware.
301+
302+ const myValidator = (name, req) => {
303+ // do something with req
304+ // ....
305+ // check for valid name
306+ return (name == 'cool')
307+ }
308+
309+ api.validator({
310+ enabled: true,
311+ customValidators: {
312+ myValidator
313+ }
314+ })
315+
316+ api.get({
317+ url: '/test',
318+ validate: {
319+ params: {
320+ name: {
321+ required: true,
322+ myValidator: 'Invalid name'
323+ }
324+ }
325+ }}, r => r.send('Passed validation))
326+ ```
327+
328+ ---
329+
226330### BodyParser
227331
228332The BodyParser can be enabled with a single option.
@@ -237,31 +341,29 @@ api.bodyParser({
237341})
238342```
239343
240- If you want to change more settings, simply pass an object :
344+ If you want to change more settings:
241345
242346``` coffeescript
243347api .bodyParser ({
244348 enabled : true ,
245- settings : {
246- maxBodySize : 0 ,
247- mapParams : true ,
248- mapFiles : false ,
249- overrideParams : false ,
250- multipartHandler : function (part) {
251- part .on (' data' , function (data) {
252- # do something with the multipart data
253- });
254- },
255- multipartFileHandler : function (part) {
256- part .on (' data' , function (data) {
257- # do something with the multipart file data
258- });
259- },
260- keepExtensions : false ,
261- uploadDir : os .tmpdir (),
262- multiples : true
263- hash : ' sha1'
264- }
349+ maxBodySize : 0 ,
350+ mapParams : true ,
351+ mapFiles : false ,
352+ overrideParams : false ,
353+ multipartHandler : function (part) {
354+ part .on (' data' , function (data) {
355+ # do something with the multipart data
356+ });
357+ },
358+ multipartFileHandler : function (part) {
359+ part .on (' data' , function (data) {
360+ # do something with the multipart file data
361+ });
362+ },
363+ keepExtensions : false ,
364+ uploadDir : os .tmpdir (),
365+ multiples : true
366+ hash : ' sha1'
265367})
266368```
267369
0 commit comments