πŸ› οΈToolsShed

Validator JSON Schema

Validasi dokumen JSON terhadap JSON Schema (Draft 7) dengan pesan kesalahan terperinci.

Pertanyaan yang Sering Diajukan

Implementasi Kode

import json
import jsonschema
from jsonschema import validate, ValidationError

# pip install jsonschema

schema = {
    "type": "object",
    "properties": {
        "name":  { "type": "string", "minLength": 1 },
        "age":   { "type": "integer", "minimum": 0 },
        "email": { "type": "string", "format": "email" }
    },
    "required": ["name", "age"]
}

# Valid data
data = { "name": "Alice", "age": 30, "email": "alice@example.com" }
try:
    validate(instance=data, schema=schema)
    print("Valid!")
except ValidationError as e:
    print(f"Invalid: {e.message}")

# Invalid data
bad_data = { "name": "", "age": -1 }
try:
    validate(instance=bad_data, schema=schema)
except ValidationError as e:
    print(f"Invalid: {e.message}")
    # Invalid: '' is too short

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.