Apa itu JSON Schema dan Cara Memvalidasinya
JSON Schema adalah "blueprint" atau aturan yang mendefinisikan struktur, tipe data, dan batasan nilai yang valid untuk dokumen JSON. Dengan JSON Schema, tim bisa memastikan data yang masuk/keluar dari API selalu konsisten dengan kontrak yang disepakati.
Contoh Sederhana
Misal kita punya data user seperti ini:
{
"nama": "Budi",
"umur": 28,
"email": "budi@example.com"
}
JSON Schema yang memvalidasi struktur di atas:
{
"type": "object",
"properties": {
"nama": { "type": "string" },
"umur": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
},
"required": ["nama", "umur", "email"]
}
Kenapa JSON Schema Penting?
- Kontrak API yang jelas — frontend dan backend bisa sepakat soal struktur data tanpa perlu baca kode satu sama lain.
- Validasi otomatis — request/response yang tidak sesuai schema bisa langsung ditolak sebelum masuk logika bisnis.
- Dokumentasi hidup — schema bisa dipakai untuk generate dokumentasi API secara otomatis (misalnya OpenAPI/Swagger).
- Testing lebih mudah — data dummy untuk testing bisa di-generate otomatis dari schema.
Tipe Validasi yang Umum Dipakai
type— string, number, integer, boolean, object, array, nullrequired— daftar field yang wajib adaminimum/maximum— batas nilai angkapattern— regex untuk validasi format stringenum— daftar nilai yang diperbolehkanminLength/maxLength— batas panjang string
💡 Sebelum menulis schema, pastikan dulu struktur data contohnya benar-benar valid dan rapi. Gunakan JSONYAMify untuk memformat dan memeriksa data contoh sebelum dijadikan basis schema.
🔧 Rapikan JSON Sebelum Membuat Schema
What is JSON Schema and How to Validate With It
JSON Schema is a "blueprint" — a set of rules that defines the structure, data types, and valid value constraints for a JSON document. With JSON Schema, teams can guarantee data flowing in and out of an API always matches an agreed-upon contract.
A Simple Example
Say we have this user data:
{
"name": "John",
"age": 28,
"email": "john@example.com"
}
A JSON Schema that validates the structure above:
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "age", "email"]
}
Why Does JSON Schema Matter?
- Clear API contracts — frontend and backend can agree on data structure without reading each other's code.
- Automatic validation — requests/responses that don't match the schema get rejected before reaching business logic.
- Living documentation — schemas can auto-generate API docs (e.g. via OpenAPI/Swagger).
- Easier testing — dummy test data can be auto-generated straight from the schema.
Common Validation Keywords
type— string, number, integer, boolean, object, array, nullrequired— list of mandatory fieldsminimum/maximum— numeric boundspattern— regex for string format validationenum— list of allowed valuesminLength/maxLength— string length bounds
💡 Before writing a schema, make sure your example data is actually valid and clean. Use JSONYAMify to format and inspect your sample data before basing a schema on it.
🔧 Clean Up JSON Before Building a Schema