Tạo dữ liệu JSON giả thực tế từ JSON Schema cho API testing, UI prototyping, load testing và contract-driven development.
Bước 1 – Cung cấp JSON Schema
- Dán schema vào editor bên trái, hoặc import từ file/URL/sample.
- Bắt đầu từ schema API thực tế khi có thể để mock data gần với production.
Bước 2 – Làm schema thân thiện với mock
- Tránh các keyword không hỗ trợ như
$ref, dependencies, và schema điều kiện (if/then/else). - Nếu schema của bạn dùng
$ref, hãy thử mở Mock Generator từ công cụ preload và dereference schemas (ví dụ “Generate Mock Data” trên trang code→schema). - Giữ schema tập trung vào types, properties, required, items, formats và constraints.
Bước 3 – Cấu hình settings tạo
- Chọn locale để có tên, địa chỉ và số điện thoại thực tế.
- Đặt seed để mock data có thể tái tạo (tốt cho tests và snapshots).
- Điều chỉnh batch size, array count, number distributions và xác suất trường tùy chọn.
- Dùng mô phỏng missing/dirty data để test validation và UI error handling.
Bước 4 – Tạo và kiểm tra output
- Nhấp “Generate Mock Data” để tạo output tuân theo constraints của schema.
- Nếu giá trị không như mong muốn, hãy siết schema (formats, enums, min/max) và tạo lại.
Bước 5 – Dùng mock data trong tests và generators
- Sao chép hoặc tải JSON xuống và dùng làm fixtures cho unit/integration tests.
- Đưa JSON đã tạo vào code generators (TypeScript/Java/etc.) để tạo DTOs khớp.
Lưu ý quan trọng về tính năng JSON Schema
- Schema dựa vào
$ref, dependencies, hoặc if/then/else cần được đơn giản hóa hoặc dereference trước khi tạo mock. - Validators khác nhau về cách diễn giải drafts/keywords; hãy dùng validator đầy đủ trong CI để contract testing nghiêm ngặt.
Ví dụ: JSON Schema → mock JSON
// JSON Schema (input)
{
"type": "object",
"properties": {
"id": { "type": "string", "format": "uuid" },
"email": { "type": "string", "format": "email" },
"active": { "type": "boolean" },
"createdAt": { "type": "string", "format": "date-time" }
},
"required": ["id", "email", "active", "createdAt"]
}
// Mock JSON (output example)
{
"id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
"email": "[email protected]",
"active": true,
"createdAt": "2024-03-01T10:15:00.000Z"
}