programing

여러 JSON 스키마 파일을 관리하는 방법

linuxpc 2023. 3. 27. 21:03
반응형

여러 JSON 스키마 파일을 관리하는 방법

commonjs-utils의 node.js + json-schema.js를 사용하여 JSON API를 검증하려고 합니다.단일 검증은 쉬웠지만 서로 참조할 수 있도록 여러 스키마 파일을 관리하는 올바른 방법을 찾을 수 없었습니다.

모델 2개와 API 2개가 있다고 가정합니다.

// book
{
  "type": "object",
  "properties": {
      "title": { "type": "string" },
      "author": { "type": "string" }
  }
}
// author
{
  "type": "object",
  "properties": {
      "first_name": { "type": "string" },
      "last_name": { "type": "string" }
  }
}  
// authors API
{
  "type": "array",
  "items": { "$ref": "author" }
}
// books API: list of books written by same author
{
  "type": "object",
  "properties": {
    "author": { "$ref": "author" } 
    "books": { "type": "array", "items": { "$ref": "book" } }
  }
}  

각 스키마는 별도의 파일로 분할되어 온라인 상태가 되어야 합니다.아니면 아래와 같이 하나의 스키마 파일로 결합할 수 있나요?가능하다면 로컬 스키마를 어떻게 참조할 수 있을까요?

// single schema file {
    "book": { ... },
    "author": { ... },
    "authors": { ... },
    "books": { ... } }

JSON Schema에서는 파일별로 스키마를 삽입한 후 URL(저장 장소)을 사용하여 액세스하거나, 파일별로 큰 스키마를 사용하여 액세스 할 수 있습니다.id태그를 지정합니다.

다음은 하나의 큰 파일에 대한 것입니다.

{
    "id": "#root",
    "properties": {
        "author": {
            "id": "#author",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        // author
        "author_api": {
            "id": "#author_api",
            "items": {
                "$ref": "author"
            },
            "type": "array"
        },
        // authors API
        "book": {
            "id": "#book",
            "properties": {
                "author": {
                    "type": "string"
                },
                "title": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        // books API: list of books written by same author
        "books_api": {
            "id": "#books_api",
            "properties": {
                "author": {
                    "$ref": "author"
                },
                "books": {
                    "items": {
                        "$ref": "book"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        }
    }
}

그런 다음 검증자를 참조할 수 있습니다(이러한 서브스키마에는id).

스키마 외부에서 다음과 같이 입력합니다.

{ "$ref": "url://to/your/schema#root/properties/book" }

다음과 같습니다.

{ "$ref": "url://to/your/schema#book" }

내부적으로는 다음과 같습니다.

{ "$ref": "#root/properties/book" }

또는 이것(내부로부터 계속):

{ "$ref": "#book" }

자세한 내용은 여기를 참조하십시오.

언급URL : https://stackoverflow.com/questions/8179137/how-to-manage-multiple-json-schema-files

반응형