{"id":16884560,"url":"https://github.com/timwis/koa-superstruct","last_synced_at":"2025-09-02T20:46:12.315Z","repository":{"id":57141958,"uuid":"121033045","full_name":"timwis/koa-superstruct","owner":"timwis","description":"Use the superstruct data validation library as middleware for your koa app.","archived":false,"fork":false,"pushed_at":"2018-08-28T19:23:42.000Z","size":7,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T08:42:46.922Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/timwis.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":"2018-02-10T16:38:51.000Z","updated_at":"2022-08-18T18:37:49.000Z","dependencies_parsed_at":"2022-09-05T18:50:56.905Z","dependency_job_id":null,"html_url":"https://github.com/timwis/koa-superstruct","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timwis%2Fkoa-superstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timwis%2Fkoa-superstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timwis%2Fkoa-superstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timwis%2Fkoa-superstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timwis","download_url":"https://codeload.github.com/timwis/koa-superstruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248401478,"owners_count":21097328,"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-10-13T16:29:14.813Z","updated_at":"2025-04-11T12:24:21.073Z","avatar_url":"https://github.com/timwis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-superstruct\nUse the [superstruct][superstruct] data validation library as middleware for your [koa][koa] app.\n\n## usage\n```javascript\nconst { struct } = require('superstruct')\nconst validate = require('koa-superstruct')\n\nconst schema = struct({\n  body: {\n    id: 'number',\n    title: 'string',\n    isPublished: 'boolean?',\n    tags: ['string'],\n    author: {\n      id: 'number'\n    }\n  }\n})\n\nrouter.post('/entry', validate(schema), handler)\n```\n\nIf validation fails, it throws an HTTP 422 error (Unprocessable Entity) with descriptive message, ex:\n\n\u003e Expected a value of type `string` for `title` but received `undefined`.\n\n## intallation\n```bash\nnpm install koa-superstruct\n```\nInstall `superstruct` separately, allowing you to pass custom types and avoid peer dependency.\n\n## api\n\n### `validate`\n`validate(schema: Function) =\u003e Function`\n\nAccepts a [Struct validator function][validator-function]. The top-level keys should map to koa's [`ctx.request` object][request-object] (ex. `body`, `query`, `headers`) and, failing that, to the `ctx` object (ex. `ctx.params`).\n\n```javascript\nconst schema = struct({\n  headers: {\n    'X-Foo': 'string'\n  },\n  body: {\n    'count': 'number'\n  },\n  query: {\n    'page': 'number?'\n  },\n  params: {\n    'slug': 'string'\n  }\n})\nvalidate(schema)\n```\n\n[superstruct]: https://github.com/ianstormtaylor/superstruct\n[koa]: https://github.com/koajs/koa\n[validator-function]: https://github.com/ianstormtaylor/superstruct/blob/master/docs/reference.md\n[request-object]: http://koajs.com/#request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimwis%2Fkoa-superstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimwis%2Fkoa-superstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimwis%2Fkoa-superstruct/lists"}