-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose keywords option during the initialization(in init method). (#47)
Expose keywords option during the initialization(in init method). Resolve #45
- Loading branch information
Showing
12 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
swagger: "2.0" | ||
info: | ||
version: 1.0.0 | ||
title: Swagger with custom ajv keywords | ||
schemes: | ||
- http | ||
consumes: | ||
- application/json | ||
produces: | ||
- application/json | ||
paths: | ||
/keywords: | ||
post: | ||
description: test custom keywords | ||
produces: | ||
- application/json | ||
parameters: | ||
- name: post data | ||
in: body | ||
required: true | ||
description: body data | ||
schema: | ||
type: object | ||
properties: | ||
age: | ||
type: number | ||
range: [15, 30] | ||
prohibited: ['ages'] | ||
required: | ||
- age | ||
|
||
responses: | ||
'200': | ||
description: Import result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var inputValidation = require('../../src/middleware'); | ||
var range = require('ajv-keywords/keywords/range'); | ||
|
||
const definition = { | ||
type: 'object', | ||
macro: function (schema) { | ||
if (schema.length === 0) return true; | ||
if (schema.length === 1) return {not: {required: schema}}; | ||
var schemas = schema.map(function (prop) { | ||
return {required: [prop]}; | ||
}); | ||
return {not: {anyOf: schemas}}; | ||
}, | ||
metaSchema: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}; | ||
|
||
var inputValidationOptions = { | ||
keywords: [range, { name: 'prohibited', definition }], | ||
beautifyErrors: true, | ||
firstError: true, | ||
expectFormFieldsInBody: true | ||
}; | ||
|
||
module.exports = inputValidation.init('test/custom-keywords-swagger.yaml', inputValidationOptions) | ||
.then(function () { | ||
var app = express(); | ||
app.use(bodyParser.json()); | ||
app.post('/keywords', inputValidation.validate, function (req, res, next) { | ||
res.json({ result: 'OK' }); | ||
}); | ||
app.use(function (err, req, res, next) { | ||
if (err instanceof inputValidation.InputValidationError) { | ||
res.status(400).json({ more_info: err.errors }); | ||
} | ||
}); | ||
|
||
module.exports = app; | ||
|
||
return Promise.resolve(app); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const bodyParser = require('koa-bodyparser'); | ||
const inputValidation = require('../../src/middleware'); | ||
var range = require('ajv-keywords/keywords/range'); | ||
|
||
const definition = { | ||
type: 'object', | ||
macro: function (schema) { | ||
if (schema.length === 0) return true; | ||
if (schema.length === 1) return {not: {required: schema}}; | ||
var schemas = schema.map(function (prop) { | ||
return {required: [prop]}; | ||
}); | ||
return {not: {anyOf: schemas}}; | ||
}, | ||
metaSchema: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}; | ||
|
||
let app = new Koa(); | ||
let router = new Router(); | ||
|
||
app.use(async function(ctx, next) { | ||
try { | ||
await next(); | ||
} catch (err) { | ||
if (err instanceof inputValidation.InputValidationError) { | ||
ctx.status = 400; | ||
ctx.body = { more_info: JSON.stringify(err.errors) }; | ||
} | ||
} | ||
}); | ||
app.use(bodyParser()); | ||
app.use(router.routes()); | ||
|
||
var inputValidationOptions = { | ||
keywords: [range, { name: 'prohibited', definition }], | ||
beautifyErrors: true, | ||
firstError: true, | ||
expectFormFieldsInBody: true, | ||
framework: 'koa' | ||
}; | ||
|
||
module.exports = inputValidation.init('test/custom-keywords-swagger.yaml', inputValidationOptions) | ||
.then(function () { | ||
router.post('/keywords', inputValidation.validate, function (ctx, next) { | ||
ctx.status = 200; | ||
ctx.body = { result: 'OK' }; | ||
}); | ||
return Promise.resolve(app); | ||
}); |