{"id":19551432,"url":"https://github.com/technicallyjosh/koa-better-validation","last_synced_at":"2025-04-19T16:53:08.028Z","repository":{"id":86788726,"uuid":"68545972","full_name":"technicallyjosh/koa-better-validation","owner":"technicallyjosh","description":"Koa validation middleware for body, params, and querystrings.","archived":false,"fork":false,"pushed_at":"2017-01-25T21:02:28.000Z","size":331,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T15:55:12.670Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/technicallyjosh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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}},"created_at":"2016-09-18T20:38:29.000Z","updated_at":"2019-05-30T14:46:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"53970226-8362-4220-8e5e-a76284ab2891","html_url":"https://github.com/technicallyjosh/koa-better-validation","commit_stats":{"total_commits":84,"total_committers":8,"mean_commits":10.5,"dds":"0.47619047619047616","last_synced_commit":"7e202c22383d491402a609c49dbbda8cea993fab"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicallyjosh%2Fkoa-better-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicallyjosh%2Fkoa-better-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicallyjosh%2Fkoa-better-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicallyjosh%2Fkoa-better-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/technicallyjosh","download_url":"https://codeload.github.com/technicallyjosh/koa-better-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249742771,"owners_count":21318991,"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-11-11T04:13:57.911Z","updated_at":"2025-04-19T16:53:08.022Z","avatar_url":"https://github.com/technicallyjosh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Koa Better Validation\n\n[![npm version](https://badge.fury.io/js/koa-better-validation.svg)](https://badge.fury.io/js/koa-better-validation) [![Build Status](https://travis-ci.org/technicallyjosh/koa-better-validation.svg?branch=master)](https://travis-ci.org/technicallyjosh/koa-better-validation)\n\nKoa Better Validation is a more up-to-date \"fork\" of [koa-validation](https://github.com/srinivasiyer/koa-validation).\nYou can validate request params, querystring values, bodies, and even files.\n\nYou can also extend this module to add custom validations, filters, and rules.\n\n## Install\n\n```\nnpm install koa-better-validation\n```\n\n## Field Validations\n\n```js\nconst app     = require('koa')();\nconst router  = require('koa-router')();\nconst koaBody = require('koa-better-body');\n\nrequire('koa-qs')(app, 'extended');\n\nconst validate = require('koa-better-validation');\n\napp.use(koaBody({\n    'multipart': true\n}));\n\napp.use(validate());\n\nrouter.post('/', function *(){\n    yield this.validateBody({\n        name     : 'required|minLength:4',\n        girlfiend: 'requiredIf:age,25',\n        wife     : 'requiredNotIf:age,22',\n        foo      : 'requiredWith:bar,baz',\n        foobar   : 'requiredWithAll:barbaz,bazbaz',\n        gandalf  : 'requiredWithout:Saruman',\n        tyrion   : 'requiredWithoutAll:tywin,cercei',\n        age      : 'numeric',\n        teenage  : 'digitsBetween:13,19',\n        date     : 'dateFormat:MMDDYYYY',\n        birthdate: 'date',\n        past     : 'before:2015-10-06',\n        future   : 'after:2015-10-07',\n        gender   : 'in:male, female',\n        genres   : 'notIn:Pop,Metal',\n        grade    : 'accepted',\n        nickname : 'alpha',\n        nospaces : 'alphaDash',\n        email    : 'email',\n        alphanum : 'alphaNumeric',\n        password : 'between:6,15'\n    }, {\n        'name.required': 'The name field is a required one'\n    }, {\n        before: {\n            name    : 'lowercase',\n            nickname: 'uppercase',\n            snum    : 'integer',\n            sword   : 'trim',\n            lword   : 'ltrim',\n            rword   : 'rtrim',\n            dnum    : 'float',\n            bword   : 'boolean',\n        },\n\n        after: {\n            obj    : 'json',\n            eword  : 'escape',\n            reword : 'replace:come,came',\n            shaword: 'sha1',\n            mdword : 'md5',\n            hexword: 'hex:sha256'\n        }\n    });\n\n    if (this.validationErrors) {\n        this.status = 422;\n        this.body = this.validationErrors;\n        return;\n    }\n\n    this.body = { success: true };\n});\n\n```\n\n## File Validations\n\n```js\nconst app     = require('koa')();\nconst router  = require('koa-router')();\nconst koaBody = require('koa-better-body');\n\nrequire('koa-qs')(app, 'extended');\n\nconst validate = require('koa-better-validation');\n\napp.use(koaBody({\n    'multipart': true\n}));\n\napp.use(validate());\n\nrouter.post('/files', function* () {\n    yield this.validateFiles({\n        'jsFile'  :'required|size:min,10kb,max,20kb',\n        'imgFile' : 'required|image',\n        'imgFile1': 'mime:jpg',\n        'imgFile2': 'extension:jpg',\n        'pkgFile' : 'name:package'\n    }, true, {}, {\n        jsFile: {\n            action: 'move',\n            args: `${__dirname}/../files/tmp/rules.js`,\n            callback: function* (validator, file, destination) {\n                validator.addError(file, 'action', 'move', 'Just checking if the callback action works!!')\n            }\n        },\n        imgFile: [\n            {\n                action: 'copy',\n                args: __dirname + '/../files/tmp/panda.jpg'\n            },\n            {\n                action: 'delete'\n            }\n        ]\n    });\n\n    if (this.validationErrors) {\n        this.status = 422;\n        this.body = this.validationErrors;\n        return;\n    }\n\n    this.body = { success: true };\n});\n\napp.use(router.routes()).use(router.allowedMethods());\n\n```\n\nCurrently it works like the original package `koa-validation`. Until we get our\nown in-depth documentation, you can\n[check out the original documentation](https://koa-validation.readme.io).\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnicallyjosh%2Fkoa-better-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechnicallyjosh%2Fkoa-better-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnicallyjosh%2Fkoa-better-validation/lists"}