-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature schema #5
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,9 @@ | |
# Why | ||
|
||
- It uses [Joi](https://www.npmjs.com/package/@hapi/joi) (_The most powerful schema description language and data validator for JavaScript._) | ||
- Input validation (`query`, `params`, `body`, `headers`). | ||
- Input validation | ||
- `query`, `params`, `body`, `headers` - each input part is validated separately. | ||
- `schema` - validates all input together, should be used with more complex schemas as [Joi.when](https://hapi.dev/module/joi/api/?v=17.1.1#anywhencondition-options) or [Joi.alternatives](https://hapi.dev/module/joi/api/?v=17.1.1#alternatives). | ||
- Output validation, based on the HTTP returned code from the router `200`, `204` ...etc. | ||
- Configurable. | ||
- It does only one thing (**validation**) and it does it right. | ||
|
@@ -40,12 +42,13 @@ The middleware function takes an object as argument | |
import validate, { Joi } from ('koa-router-joi-validation'); | ||
..... | ||
validate({ | ||
query: // Joi schema object | ||
body: // Joi schema object | ||
params: // Joi schema object | ||
headers: // Joi schema object | ||
200: // Joi schema object | ||
503: // Joi schema object | ||
query: // Joi schema definition | ||
body: // Joi schema definition | ||
params: // Joi schema definition | ||
headers: // Joi schema definition | ||
schema: // Compiled Joi schema object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's the same we should keep the terms There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have simple JS that is transformed to joi.object. Compiled is already Joi object and don't need to call Joi.object function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they call it compilation of JS object to Joi object on their site There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what I want to say there is that if you validate query, params and others separately with old version you need to pass JS object. But for schema need to pass Joi object |
||
200: // Joi schema definition | ||
503: // Joi schema definition | ||
..... | ||
config: { | ||
denyUnknown: [], | ||
|
@@ -61,13 +64,14 @@ import validate, { Joi } from ('koa-router-joi-validation'); | |
|
||
## The object contains the next keys: | ||
|
||
| Key | Type | Validates | Note | | ||
| -------- | :---------------: | ------------------ | ---------------------------------------------------------------------------------------- | | ||
| query | Joi Schema Object | `ctx.query` | | | ||
| params | Joi Schema Object | `ctx.params` | | | ||
| headers | Joi Schema Object | `ctx.headers` | | | ||
| body | Joi Schema Object | `ctx.request.body` | ⚠️ use a body parser e.g. [koa-bodyparser](https://www.npmjs.com/package/koa-bodyparser) | | ||
| 200..503 | Joi SchemaObject | `ctx.body` | when `ctx.status` === 200..503 | | ||
| Key | Type | Validates | Note | | ||
| -------- | :-------------------: | ------------------ | ---------------------------------------------------------------------------------------- | | ||
| query | Joi Schema definition | `ctx.query` | | | ||
| params | Joi Schema definition | `ctx.params` | | | ||
| headers | Joi Schema definition | `ctx.headers` | | | ||
| body | Joi Schema definition | `ctx.request.body` | ⚠️ use a body parser e.g. [koa-bodyparser](https://www.npmjs.com/package/koa-bodyparser) | | ||
| schema | Compiled Joi Schema Object | `ctx.query, ctx.params, ctx.headers, ctx.request.body` | | | ||
| 200..503 | Joi Schema definition | `ctx.body` | when `ctx.status` === 200..503 | | ||
| config | Object | | Use it to change the validator behavior: | | ||
|
||
## `config` | ||
|
@@ -118,6 +122,8 @@ import validate, { Joi } from ('koa-router-joi-validation'); | |
|
||
# Example | ||
|
||
Simple validation | ||
|
||
```javascript | ||
import Koa from "koa"; | ||
import Router from "@koa/router"; | ||
|
@@ -155,6 +161,46 @@ router.get( | |
app.use(router.routes()); | ||
``` | ||
|
||
Validation using <b>schema</b> | ||
|
||
```javascript | ||
import Koa from "koa"; | ||
import Router from "@koa/router"; | ||
import validate, { Joi } from ('koa-router-joi-validation'); | ||
|
||
const app = new Koa(); | ||
const router = new Router() | ||
|
||
router.get( | ||
"/hello/:id", | ||
validate({ | ||
schema: Joi.alternatives().try( | ||
Joi.object({ | ||
query: Joi.object({ | ||
q1: Joi.boolean().required() | ||
}) | ||
}).unknown(true), | ||
Joi.object({ | ||
query: Joi.object({ | ||
q2: Joi.boolean() | ||
}), | ||
body: Joi.object({ | ||
id: Joi.string().required() | ||
}).required() | ||
}).unknown(true) | ||
) | ||
}), | ||
async (ctx, next) => { | ||
ctx.body = { | ||
succuss: true | ||
}; | ||
await next(); | ||
} | ||
); | ||
|
||
app.use(router.routes()); | ||
``` | ||
|
||
# Licences | ||
|
||
### [MIT](https://github.com/fkanout/koa-router-joi-validation/blob/master/LICENSE) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.