You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AdonisJS生产环境仅出现e_validation_failure验证异常求助

Adonis 5在AWS EC2生产环境仅触发验证异常问题

本地与预发布环境运行正常,但部署到AWS EC2生产环境后,用户注册接口始终抛出「e_validation_failure: validation exception」验证异常,请求数据格式符合验证规则,已通过node ace make:validator SignupUserValidator命令创建验证器。

控制器代码

public async store({ request, response }: HttpContextContract) {
    response.header('Cache-Control', 'no-cache, no-store');
    try {
      const payload = await request.validate(SignupUserValidator); 
      await UserService.store(payload);

      return responseWithSuccess(response);
    } catch (error) {
      return responseWithError(response, error.message);
    }
  }

验证器代码

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

export default class SignupUserValidator {
  constructor(protected ctx: HttpContextContract) {}

  public schema = schema.create({
    first_name: schema.string({ trim: true }, [rules.minLength(2)]),
    last_name: schema.string({ trim: true }, [rules.minLength(2)]),
    password: schema.string({}, [rules.minLength(8), rules.confirmed()]),
    email: schema.string({}, [
      rules.email(),
      rules.unique({ table: 'users', column: 'email' }),
    ]),
    role: schema.enum([UserRoleEnum.CLIENT, UserRoleEnum.COWORKING] as const),
    photo_id: schema.number.optional([rules.exists({ table: 'photos', column: 'id' })]),
    personal_address: schema.object.optional().members({
      fulltext: schema.string.optional({ trim: true }),
      latitude: schema.number.optional(),
      longitude: schema.number.optional(),
      city: schema.string.optional(),
      state: schema.string.optional(),
      country: schema.string.optional({ trim: true }),
    }),
    personal_phone: schema.string({}, [rules.maxLength(14), rules.minLength(10)]),
    cowork: schema.object
      .optional([rules.requiredWhen('role', '=', UserRoleEnum.COWORKING)])
      .members({
        name: schema.string({ trim: true }),
        email: schema.string.optional({}, [
          rules.email(),
          rules.unique({ table: 'cowork_accounts', column: 'email' }),
        ]),
        phone: schema.string.optional({}, [rules.maxLength(14), rules.minLength(10)]),
        photo_id: schema.number.optional([
          rules.exists({ table: 'photos', column: 'id' }),
        ]),
      }),
    client: schema.object
      .optional([rules.requiredWhen('role', '=', UserRoleEnum.CLIENT)])
      .members({
        company_name: schema.string.optional({ trim: true }),
        company_email: schema.string.optional({}, [rules.email()]),
        company_phone: schema.string.optional({}, [
          rules.maxLength(14),
          rules.minLength(10),
        ]),
        company_address: schema.object.optional().members({
          fulltext: schema.string.optional({ trim: true }),
          latitude: schema.number.optional(),
          longitude: schema.number.optional(),
          country: schema.string.optional({ trim: true }),
        }),
        company_photo_id: schema.number.optional([
          rules.exists({ table: 'photos', column: 'id' }),
        ]),
      }),
  });

  public messages = {
    minLength: 'The {{ field }} must be at least {{ options.minLength }} characters',
    maxLength: 'The {{ field }} must have at most {{ options.maxLength }} characters',
    required: 'The {{ field }} is required',
    exists: 'The {{ field }} is invalid',
    number: 'The {{ field }} must be a number',
    string: 'The {{ string }} must be a string',

    'password_confirmation.confirmed': 'The passwords are different',
    'email.email': 'The email is not valid',
    'email.unique': 'User with email address already exists',
    'cowork.requiredWhen': 'The cowork object must be sent when the user is a coworking',
    'cowork.email.email': 'The email is not valid',
    'cowork.email.unique': 'Company with email address already exists',
    'client.requiredWhen': 'The client object must be sent when the user is a client',
  };
}

请求示例

{
"first_name": "Dan",
"last_name": "Abreu",
"password": "secret1233",
"password_confirmation": "secret1233",
"email": "test@gmail.com",
"role": "CLIENT",
"personal_phone": "1234567890",
"photo_id": null,
"personal_address": {
    "fulltext": "Rua do Lead",
    "city":"Gravatá",
    "state":"Pe",
    "country":"Brazil",
    "longitude": -10.00,
    "latitude": 1.00
},
"client": {
    "company_name": "my company",
    "company_email": "test1@gmail.com",
    "company_phone": "10800-0000",
    "company_photo_id": null,
    "company_address": {
        "fulltext": "Rua do labrador",
        "longitude": -10.00,
        "latitude": 1.00
    }
}
}

相关问题与排查方向

确实有部分Adonis 5开发者在AWS EC2生产环境遇到过类似的验证异常问题,常见触发原因和解决方式如下:

  • 环境变量差异:检查生产环境的APP_KEY是否与本地/预发布环境一致,Adonis 5的验证器依赖APP_KEY进行数据校验,密钥不匹配可能导致隐式验证失败。
  • 时区与数据库连接:生产环境EC2实例的时区可能与数据库时区不一致,导致unique或exists等数据库相关验证规则触发异常,可通过配置.env文件中的TZ参数统一时区。
  • 请求解析问题:EC2前端代理(如Nginx)可能修改了请求体格式,导致Adonis 5无法正确解析JSON数据,可检查代理配置中是否开启了正确的请求体转发设置。
  • 验证器缓存:生产环境可能启用了验证器缓存,若验证器代码更新后未重新构建缓存,会导致旧规则生效,可执行node ace validator:clear清除缓存后重新部署。

内容的提问来源于stack exchange,提问作者Danhdds

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:10:39