Skip to content
Permalink
9088655e54
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
35 lines (28 sloc) 901 Bytes
const {Validator, ValidationError} = require('jsonschema');
const userSchema = require('../schemas/user.json').definitions.user;
const appSchema = require('../schemas/application.schema.js');
const makeKoaValidator = (schema, resource) => {
const v = new Validator();
const validationOptions = {
throwError: true,
propertyName: resource
};
const handler = async (ctx, next) => {
const body = ctx.request.body;
try {
v.validate(body, schema, validationOptions);
await next();
} catch (error) {
if (error instanceof ValidationError) {
console.error(error);
ctx.body = {message: error.stack};
ctx.status = 400;
} else {
throw error;
}
}
}
return handler;
}
exports.validateUser = makeKoaValidator(userSchema, 'user');
exports.validateApplication = makeKoaValidator(appSchema, 'application');