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

객체 — additionalProperties, patternProperties

~10 min · json-schema, objects, additional-properties

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

나열 안 한 property 도 정책 필요

additionalProperties

기본으로 properties 에 나열 안 된 property 는 허용. 금지하려면 additionalProperties: false — schema 가 'closed' 가 됨. sub-schema 로 검증하려면 additionalProperties 에 schema (예: { "type": "string" }: '다른 모든 키는 string 이어야 함').

patternProperties

가끔 property 이름 이 패턴 따름. patternProperties 가 키-regex 를 schema 에 매핑. "^env_": {"type": "string"} = 'env_ 로 시작하는 모든 키는 string'. 고전 사용: env-var 맵, locale 맵 ("^[a-z]{2}(-[A-Z]{2})?$").

propertyNames

키 문자열 자체 제약: "propertyNames": {"pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"} = '모든 키가 유효한 식별자'. JSON 키가 downstream 에서 변수 이름 될 때 유용.

'기본 closed' 는 forwards-compatibility 비용. additionalProperties: false 의 schema 는 v1 validator 에서 유효한 v2 문서 거부. public API 는 보통 열어 둠 (또는 정책 명시적 문서화); 사적 내부 schema 는 자주 닫음. 반사 아니라 의식적으로 골라.

Code

additionalProperties: false (closed schema)·json
{
  "type": "object",
  "properties": {
    "id":   { "type": "integer" },
    "name": { "type": "string" }
  },
  "required": ["id", "name"],
  "additionalProperties": false
}
additionalProperties 가 schema (typed map)·json
{
  "type": "object",
  "properties": {
    "name":  { "type": "string" },
    "email": { "type": "string" }
  },
  "additionalProperties": { "type": "string" }
}
patternProperties — regex 매칭 키·json
{
  "type": "object",
  "patternProperties": {
    "^env_":     { "type": "string" },
    "_count$":   { "type": "integer", "minimum": 0 }
  },
  "additionalProperties": false
}
propertyNames — 키 자체 제약·json
{
  "type": "object",
  "propertyNames": {
    "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
  },
  "additionalProperties": { "type": "any" }
}

External links

Exercise

관리하는 config schema 골라. 최상위마다 결정: closed 또는 open. 하나엔 additionalProperties: false 추가, config 파일에 일부러 키 오타 내고 validator 가 잡는 거 봐. 30 분 디버그 세션에서 한 번 구해주면 신자 됨.

Progress

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

댓글 0

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

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