Middleware and utilities for validating data against swagger schema's.
Requirements:
- Node >= 6.0.0
- fleek-context
This package is to be used as middleware for Koa2 to validate swagger documentation using ctx.fleek.context
defined by fleek-context or an equivalent custom middleware. Results of the validation are mapped to ctx.fleek.validation
, including passed
, failed
, and errors
.
npm install --save fleek-validator
For a swagger example, refer to the test swagger json
const Koa = require('koa');
const fleekCtx = require('fleek-context');
const fleekValidator = require('fleek-validator');
const SWAGGER = require('./swagger.json');
let app = new Koa();
app.use(fleekCtx(SWAGGER));
app.use(fleekValidator()); // Reject promise for validation failure
// OR
app.use(fleekValidator().catch((ctx, next) => {
console.log(ctx.fleek.validation); // =>{
// passed: [Boolean],
// failed: [Boolean],
// errors: [Array({ - github.com/fleekjs/fleek-validator/blob/lib/error/index.js
// name: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json
// code: [Integer] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
// message: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
// parameter : { [rejected param definition from swagger doc] }
// })]
// }
return next();
}));
app.listen(3000);
- Accepts
- Object - options
throw
: Boolean - if false, do not reject the middleware promise on validation failurecatch
: Function - must act as Koa2 middleware. this will be call if validation fails, withnext
referring to the next middleware in the chain. prioritized overthrow
- Object - options
- Returns
- Function - returns a promise when called
- Accepts
- Object - context of the request. must have
ctx.fleek.context
to perform validation - Function - returns promise when called
- Object - context of the request. must have
- Accepts
- Function - returns a promise when called
- Binds
ctx.fleek.validation
{
passed: [Boolean],
failed: [Boolean],
errors: [Array({ - github.com/fleekjs/fleek-validator/blob/lib/error/index.js
name: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json
code: [Integer] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
message: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
parameter : { [rejected param definition from swagger doc] }
})]
}
app.use(validator()); // reject middleware promise on failure
app.use(validator({ throw: false })); // continue down middleware on failure
app.use(validator({ catch: (ctx, next) => { return next(); } })); // continue down middleware on failure
app.use(validator().catch((ctx, next) => { return next(); })); // continue down middleware on failure
- Accepts
- Object - context of a request definted by Koa2, and containing
ctx.fleek.context
to define the validations - Object - swagger definition to validate, matches
ctx.fleek.context
set by fleek-context - Boolean - if true, the ctx will be cloned and returned. defaults to false, and updates in place
- Object - context of a request definted by Koa2, and containing
- Returns
- Object - updated context
ctx.request.body = { name: 'foo' };
validator.ctx(ctx);
validator.ctx(ctx, {
parameters: [{
name: "user",
in: "body",
schema: {
type: "object",
required: ["name"],
properties: {
name: { type: "string", uppercase: true }
}
}
}]
});
let newCtx = validator.ctx(ctx, null, true);
console.log(newCtx); // => {
// passed: false,
// failed: true,
// errors: [{
// name: 'VALUE.UPPERCASE'
// code: 207,
// message: 'Must be uppercase',
// parameter : { name: 'user.name' type: 'string', uppercase: true }
// }]
// }
// }
- Accepts
- Mixed - value to be validated
- Object - swagger object definition
- Returns
- Either
- Mixed - normalized result of the validations
- Error - error containing
.errors
list of validation errors
- Either
let result = validator.object({ name: 'FOO' }, { name: { type: 'string', uppercase: true } });
console.log(result); // => { name: 'FOO' }
let result = validator.object({ name: 'foo' }, { name: { type: 'string', toUpperCase: true } });
console.log(result); // => { name: 'FOO' }
let result = validator.object({ name: 'foo' }, { name: { type: 'string', uppercase: true } });
console.log(result); // => {
// message 'Validation failed',
// errors: [{
// name: 'VALUE.UPPERCASE'
// code: 207,
// message: 'Must be uppercase',
// parameter : { type: 'string', uppercase: true }
// }]
// }
- Accepts
- Mixed - value to be validated
- Object - swagger parameter definition
- Returns
- Either
- Mixed - normalized result of the validations
- ValidationError - validation error
- Either
let result = validator.one('FOO', { type: 'string', uppercase: true });
console.log(result); // => 'FOO'
let result = validator.one('foo', { type: 'string', toUpperCase: true });
console.log(result); // => 'FOO'
let result = validator.one('foo', { type: 'string', uppercase: true });
console.log(result); // => {
// name: 'VALUE.UPPERCASE'
// code: 207,
// message: 'Must be uppercase',
// parameter : { type: 'string', uppercase: true }
// }
- Expect: [Boolean]
- Type: [Mixed]
- Action: if the value is not defined, set it to the default
- Note: First action taken in order of operations
- Expect: [Boolean]
- Type: [String]
- Action: trim whitespace
- Expect: [Boolean]
- Type: [String]
- Action: convert to uppercase
- Expect: [Boolean]
- Type: [String]
- Action: convert to uppercase
- Expect: [Boolean]
- Type: [String]
- Action: reject undefined
- Note: Second action taken in order of operations, after default
- Expect: [String]
- Type: [Mixed]
- Action: reject the value if it does not match the expected type
- Expect: [Integer]
- Type: [Array]
- Action: rejects an array with more than the expected max
- Expect: [Integer]
- Type: [Array]
- Action: rejects an array with fewer than the expected min
- Expect: [Boolean]
- Type: [Array]
- Action: reject an array with duplicate items
- Expect: [Integer]
- Type: [Number]
- Action: reject a number thats not a multiple of the expected integer
- Expect: [Integer]
- Type: [Number]
- Action: reject a number thats above the expected max
- Expect: [Integer]
- Type: [Number]
- Action: reject a number thats above or equal to the expected max
- Expect: [Integer]
- Type: [Number]
- Action: reject a number thats below the expected min
- Expect: [Integer]
- Type: [Number]
- Action: reject a number thats below or equal to the expected min
- Expect: [Integer]
- Type: [Object]
- Action: reject an object with more than the expected max number of properties
- Expect: [Integer]
- Type: [Object]
- Action: reject an object with fewer than the expected min number of properties
- Expect: [Boolean]
- Type: [String]
- Action: reject a string that is not a valid email
- Expect: [Boolean]
- Type: [String]
- Action: reject a non-alphanumeric string
- Expect: [Boolean]
- Type: [String]
- Action: reject a string containing uppercase characters
- Expect: [Boolean]
- Type: [String]
- Action: reject a string containing lowercase characters
- Expect: [Integer]
- Type: [String]
- Action: reject a string of length below the expected length
- Expect: [Integer]
- Type: [String]
- Action: reject a string of length above the expected length
- Expect: [RegExp String]
- Type: [String]
- Action: reject a string that doesnt pass the rejex
- Expect: [Array]
- Type: [Mixed]
- Action: reject any value not listed in the enumeration
Built and maintained with by the Hart team.