1.2.2 • Published 1 year ago

simple-express-validation v1.2.2

Weekly downloads
12
License
ISC
Repository
-
Last release
1 year ago

simple-express-validation

Simple schemas validation for express only.

Opportunities

This package allows you to check:

  • req.body
  • req.params
  • req.query

In each of them you can check fields for:

  • existance
  • type (simple js data types + date)
  • value (by specifying array with allowed values)
  • is email

You can validate nested objects by specifying '_store' property in req.body. It contains nested object.

You can specify a list of allowed properties in req.body or req.query by setting '_allowedProps' array of keys. If '_allowedProps' contains array of strings then validation will return error in case any property would not found in '_allowedProps' array.

Usage

const express = require('express');
const { validate } = require('simple-express-validation');

const app = express();
app.use(express.json());

const bodySchema = {
  _allowedProps: ['email', 'fio', 'sex', 'birthdate', 'phone', 'address'],
  email: {
    required: true,
    type: 'string',
    isEmail: true
  },
  fio: {
    required: true,
    type: 'string'
  },
  sex: {
    required: true,
    type: 'string',
    equals: ['m', 'f']
  },
  birthdate: {
    required: true,
    type: 'date'
  },
  phone: {
    required: true,
    type: 'string',
    regexp: /^0[0-9]{9}$/
  },
  address: {
    required: false,
    _store: {
      _allowedProps: ['street', 'house'],
      street: {
        required: true,
        type: 'string'
      },
      house: {
        required: true,
        type: 'number'
      }
    }
  }
};

// You can specify these schemas the same as previous instead of '_store' property.
const paramsSchema = {};
const querySchema = {};

app.post('/person', validate(bodySchema, paramsSchema, querySchema), (req, res) => {
  console.log(req.body);
  res.sendStatus(200);
});

app.listen(3000);
1.2.2

1 year ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.10

2 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago