这是我的验证器类
import { schema, rules } from "@ioc:Adonis/Core/Validator";
import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
export default class CreateUserValidator {
constructor(protected ctx: HttpContextContract) {}
schema = schema.create({
lat: schema.string({}, [
rules.regex(/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/),
]),
lng: schema.string({}, [
rules.regex(/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/),
]),
radius: schema.number(),
distance: schema.number(),
});
public messages = {};
}
这是我的控制器
import { validator } from '@ioc:Adonis/Core/Validator'
import CreateUser from 'App/Validators/CreateUserValidator'
public async station({
request,
response,
}: HttpContextContract) {
let inputs: any = await EncyptionManager.decrypt(request.body().inputs);
inputs = JSON.parse(inputs);
await validator.validate(
new CreateUser({
lat: ainputs.lat,
long: ainputs.lat,
radius: ainputs.radius,
distance: ainputs.distance,
})
);
console.log(inputs)
简单地说,我想使用 HTTP 请求之外的验证器类来验证 lat、long、radius 和 distance。