JSON Schema

Younghwan Cha·2022년 2월 7일
0

JSON 데이터는 Key, Value 로 이루어진 데이터 쌍이다.
그렇다면 JSON 데이터가 정해진 규칙에 따라서 구성되어 있는지 유효성 검사는 어떻게 할 수 있을까?
JSON Schema 가 그 대답이 될 수 있겠다.
JSON Schema 는 JSON 형태의 데이터가 정해진 규약에 맞게 구성되어 있는지 검사하는 규칙이다.

JSON Schema 검증 조건 키워드

  • type: 필드의 유효한 데이터 타입을 명시
    • array
      • additionalItems: false 인 경우 선언된 요소 외에 다른 요소는 허용되지 않는다
      • uniqueItems: true 인 경우 중복값을 허용하지 않는다
      • minItems, maxItems: 배열의 길이 검사
    • object
      • dependencies: 속성 간의 의존성 정의
        {
            "type": "object",
            "properties": {
                "customerName": {
                    "type": "string"
                },
                "creditCardNumber": {
                    "type": "string"
                },
                "billingAddress": {
                    "type": "string"
                }
            },
            "dependencies": {
                "creditCardNumber": ["billingAddress"]
            }
        }
        위와 같이 dependencies 를 정의해 놓은 경우 아래와 같은 경우는 허용되지 않는다
        			{
        "customerName": "kimtaeng",
        "creditCardNumber": "12341234"
        }
       
       - minProperties, maxProperties: 객체가 갖고 있는 속성의 최소/최대 개수
       
    {
      "type": "object",
      "minProperties": 2,
      "maxProperties": 3
    }
{
        "type": "array",
        "items": [
              {
                "type": "integer",
            },
            {
                "type": "string",
                "format": "date",
            },
            {
                "type": "string"
            }
        ],
        "additionalItems": false
        "uniqueItems": true
        "minItems": 5,
        "maxItems": 10
 }
  • required: 반드시 포함되어야 하는 속성을 배열 형태로 표현
  • properties: 객체(object) 타입인 경우 속성을 표기
{
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "title": {
            "type": "string"
        },
        "type": {
            "type": "integer"
        }
    },
    "required": ["id", "title"]
}
  • minLength, maxLength: 문자열의 최소, 최대 길이
  • minItems, maxItems: 배열 요소의 최소, 최대 개수
  • pattern: 정규 표현식과 일치한 문자열
    {"type": "string", "pattern": "(younghwan/cha)\\.com"
  • minimum, maximum: 숫자의 최솟값, 최댓값
    • exclusiveMinimum, exclusiveMaximum
  • multipleOf: 배수 판단
    {"type": "number", "multipleOf": 5}
  • format: 해당 문자열이 지정 포멧을 갖는지 확인
    {"type": "string", "format": "date"}
    • 날짜와 시간
      • date-time: 1991-02-04T00:31:00+09:00
      • time: 00:31:00+09:00
      • date: 1991-02-04
    • 이메일
      • email: kimtaeng@madplay.com
    • 호스트 이름
      • hostname: kimtaeng-madplay.com
    • IP
      • ipv4: 123.123.123.123
      • ipv6: 2001:0DB8:0000:0000:0000:0000:1428:57ab
  • contentEncoding, contentMediaType: 미디어 타입에 대한 검증
    {"type": "string", "contentEncoding": "base64", "contentMediaType": "application/json"}

JSON Schema 데이터 타입

JSON Schema 메타데이터 키워드

  • title: 스키마에 대한 이름
  • description: 스키마에 대한 설명
  • example: 스키마 검증에 통과하는 예시를 나열
  • $comment: 특정 스키마에 주석 작성
{
    "title": "어떤어떤 스키마",
    "description": "이 스키마는 이렇게 저렇게 하는 용도입니다.",
    "example": ["mad", "play"],

    "$comment": "음... 그냥 남겨 봅니다 :)",
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "name": {
            "$comment": "음, 이게 과연 좋은 건지 모르겠어요 :(",
            "type": "string",
            "pattern": "^a"
        }
    }
}

https://madplay.github.io/post/json-schema-validation-examples
https://madplay.github.io/post/multiple-and-conditional-json-schemas-validation-examples

profile
개발 기록

0개의 댓글

관련 채용 정보