Commit 9fb4af14 authored by xuezj's avatar xuezj

增加校验

parent 8b6d391f
......@@ -6,6 +6,7 @@ const log4js = require('koa-log4')
const logConf = require('../config/logger')
const error = require('../middleware/error')
const result = require('../middleware/result')
const validate = require('../middleware/validate')
const namespaceRoute = require('./namespace')
const serviceRoute = require('./service')
......@@ -28,6 +29,7 @@ function loadRoutes(router) {
router
.use(error())
.use(result())
.use(validate())
.use(async (ctx, next) => {
// 腾讯云
ctx.container = container
......
......@@ -73,6 +73,11 @@ router.post('/create', async (ctx) => {
})
router.post('/details', async (ctx) => {
ctx.validate(ctx.Joi.object().keys({
serviceName: ctx.Joi.string().required(),
namespace: ctx.Joi.string().required(),
}))
const data = await ctx.cluster.service_get(ctx.request.body.serviceName, ctx.request.body.namespace)
ctx.body = ctx.ok(data)
})
......
const Joi = require('joi')
function ValidationError(message) {
this.name = 'ValidationError'
this.message = message || ''
}
ValidationError.prototype = Object.create(Error.prototype);
ValidationError.prototype.constructor = ValidationError;
module.exports = function () {
return function (ctx, next) {
if (ctx.validate) {
return next()
}
ctx.Joi = Joi
ctx.validate = function (schema) {
const target = ctx.method === 'GET' ? ctx.query : (ctx.request.body || {})
const result = ctx.Joi.validate(target, schema)
if (result.error) {
throw new ValidationError(result.error.details[0] && result.error.details[0].message)
}
}
return next()
}
}
......@@ -2208,6 +2208,11 @@
}
}
},
"hoek": {
"version": "6.1.3",
"resolved": "http://npmprivate.quantgroups.com/hoek/-/hoek-6.1.3.tgz",
"integrity": "sha512-YXXAAhmF9zpQbC7LEcREFtXfGq5K1fmd+4PHkBq8NUqmzW3G+Dq10bI/i0KucLRwss3YYFQ0fSfoxBZYiGUqtQ=="
},
"hosted-git-info": {
"version": "2.7.1",
"resolved": "http://npmprivate.quantgroups.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz",
......@@ -2631,6 +2636,14 @@
"resolved": "http://registry.npm.taobao.org/isarray/download/isarray-0.0.1.tgz",
"integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
},
"isemail": {
"version": "3.2.0",
"resolved": "http://npmprivate.quantgroups.com/isemail/-/isemail-3.2.0.tgz",
"integrity": "sha512-zKqkK+O+dGqevc93KNsbZ/TqTUFd46MwWjYOoMrjIMZ51eU7DtQG3Wmd9SQQT7i7RVnuTPEiYEWHU3MSbxC1Tg==",
"requires": {
"punycode": "2.x.x"
}
},
"isexe": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/isexe/download/isexe-2.0.0.tgz",
......@@ -2657,6 +2670,16 @@
"is-object": "^1.0.1"
}
},
"joi": {
"version": "14.3.1",
"resolved": "http://npmprivate.quantgroups.com/joi/-/joi-14.3.1.tgz",
"integrity": "sha512-LQDdM+pkOrpAn4Lp+neNIFV3axv1Vna3j38bisbQhETPMANYRbFJFUyOZcOClYvM/hppMhGWuKSFEK9vjrB+bQ==",
"requires": {
"hoek": "6.x.x",
"isemail": "3.x.x",
"topo": "3.x.x"
}
},
"js-tokens": {
"version": "4.0.0",
"resolved": "http://npmprivate.quantgroups.com/js-tokens/-/js-tokens-4.0.0.tgz",
......@@ -3683,7 +3706,7 @@
},
"pseudomap": {
"version": "1.0.2",
"resolved": "http://npmprivate.quantgroups.com/pseudomap/-/pseudomap-1.0.2.tgz",
"resolved": "http://registry.npm.taobao.org/pseudomap/download/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM="
},
"psl": {
......@@ -4507,6 +4530,14 @@
"resolved": "http://registry.npm.taobao.org/toidentifier/download/toidentifier-1.0.0.tgz",
"integrity": "sha1-fhvjRw8ed5SLxD2Uo8j013UrpVM="
},
"topo": {
"version": "3.0.3",
"resolved": "http://npmprivate.quantgroups.com/topo/-/topo-3.0.3.tgz",
"integrity": "sha512-IgpPtvD4kjrJ7CRA3ov2FhWQADwv+Tdqbsf1ZnPUSAtCJ9e1Z44MmoSGDXGk4IppoZA7jd/QRkNddlLJWlUZsQ==",
"requires": {
"hoek": "6.x.x"
}
},
"touch": {
"version": "3.1.0",
"resolved": "http://registry.npm.taobao.org/touch/download/touch-3.1.0.tgz",
......
......@@ -4,6 +4,7 @@
"description": "",
"main": "bin/www.js",
"dependencies": {
"joi": "^14.3.1",
"js-yaml": "^3.12.1",
"koa": "^2.6.2",
"koa-bodyparser": "^4.2.1",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment