{"id":13671891,"url":"https://github.com/nswbmw/koa-scheme","last_synced_at":"2025-04-27T18:31:51.748Z","repository":{"id":18760082,"uuid":"21972592","full_name":"nswbmw/koa-scheme","owner":"nswbmw","description":"koa-scheme is a parameter validation middleware for koa.","archived":false,"fork":false,"pushed_at":"2021-08-13T03:48:30.000Z","size":108,"stargazers_count":46,"open_issues_count":1,"forks_count":2,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-26T13:05:29.227Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nswbmw.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-18T08:00:07.000Z","updated_at":"2024-03-21T02:30:58.000Z","dependencies_parsed_at":"2022-07-20T10:04:13.823Z","dependency_job_id":null,"html_url":"https://github.com/nswbmw/koa-scheme","commit_stats":null,"previous_names":["mangrovetech/koa-scheme"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-scheme","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-scheme/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-scheme/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-scheme/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nswbmw","download_url":"https://codeload.github.com/nswbmw/koa-scheme/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250990541,"owners_count":21519119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:21.081Z","updated_at":"2025-04-27T18:31:50.606Z","avatar_url":"https://github.com/nswbmw.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"## koa-scheme\n\nkoa-scheme is a parameter validation middleware for koa.\n\nversion v3.0.0 support for koa2\n\n### Install\n\n    npm i koa-scheme --save\n    \n### Usage\n\n    scheme(config, options)\n\n- config: {Object|String} scheme object or path.\n- options: {Object}\n  - debug: {Boolean} If true, print compiled `config` and throw error, Default false.\n\n**app.js**\n\n```\nvar koa = require('koa');\n//var bodyParser = require('koa-bodyparser');\nvar scheme = require('koa-scheme');\nvar conf = require('./scheme');\nvar route = require('./route');\n\nvar app = koa();\n//app.use(bodyParser());\napp.use(scheme(conf));\n\nroute(app);\n\napp.listen(3000, function() {\n  console.log(\"listening on 3000\")\n});\n```\n\n**scheme.json**\n\n```\n{\n  \"/(.*)\": {\n    \"request\": {\n      \"header\": {\n        \"version\": \"[1-9]+\"        \n      }\n    }\n  },\n  \"/\": {\n    \"response\": {\n      \"status\": 200\n    }\n  },\n  \"GET /user/:username\": {\n    \"response\": {\n      \"body\": {\n        \"name\": /[a-z]+/i,\n        \"age\": \"[0-9]{1,3}\"\n      }\n    }\n  },\n  \"/user/:username\": {\n    \"request\": {\n      \"method\": \"(POST|patch)\",\n      \"body\": {\n        \"name\": \"[a-zA-Z]+\",\n        \"age\": /[0-9]{1,3}/\n      }\n    },\n    \"response\": {\n      \"status\": 200\n    }\n  },\n  \"(delete|OPTIONS) /user/:username\": {\n    ...\n  }\n}\n```\n\nsee [path-to-regexp](https://github.com/pillarjs/path-to-regexp).\n\n**NB**: when use body-parser middleware (like: koa-bodyparser) before koa-scheme, you could configure 'body' field in 'request'.\n\nuse function:\n\n**scheme.js**\n\n```\nmodule.exports = {\n  \"/users\": {\n    \"request\": {\n      \"method\": \"POST\",\n      \"body\": {\n        \"nameArr\": testRequestNameArr\n      }\n    }\n  }\n}\n\nfunction testRequestNameArr(arr) {\n  if (arr.length === 3 \u0026\u0026 arr[1] === 'nswbmw') {\n    return true;\n  } else {\n    return false;\n  }\n}\n```\n\nwith [validator](https://github.com/chriso/validator.js):\n\n**scheme.js**\n\n```\nvar validator = require('validator');\n\nmodule.exports = {\n  \"GET /user/:username\": {\n    \"response\": {\n      \"body\": {\n        \"age\": validator.isNumeric,\n        \"email\": validator.isEmail,\n        \"webset\": validator.isURL,\n        ...\n      }\n    }\n  }\n}\n```\n\nEven you could write flat object like this:\n\n```\n{\n  \"GET /user/:username\": {\n    \"response\": {\n      \"body.age\": validator.isNumeric,\n      \"body.email\": validator.isEmail,\n      \"body.family.mother.age\": validator.isNumeric\n    }\n  }\n}\n```\n\n### Example\n\n**scheme.js**\n\n```\nvar validator = require('validator');\n\nmodule.exports = {\n  \"/(.*)\": {\n    \"request\": {\n      \"header.version\": \"[1-9]+\"\n    }\n  },\n  \"/users\": {\n    \"request\": {\n      \"method\": \"GET\"\n    },\n    \"response\": {\n      \"body\": testRes\n    }\n  },\n  \"GET /user/:username\": {\n    \"response\": {\n      \"body\": {\n        \"name\": \"[a-zA-Z]+\",\n        \"age\": validator.isNumeric,\n        \"email\": validator.isEmail\n      }\n    }\n  },\n  \"/user/:username\": {\n    \"request\": {\n      \"method\": \"(POST|PATCH)\",\n      \"body.name\": /[a-zA-Z]+/,\n      \"body.age\": \"[0-9]{1,3}\",\n      \"body.email\": validator.isEmail\n    }\n  },\n  \"(delete|OPTIONS) /user/:username\": {\n    \"response\": {\n      \"status\": 200\n    }\n  }\n}\n\n// throw a error is ok\nfunction testRes(arr) {\n  throw new Error('badRequest');\n}\n```\n\n### Test\n\n   npm test\n\n### License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnswbmw%2Fkoa-scheme","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnswbmw%2Fkoa-scheme","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnswbmw%2Fkoa-scheme/lists"}