Skip to content

Model Validation

StateZero provides client-side validation that leverages your server-side serializers as the source of truth. This approach ensures consistency between frontend validation and backend data integrity.

Philosophy

We favor server-side validation because:

  • Serializers are the source of truth - Your Django REST Framework serializers already define validation rules
  • Consistency - Frontend and backend use the same validation logic
  • Security - Server-side validation cannot be bypassed by malicious clients
  • Maintainability - One place to define and update validation rules

Performance

Validation is fast because it performs no database operations. It only:

  • Checks users basic permissions (not model instance specific permissions)
  • Validates data structure using your existing serializers
  • Returns validation results without side effects

API

Static Method

javascript
await Model.validate(data, validate_type, partial)

Instance Method

javascript
await instance.validate(validate_type, partial)

Parameters

validate_type: 'create' or 'update'

  • Different validation rules may apply
  • Permission checks vary by operation type

partial: boolean

  • false - Validate all required fields (full validation)
  • true - Validate only provided fields (field-level validation)

Use Cases

  • Form validation - Validate before submission
  • Field-level validation - Check individual fields as users type
  • Pre-save validation - Ensure data validity before database operations

Error Handling

Validation throws standard StateZero exceptions:

  • ValidationError for data validation failures
  • PermissionDenied for insufficient permissions