restify / node-restify

The future of Node.js REST development

Home Page:http://restify.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Input validation

fantik11 opened this issue · comments

Hello guys. I really don't understand why this framework hasn't any working validation plugin for input from user. I found express-validator, but it didn't work for me, just ignore any rules. I know that this plugin for express, but as I read from documentation, restify can accept express plugins.

Do I need to write a validation plugin by self by using validator.js for example? Or plugins like this already exist for restify? Or maybe I used express-validation in incorrect way?

I'm using express-validator. Works.
This is TypeScript:

import { checkSchema, Schema, validationResult } from 'express-validator';

const CHECK_SCHEMA: Schema = {
    auth_hash: { in: ['params'], isHash: { options: 'md5' } },
};

this.#server.get('/auth/:auth_hash',
            checkSchema(CHECK_SCHEMA),
            (req, res, next) => {
                const validation_errors = validationResult(req);
                if (!validation_errors.isEmpty()) {
                    return next(new errors.BadRequestError(JSON.stringify(validation_errors.array())));
                }

            });

I'm using express-validator. Works. This is TypeScript:

import { checkSchema, Schema, validationResult } from 'express-validator';

const CHECK_SCHEMA: Schema = {
    auth_hash: { in: ['params'], isHash: { options: 'md5' } },
};

this.#server.get('/auth/:auth_hash',
            checkSchema(CHECK_SCHEMA),
            (req, res, next) => {
                const validation_errors = validationResult(req);
                if (!validation_errors.isEmpty()) {
                    return next(new errors.BadRequestError(JSON.stringify(validation_errors.array())));
                }

            });

Thanks. I didn't call validationResult. Documentation of express-validator is really need to be more informative