有没有一种方法可以要求对象上的两个数组属性具有相同的长度?

huangapple go评论82阅读模式
英文:

Is there a way to require two array properties on an object to have the same length?

问题

我正在尝试修改一个Swagger模型。我的目标是将一个对象附加的两个属性从字符串改为字符串数组。到目前为止,我有以下代码:

config:
  type: object
  minLength: 1
  required:
    - field1
    - field2
  properties:
    field1:
      type: array
      minItems: 1
      items:
        type: string
        pattern: '[a-zA-Z]*'
        x-nullable: false
    field2:
      type: array
      minItems: 1
      items:
        type: string
        pattern: '[a-zA-Z]*'
        x-nullable: false

之前,这两个字段的定义是现在的项定义。这些修改基本上满足了我的需求,但是我想要求field1field2这两个数组具有相同的长度。有没有办法做到这一点?我可以看到如何修改生成的文件来满足这个要求,但是我非常不想每次重新生成文件时都要这样做。

英文:

I'm trying to modify a swagger model. My goal is two change a pair of properties attached to an object that are currently strings to be arrays of strings. Thus far I have

config:
  type: object
  minLength: 1
  required:
    - field1
    - field2
  properties:
    field1:
      type: array
      minItems: 1
      items:
        type: string
        pattern: '[a-zA-Z]*'
        x-nullable: false
    field2:
      type: array
      minItems: 1
      items:
        type: string
        pattern: '[a-zA-Z]*'
        x-nullable: false

Previously the definitions for the two fields were what are now the item definitions. The modifications mostly addresses my needs, however, I would like to require that the two arrays field1 and field2 have the same length. Is there a way to do this? I can see how I might modify the generated file to enforce this requirement, but I would much prefer not to have to do this every time I want to regenerate that file.

答案1

得分: 1

我想要求field1field2这两个数组具有相同的长度。有没有办法实现这个要求?

这个要求无法通过OpenAPI规范来表达。你只能在模式(schema)的description中提及这个要求。

英文:

> I would like to require that the two arrays field1 and field2 have the same length. Is there a way to do this?

This cannot be expressed using OpenAPI Specification. The most you can do is mention this requirement in the schema description.

huangapple
  • 本文由 发表于 2022年5月27日 09:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72399670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定