Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `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).
- `schema` - validates all input together, should be used with more complex schemas like [Joi.when](https://hapi.dev/module/joi/api/?v=17.1.1#anywhencondition-options) and [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.
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between Compiled Joi schema object and Joi schema?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's the same we should keep the terms

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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: [],
Expand All @@ -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`
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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)
20 changes: 20 additions & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading