1.10.2 • Published 7 months ago

exframe-request-validator v1.10.2

Weekly downloads
196
License
ISC
Repository
bitbucket
Last release
7 months ago

exframe-request-validator

Wraps ajv to validate requests and produce human readable error messages when validation fails.

Example - validate requests

const validator = require('exframe-request-validator');

const sampleSchema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  title: 'Sample Validation Schema',
  type: 'object',
  required: [ 'thing1' ],
  properties: {
    thing1: { type: 'string' },
    thing2: { type: 'string', minLength: 1 }
  }
};

const validateSchema = (schema) => {
  const validate = validator.generateValidator(schema);

  return async (context, obj) => {
    validate({ log: context.log }, obj);
  };
};

return validateSchema(sampleSchema)(context, requestBody)
  .then(()) => {
    // Passed validation - do something awesome
  }
  .catch(err => {
    // One or more things failed validation.
    // err.status = 400
    // err.message = Human readable message
    // err.validationErrors = Array of validation failures
  });

custom error messages can also be used against specific properties / items use https://github.com/epoberezkin/ajv-errors for reference

Schema Dependencies

An optional second parameter can be passed to the generateValidator() function. It must be an array of schema dependencies which will be added via ajv.addSchema(). Each of these schemas must contain an $id. More information about addSchema() can be found here.

Example

  const validate = validator.generateValidator(schema, [referenceSchema1, referenceSchema2]);
1.10.2

7 months ago

1.9.0

8 months ago

1.8.0

8 months ago

1.10.1

8 months ago

1.10.0

8 months ago

1.7.0

10 months ago

1.6.0

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.1

2 years ago

1.4.0

2 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

6 years ago