C.W.K.
Stream
Lesson 02 of 08 · published

type, properties, required — 세 일꾼

~10 min · json-schema, type, properties

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

모양 선언의 핵심

JSON Schema 작업의 대부분을 세 키워드가 함: type 이 JSON 타입 선언, properties 가 객체의 키 선언, required 가 어느 키가 필수인지 나열.

Type 값

일곱 type 값이 JSON 의 6 + 분리 하나에 매핑: "string", "number", "integer", "boolean", "null", "object", "array". integernumber 의 subtype — validator 가 값에 소수 부분 없는지 확인.

Type union

union 엔 type 에 배열 전달: "type": ["string", "null"] = 'string 이나 null', nullable 필드 흔한 모양.

required 는 opt-in

기본으로 모든 property 가 옵션. required 가 반드시 있어야 할 property 이름 배열. required 생략하면 다 옵션 — schema 가 빈 객체를 valid 로 검증.

'required' 는 'properties' 옆에 살지 그 안 아냐. 흔한 버그가 각 property 안에 required: true 넣는 거 — 그건 Draft-04 문법, Draft-06+ 에서 제거됨. modern JSON Schema 는 객체 단계의 배열 사용: "required": ["name", "email"].

Code

필수 + 옵션 필드 가진 객체·json
{
  "type": "object",
  "properties": {
    "id":         { "type": "integer" },
    "name":       { "type": "string" },
    "email":      { "type": "string" },
    "created_at": { "type": "string" },
    "deleted_at": { "type": ["string", "null"] }
  },
  "required": ["id", "name", "email"]
}
중첩 객체 type·json
{
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "email": { "type": "string" }
      },
      "required": ["name", "email"]
    }
  },
  "required": ["user"]
}
Type union (nullable)·json
{
  "type": "object",
  "properties": {
    "middle_name": { "type": ["string", "null"] }
  }
}

External links

Exercise

최근 작업한 작은 JSON 문서 (API 응답, config) 골라. 처음부터 JSON Schema 작성 — type 선언, 필수 필드 나열. validator 실행 (ajv 또는 Python jsonschema). 한 필드씩 깨뜨리고 에러 메시지가 정확히 뭘 실패했는지 알려주는 거 봐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.