programing

json 스키마에서 배열의 최소 크기를 정의하는 방법

mailnote 2023. 3. 27. 21:24
반응형

json 스키마에서 배열의 최소 크기를 정의하는 방법

json 파일의 스키마를 만들고 싶습니다.여러 가지 제품을 위한 것입니다.

json 스키마는 다음과 같습니다.

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product set",
"type": "array",
"items": {
    "title": "Product",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "number"
        },
        "name": {
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        },
        "tags": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "minItems": 1,
            "uniqueItems": true
        },
        "dimensions": {
            "type": "object",
            "properties": {
                "length": {"type": "number"},
                "width": {"type": "number"},
                "height": {"type": "number"}
            },
            "required": ["length", "width", "height"]
        },
        "warehouseLocation": {
            "description": "Coordinates of the warehouse with the product",
            "$ref": "http://json-schema.org/geo"
        }
    },
    "required": ["id", "name", "price"]
}
}

어레이에는 적어도1개의 항목이 포함되어 있어야 합니다.어레이의 최소값을 정의하려면 어떻게 해야 합니까?

최소 디피니션을 추가해야 합니까?

배열 내의 최소 항목 수를 설정하려면"minItems".

참조:

https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.3.3

그리고.

http://jsonary.com/documentation/json-schema/?section=section/Array%20 검증

   {
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "object",
   "properties": {
      ...
      "tags": {
          "type": "array",
          "items": {
              "type": "string"
          },
          "minItems": 1,
          "maxItems": 4,
          "uniqueItems": true
      }
  },
  "required": ["id", "name", "price"]
  }

초안 v4가 원하는 것을 허용하는 것 같습니다.http://json-schema.org/example1.html 에서 :

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
    ...
    "tags": {
        "type": "array",
        "items": {
            "type": "string"
        },
        "minItems": 1,
        "uniqueItems": true
    }
},
"required": ["id", "name", "price"]
}

"tags" 속성은 최소 항목 수(1)를 포함하는 배열로 정의됩니다.

아니겠죠, 적어도 초안 작성은 해야 할 것 같아요.minimum는 배열이 아닌 숫자 값에만 적용됩니다.

5.1. 숫자 인스턴스 검증 키워드(숫자 및 정수)
...
5.1.3 최소 및 배타적 최소

따라서 어레이의 최소 항목/최대 항목에 대해 잘 알고 있어야 합니다.

언급URL : https://stackoverflow.com/questions/16583485/how-to-define-the-min-size-of-array-in-the-json-schema

반응형