JSON Schema 정의
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 2, "maxLength": 50 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 }
},
"required": ["name", "email"]
}검증 (ajv)
import Ajv from "ajv";
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(data)) {
console.log(validate.errors);
}Fastify는 JSON Schema를 네이티브로 지원하여 문서화와 검증이 자동입니다.
댓글 0