1.1.7 • Published 3 years ago

@expresso/validator v1.1.7

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

Expresso Validator

JSON Schema validation middleware for Expresso

Summary

What is this

This middleware validates an input against a JSON Schema and automatically throws a boom error for badData (which is HTTP code 422 - Unprocessable Entity) if this input is not matched.

Basic Usage

Install:

$ npm i @expresso/validator

Import and use:

import { validate } from '@expresso/validator'

// Your expresso initialization here

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  },
  additionalProperties: false,
  required: ['name', 'age']
}

app.post('/users', validate(schema), (req: Reques, res: Response, next: NextFunction) => { // ... // })

Validating queries

By default, the validator will match the schema against the body of the request, this is why the root type is { type: 'object' }. It is also possible to validate the query string params:

import { validate } from '@expresso/validator'

// Your expresso initialization here

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  },
  required: ['name', 'age']
}

app.get('/users', validate.query(schema), (req: Reques, res: Response, next: NextFunction) => { // ... // })

This will validate the whole query string against the given schema.

validateType function

validateType is actually a dummy function that should be removed in runtime. It exists to give type safety for using @expresso/ts-transformer-json-schema

Errors

This validator follows Expresso's directives and returns a Boom error for BadData, right now it is not possible to change this behavior

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago