gpt4 book ai didi

node.js - 如何在 adonis5 中创建路径和查询验证

转载 作者:行者123 更新时间:2023-12-05 03:41:25 50 4
gpt4 key购买 nike

在 adonis5 中,我目前使用基于模式的验证,但这些验证仅适用于请求主体。我如何验证路径和查询参数验证

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}}',
}
})
}

最佳答案

您可以在 schema.create({}) 函数下添加参数对象。

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

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

关于node.js - 如何在 adonis5 中创建路径和查询验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67805091/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com