2.0.0 • Published 3 years ago

express-dee-validator v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

Express Dee Validator

npm npm Coverage Status dependency Status devDependency Status Build Status Known Vulnerabilities node

Dee-validator port for Express framework.

Table of contents

Migration to v2

The v1 doesn't support async validators meaning the API is synchronous. For migration to v2, await getErrors and hasErrors methods.

Usage

The middleware creates validator which contains three dee-validators for req.body, req.query and req.params objects. You can use each validator separately.

The example of code:

const express = require('express');
const validator = require('express-dee-validator');

const app = express();
const customValidators = { // custom validators
    isTestString: {
        execute: value => value === 'test'
    }
}

app.use(validator(customValidators));

app.use(async (req, res, next) => {
    const validator = req.validator;
    const { bodyValidator, paramsValidator, queryValidator } = validator;

    console.log(validator.request); // you can get request object from the req.validator

    bodyValidator.property('name').isNotEmpty().isTestString();

    paramsValidator.property('id').isNotEmpty();

    queryValidator.property('test').optional().isUpperCaseString();

    if (await validator.hasErrors()) { // return true in case if no errors in body, params and query validators
      next({
        errors: await validator.getErrors() // here you can get errors from all of the validators
      });
    } else {
      next();
    }
})

You can find more details about creation of custom validators and a validator usage here.

Example of errors format:

{
    'name': {
        param: 'name',
        message: 'name should be a string',
        value: 0
    },
    'id': {
        param: 'id',
        message: 'id should be an integer',
        value: 'test'
    }
}

What's in a name?

Dee is one of my favorite detective characters - Judge Dee.

Author

Ilya Markevich - @ilya_mark91

2.0.0

3 years ago

1.1.1

7 years ago

1.0.0

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago