0.1.1 • Published 8 years ago

@nielskrijger/ajv-validate v0.1.1

Weekly downloads
1
License
ISC
Repository
-
Last release
8 years ago

ajv-validate

A basic wrapper for AJV.

NOTE: this library is not very customizable nor will it be, its intent is to serve as a standard for my personal projects.

Install

$ npm install --save @nielskrijger/ajv-validate

Usage

This library manages two AJV validation instances, one for validating a request body and one for validatinga query parameters. Differences are listed below:

  • The body validator returns JSON pointers in error paths to indicate which property failed, e.g. /name. The query validator prefixes error paths with a question mark, e.g. ?name.
  • The query validator will attempt to convert properties to their expected types, the body validator doesn't.

addBodySchema(schema, schemaName)

Registers a JSON schema to validate the message body.

import { addBodySchema } from '@nielskrijger/ajv-validate';

addBodySchema({
  type: 'object',
  properties: {
    name: { type: 'string', minLength: 10 },
  },
}, 'testBody');

validateBody(schemaName, body)

Validates a message body against specified schema. A schema is referenced by its name.

import { validateBody } from '@nielskrijger/ajv-validate';

/**
 * [{ code: 'min_length',
 *    path: '/name',
 *    message: 'should NOT be shorter than 10 characters' }]
 */
validateBody('testBody', { name: 'test' });

addQuerySchema(schema, schemaName)

Registers a JSON schema to validate query parameters.

import { addQuerySchema } from '@nielskrijger/ajv-validate';

validator.addQuerySchema({
  type: 'object',
  properties: {
    limit: { type: 'integer', minimum: 0 },
  },
}, 'testBody');

validateQuery(schemaName, body)

Validates a message body against specified schema. A schema is referenced by its name.

import { validateQuery } from '@nielskrijger/ajv-validate';

/**
 * [{ code: 'minimum',
 *    path: '?limit',
 *    message: 'should be >= 0' }]
 */
validateQuery('testBody', { limit: -1 });

Additional formats

  • date-time: the standard date-time format is replaced by a moment(...).isValid() check which accepts any IS0-8601 string.

Error format

validateBody and validateQuery return an array of errors formatted as follows:

{
  code: 'minimum',
  path: '?limit',
  message: 'should be >= 0',
}
PropertyDescription
codeError code formatted in snake_case.
pathJSON pointer or query parameter name indicating which property validation failed.
messageHuman-readable error message.