简体   繁体   中英

how to create a path and query validation in adonis5

In adonis5 i am currently using schema based validation, but these validation is only applicable to request body. how i can validate path and query parameter validation

import { rules, schema } from '@ioc:Adonis/Core/Validator'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

class UserValidator {
    async createUser(ctx: HttpContextContract) {
        const createUserSchema = schema.create({
            firstName: schema.string({
                escape: true,
                trim: true,
            }, [
                rules.minLength(3),
            ]),
            lastName: schema.string({
                escape: true,
                trim: true
            }, [
                rules.minLength(3)
            ]),
            email: schema.string({}, [
                rules.email({
                    sanitize: true,
                    ignoreMaxLength: true,
                    domainSpecificValidation: true,
                }),
                rules.unique({
                    table: 'users',
                    column: 'email'
                })
            ]),
            password: schema.string({}, [
                rules.minLength(8)
            ])
        })
        await ctx.request.validate({
            schema: createUserSchema,
            messages: {
                'required': '{{field}} is required to create an account.',
                'minLength': '{{field}} must be atleast {{options.minLength}} characters',
                'unique': '{{field}} should be {{rule}}',
            }
        })
    }

You can add params object under schema.create({}) function.

public schema = schema.create({
    params: schema.object().members({
      id: schema.string({escape: true, trim: true}, [
        rules.uuid({version: 4})
      ])
    })
});

Reference: https://docs.adonisjs.com/guides/validator/introduction#validating-http-requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM