From 0503ab339a5f19068c5b737db14b3da097f2e5bc Mon Sep 17 00:00:00 2001 From: Zenkylo Date: Fri, 9 Jun 2023 12:10:04 -0400 Subject: [PATCH] Added request validator example. This example documents how to validate the request body, query, and params. --- content/guides/validator/introduction.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/content/guides/validator/introduction.md b/content/guides/validator/introduction.md index 823a3d7e..7f083472 100644 --- a/content/guides/validator/introduction.md +++ b/content/guides/validator/introduction.md @@ -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.