Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
koa-route-schema
Advanced tools
koa middleware to apply jsonschema with route
Install with npm
npm install koa-route-schema --save
or install using yarn
yarn add koa-route-schema
trans schema list to schemaOptions
and new KoaRouteSchema
instance.
let routeschema = KoaRouteSchema({
prefix: "v1",
schemaOptions: [
{
route: "/docs/:id",
method: "GET",
schema: {
type: "object",
properties: {
type: {
type: "string",
title: "type",
minLength: 0,
maxLength: 5,
},
},
},
},
{
route: "/docs",
method: "POST",
schema: {
type: "object",
properties: {
content: {
type: "string",
title: "content",
minLength: 0,
maxLength: 5000,
},
type: {
type: "string",
title: "type",
mock: { mock: "@string" },
minLength: 1,
maxLength: 20,
enum: ["example", "hexo", "weibo"],
},
},
required: ["content", "type"],
},
},
],
});
// standalone mode
this.app.use(routeschema.middleware());
// attach mode
routeschema.attachToRouter(router /* router like koa-better-router */);
// schema.js
const RouteSchema = require('../../index')
const schema = new RouteSchema({
prefix: 'v1'
})
module.exports = schema
// router.js
const compose = require('koa-compose')
const router = Router({ prefix: '/api' }).loadMethods()
router.schema = schema
router.get('/', compose([
router.schema.routeQueryMiddleware({ type: 'object', properties: { type: { type: 'string', title: '类型', minLength: 1, maxLength: 20, enum: ['example', 'hexo', 'weibo'] } }, required: ['type'] }),
(ctx, next) => {
ctx.body = `Hello world! Prefix: ${ctx.query.type}`
return next()
}
]))
// can use generator middlewares
router.post('/foobar', compose([
router.schema.routeBodyMiddleware({ type: 'object', properties: { content: { type: 'string', title: '内容', minLength: 0, maxLength: 5000 }, type: { type: 'string', title: '类型', mock: { mock: '@string' }, minLength: 1, maxLength: 20, enum: ['example', 'hexo', 'weibo'] } }, required: ['content', 'type'] }),
function(ctx, next) {
ctx.body = `Foo Bar Baz! ${ctx.request.body.content}`
return next()
}
]))
var options = {
prefix: 'v1',
ajv: {}, // options passed to ajv constructor
schemaOptions: []
parseSchemaOptions: null, // [function]-parse real schemaOptions
getRoute: function(o) { // [function]-get route from each schemaOption item
return o.route
},
getMethod: function(o) { // [function]-get method from each schemaOption item
return o.method
},
getSchema: function(o) { // [function]-get schema from each schemaOption item
return o.schema
},
getBodySchema: null, // [function]-get bodySchema from each schemaOption item
getQuerySchema: null, // [function]-get querySchema from each schemaOption item
getData: null, // [function]-get data to validate from koa context
attachRoute: null, // [function]-attach middleware to router item, support koa-better-route and koa-rest-route by default
bodyErrorPrefix: 'body: ',
queryErrorPrefix: 'query: ',
onError: null // [function]-handle validate error
}
load schema
get middleware globally, built-in route check, can work without other route system
attach validate to appropriate route, accept one argument stand for router instance
use options.attachRoute
to define how to mix validation into supplied router system
get middleware used with route middleware, to validate supplied schema
get middleware used with route middleware, to validate body schema
get middleware used with route middleware, to validate query schema
FAQs
koa middleware to apply jsonschema with route
The npm package koa-route-schema receives a total of 5 weekly downloads. As such, koa-route-schema popularity was classified as not popular.
We found that koa-route-schema demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.