> ## Documentation Index
> Fetch the complete documentation index at: https://docs.joyfill.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Schema Validation

This guide shows you how to validate document structure using Joyfill's schema validation system.

### **What is Schema Validation**

Schema validation checks if your JSON document follows the correct Joyfill document structure before creating a form.

### **Enabling/Disabling Schema Validation**

```swift theme={null}
let documentEditor = DocumentEditor(
    document: document,
    validateSchema: true  // Default is true
)

// Check for schema errors
if let error = documentEditor.schemaError {
    print("Schema validation failed: \(error.message)")
    // Handle error - show message, prevent form display, etc.
}
```

### **Manual Validation**

```swift theme={null}
let schemaManager = JoyfillSchemaManager()

if let error = schemaManager.validateSchema(document: document) {
   print("Schema validation failed: \(error.message)")
   print("Error code: \(error.code)")
   print("Errors: \(String(describing: error.error))")
} else {
   print("Document schema is valid")
}
```

### **How Schema Validation Works**

When validateSchema = true:

1. **Document is validated** against the Joyfill schema during editor creation
2. **If validation fails**, an error is set and onError callback is triggered
3. **If validation passes**, the editor is created normally

**Common Error Codes**

| Error Code                | Description                   | Solution               |
| ------------------------- | ----------------------------- | ---------------------- |
| ERROR\_SCHEMA\_VALIDATION | Document structure is invalid | Fix document structure |
| ERROR\_SCHEMA\_VERSION    | Version incompatibility       | Update SDK or document |

**FormChangeEvent Integration**

Schema errors automatically trigger the onError event:

```swift theme={null}
func onError(error: Joyfill.JoyfillError) {
        switch error {
        case .schemaValidationError(let schemaError):
            print("❌ Schema Error: \(schemaError)")
        case .schemaVersionError(let versionError):
            print("❌ Schema Error: \(versionError)")
        }
        print("Error occurred: \(error)")
    }
```

## **Important Notes**

> ⚠️ **Default Behavior**: Schema validation is **enabled by default** (validateSchema = true)

>  🚫 **Not Recommended**: Disabling schema validation can lead to crashes - only use for testing

>  🔍 **Error Details**: Schema errors include both user-friendly messages and technical details

>  ⚡ **Validation Timing**: Schema validation happens during editor creation, not during form interaction
