Chuyển JSON Schema sang TypeScript, Zod, Pydantic online
Chuyển JSON Schema sang TypeScript, Zod hoặc Pydantic, nâng draft-04/06/07 lên 2020-12, chuyển qua lại OpenAPI 3.0, gộp hoặc giải tham chiếu $ref.
Kết quả
Bấm “Chạy” để xem kết quả.
Gửi JSON Schema (đối tượng, chuỗi JSON hoặc YAML, file hoặc URL) và chọn to. Sinh code: typescript (interface / type kèm JSDoc), zod (schema có ràng buộc min / max / regex / email / uuid / datetime, .strict(), z.lazy cho kiểu đệ quy, kèm kiểu z.infer) và pydantic (model Pydantic v2 với ràng buộc Field(), alias cho khoá không phải định danh hợp lệ, extra="forbid"); definitions / $defs thành các kiểu có tên.
Phiên bản: 2020-12, 2019-09, draft-07, draft-06, draft-04 để nâng hoặc hạ phiên bản (definitions ↔ $defs, id ↔ $id, exclusiveMinimum / Maximum dạng boolean ↔ số, items dạng mảng ↔ prefixItems, dependencies ↔ dependentRequired / dependentSchemas, const ↔ enum, từ khoá đứng cạnh $ref). OpenAPI: openapi-3.0 đổi JSON Schema thành Schema Object của OpenAPI 3.0 (nullable, example, giới hạn exclusive, $defs → components.schemas); json-schema làm chiều ngược lại và nhận cả tài liệu OpenAPI 3.0 (hoặc Swagger 2.0). Tham chiếu: bundle gom các $ref bên ngoài (từ refs = {"common.json": {...}} hoặc URL với fetch=1) vào $defs; dereference thay mọi $ref bằng nội dung và báo vòng lặp (giữ lại dạng $ref, hoặc circular=error).
Những gì không biểu diễn được ở định dạng đích được liệt kê trong warnings. Liên quan: /v1/json-to-types suy kiểu từ dữ liệu mẫu, /v1/schema-mock tạo dữ liệu ví dụ, /v1/openapi-tools xử lý cả tài liệu API. format=text chỉ trả về code.
Dùng trong code của bạn
curl -X POST "https://apibox.io.vn/v1/schema-convert" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"schema":{"title":"Order","type":"object","required":["id","items"],"properties":{"id":{"type":"string","format":"uuid"},"items":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/Item"}},"note":{"type":["string","null"],"maxLength":500}},"$defs":{"Item":{"type":"object","required":["sku","qty"],"properties":{"sku":{"type":"string"},"qty":{"type":"integer","minimum":1}}}}},"to":"zod"}'const res = await fetch('https://apibox.io.vn/v1/schema-convert', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"schema": {
"title": "Order",
"type": "object",
"required": [
"id",
"items"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"items": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/$defs/Item"
}
},
"note": {
"type": [
"string",
"null"
],
"maxLength": 500
}
},
"$defs": {
"Item": {
"type": "object",
"required": [
"sku",
"qty"
],
"properties": {
"sku": {
"type": "string"
},
"qty": {
"type": "integer",
"minimum": 1
}
}
}
}
},
"to": "zod"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/schema-convert",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"schema": {"title": "Order", "type": "object", "required": ["id", "items"], "properties": {"id": {"type": "string", "format": "uuid"}, "items": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/Item"}}, "note": {"type": ["string", "null"], "maxLength": 500}}, "$defs": {"Item": {"type": "object", "required": ["sku", "qty"], "properties": {"sku": {"type": "string"}, "qty": {"type": "integer", "minimum": 1}}}}}, "to": "zod"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/schema-convert');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"schema":{"title":"Order","type":"object","required":["id","items"],"properties":{"id":{"type":"string","format":"uuid"},"items":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/Item"}},"note":{"type":["string","null"],"maxLength":500}},"$defs":{"Item":{"type":"object","required":["sku","qty"],"properties":{"sku":{"type":"string"},"qty":{"type":"integer","minimum":1}}}}},"to":"zod"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Tài liệu API → Nhận API key miễn phí
Hỏi đáp
Sinh code cho Zod phiên bản nào?
API của Zod 3 (z.object, z.string().email()…), Zod 4 vẫn chấp nhận.
Có chuyển TypeScript sang JSON Schema được không?
Không, việc đó cần trình biên dịch TypeScript. Hãy dùng JSON Schema làm nguồn, hoặc suy kiểu từ dữ liệu mẫu bằng json-to-types.
$ref trỏ ra ngoài thì sao?
Gửi các tài liệu được tham chiếu trong refs, hoặc đặt fetch=1 để tải URL http(s) công khai (tối đa 20 tài liệu).