【问题标题】:Loopback 4 auto generated model with required id failing validationLoopback 4 自动生成的模型,所需的 id 验证失败
【发布时间】:2022-11-11 11:11:45
【问题描述】:

我正在使用一个使用 lb4 cli 运行自动生成模型的自动化脚本。 看起来验证需要提供 id ,但在其架构中大摇大摆地错过了它。为什么我在 swagger 中看不到 id 属性?

请注意!我不想手动修改我的模型

lb4 model activity --dataSource DS --table activity

创建模型:

export class Activity extends Entity {
  @property({
    type: 'string',
    required: true,
    id: 1,
    postgresql: {
      columnName: 'id',
      dataType: 'uuid',
      dataLength: null,
      dataPrecision: null,
      dataScale: null,
      nullable: 'NO',
    },
  })
  id: string;
...
}

当我运行 swagger 工具并尝试发布新活动时,它缺少 id 字段并返回以下错误:

{
  "error": {
    "statusCode": 422,
    "name": "ValidationError",
    "message": "The `Activity` instance is not valid. Details: `id` can't be blank (value: undefined).",
    "details": {
      "context": "Activity",
      "codes": {
        "id": [
          "presence"
        ]
      },
      "messages": {
        "id": [
          "can't be blank"
        ]
      }
    }
  }
}

如果我手动添加属性id,则会引发验证错误:

{
  "error": {
    "statusCode": 422,
    "name": "UnprocessableEntityError",
    "message": "The request body is invalid. See error object `details` property for more info.",
    "code": "VALIDATION_FAILED",
    "details": [
      {
        "path": "",
        "code": "additionalProperties",
        "message": "must NOT have additional properties",
        "info": {
          "additionalProperty": "id"
        }
      }
    ]
  }
}

【问题讨论】:

    标签: api rest strongloop loopback4


    【解决方案1】:

    把你的模型文件放在这个:

    // Indexer property to allow additional data // eslint-disable-next-line @typescript-eslint/no-explicit-any [prop: string]: any;

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2016-09-07
      • 2023-03-11
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多