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

Added request validator example. #215

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
24 changes: 24 additions & 0 deletions content/guides/validator/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ Requests negotiating using `Accept=application/vnd.api+json` header, receives th
}
```

### Validating request body, query, and params
It can be useful to validate the body, query, and route params of a single request. For example, when updating a user profile, you may want to validate **both** the request body as well as ensure the `id` route param is a number.
```ts
Route.put('users/:id', async ({ request, response }) => {

const updateUserSchema = schema.create({
// Validate the request body and query
username: schema.string(),
email: schema.string([
rules.email()
]),
// Validate the request params
params: schema.object().members({
id: schema.number([
rules.unsigned()
])
})

})
```
:::note
The request body and query strings are merged during validation. Request params are not merged.
:::

## Standalone validator usage
You can also use the validator outside of an HTTP request by importing the `validate` method from the Validator module. The functional API remains the same. However, you will have to manually provide the `data` to validate.

Expand Down