{"id":25045141,"url":"https://github.com/petruki/validator4oak","last_synced_at":"2026-04-28T20:06:13.319Z","repository":{"id":226159405,"uuid":"767915768","full_name":"petruki/validator4oak","owner":"petruki","description":"Validator \u0026 Sanitizer middleware for Oak.","archived":false,"fork":false,"pushed_at":"2025-03-08T21:12:54.000Z","size":96,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T22:19:50.922Z","etag":null,"topics":["deno","oak","validator"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petruki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"patreon":"switcherapi","ko_fi":"petruki","github":["petruki"]}},"created_at":"2024-03-06T06:03:43.000Z","updated_at":"2025-03-08T21:12:57.000Z","dependencies_parsed_at":"2025-03-09T02:15:09.724Z","dependency_job_id":null,"html_url":"https://github.com/petruki/validator4oak","commit_stats":null,"previous_names":["petruki/validator4oak"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fvalidator4oak","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fvalidator4oak/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fvalidator4oak/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fvalidator4oak/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petruki","download_url":"https://codeload.github.com/petruki/validator4oak/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246399108,"owners_count":20770862,"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":["deno","oak","validator"],"created_at":"2025-02-06T05:32:39.453Z","updated_at":"2026-04-28T20:06:13.313Z","avatar_url":"https://github.com/petruki.png","language":"TypeScript","funding_links":["https://patreon.com/switcherapi","https://ko-fi.com/petruki","https://github.com/sponsors/petruki"],"categories":[],"sub_categories":[],"readme":"***\n\n\u003cdiv align=\"center\"\u003e\n\u003cb\u003eValidator4Oak\u003c/b\u003e\u003cbr\u003e\nValidator \u0026 Sanitizer middleware for Oak.\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![Master CI](https://github.com/petruki/validator4oak/actions/workflows/master.yml/badge.svg)](https://github.com/petruki/validator4oak/actions/workflows/master.yml)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=petruki_validator4oak\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=petruki_validator4oak)\n[![deno.land/x/validator4oak](https://shield.deno.dev/x/validator4oak)](https://deno.land/x/validator4oak)\n[![JSR](https://jsr.io/badges/@trackerforce/validator4oak)](https://jsr.io/@trackerforce/validator4oak)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n\u003c/div\u003e\n\n***\n\nThis module provides a middleware for [Oak](https://github.com/oakserver/oak) to validate and sanitize requests.\n\n**Compatibility**: Oak v14.0.0+\n\n- Validate request query, body, form and header parameters\n- Sanitize request parameters\n- Define custom error handlers\n- Create custom validators and sanitizers\n- Stack up multiple validators and sanitizers\n\n\nImport module with:\n    \n```typescript\nimport * as mod from \"@trackerforce/validator4oak@[VERSION]\";\nimport * as mod from \"https://deno.land/x/validator4oak@v[VERSION]/mod.ts\";\n```\n\n# Usage\n\n### Validate query parameters\n\n```typescript\nconst router = new Router();\nconst { query, check } = ValidatorMiddleware.createMiddleware\u003cContext, Next\u003e();\nconst { isUrl } = ValidatorFn.createValidator();\n\nrouter.get('/api/v1/shorten',\n  query(check('name').ifValue(isUrl())), \n  (ctx: Context) =\u003e {\n    // ...\n  },\n);\n```\n\nOptional query parameters:\n\n```typescript\nrouter.get('/api/v1/shorten',\n  query(check('url').isOptional()), \n  (ctx: Context) =\u003e {\n    // ...\n  },\n);\n```\n\n### Validate body and form parameters\n\nKey body parameters can be accessed using dot-path notation keys, e.g. `order.number`.\u003cbr\u003e\nSince Oak v14, body request can only be consumed once, so you'll need to use state.request_body to access it within the route.\n\nUsing body:\n\n```typescript\nrouter.post('/checkout/v1/confirm',\n  body(check('order.number').ifValue(isNumeric())),\n  (ctx: Context) =\u003e {\n    // ...\n    const body = ctx.state.request_body; // Access the body request\n    // ...\n  },\n);\n```\n\nUsing form:\n\n```typescript\nrouter.post('/checkout/v1/confirm',\n  form(check('order.number').ifValue(isNumeric())), \n  (ctx: Context) =\u003e {\n    // ...\n    const body = ctx.state.request_body; // Access the form request\n    // ...\n  },\n);\n```\n\nIt's also possible to validate array of complex objects using `isArray()` and `*` wildcard to access array elements.\n\n```typescript\nrouter.post('/checkout/v1/confirm',\n  body(\n    check('order.items').ifValue(isArray()),\n    check('order.items.*.sku').ifValue(isString(), hasLength({ min: 6 })),\n    check('order.items.*.quantity').ifValue(isNumeric())\n  ), (ctx: Context) =\u003e {\n    // ...\n  },\n);\n```\n\n### Validate header parameters\n\n```typescript\nconst { header } = ValidatorMiddleware.createMiddleware\u003cContext, Next\u003e();\nconst { isNumber } = ValidatorFn.createValidator();\n\nrouter.post('/example/v1/shorten',\n  header(check('x-api-key').ifValue(isNumber())), \n  (ctx: Context) =\u003e {\n    // ...\n  },\n);\n```\n\n### Sanitize parameters\n\n```typescript\nconst { escape, trim } = ValidatorSn.createSanitizer();\nconst { hasLength } = ValidatorFn.createValidator();\n\nrouter.post('/message/v1/send',\n  body(\n    check('message')\n      .sanitizeWith(escape(), trim())\n      .ifValue(hasLength({ max: 500 }))\n  ), (ctx: Context) =\u003e {\n    // ...\n  },\n);\n```\n\n### Testing\n\nUse `deno task test` to run tests.\n\n#### Check Oak compatibility\n\nReplace the Oak version in the `tests/deps.ts` file to verify if middleware is compatible.\n```typescript\nexport * from \"jsr:@oak/oak@[OAK_VERSION]\";\n```\n\n## Contributing\n\nPlease open an issue if you have some cool ideas to contribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetruki%2Fvalidator4oak","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetruki%2Fvalidator4oak","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetruki%2Fvalidator4oak/lists"}