Domain Object

Domain object constraints are the most powerful and maintainable way to define validation rules in CBValidation. By declaring constraints directly within your model objects, you create self-validating entities that encapsulate both data structure and validation logic.

Overview

Within any domain object (model, entity, or component), you can define a public variable called this.constraints that contains validation rules for your object's properties. This approach provides:

  • Encapsulation: Validation rules live with the data they validate

  • Reusability: Objects carry their validation wherever they're used

  • Maintainability: Single source of truth for validation logic

  • IDE Support: Auto-completion and syntax highlighting for constraints

Basic Declaration

models/User.bx
// Object properties
property id;
property fname;
property lname;
property email;
property username;
property password;
property age;

// Validation constraints
this.constraints = {
    // Constraints go here
};

Complete Constraint Definitions

Here's a comprehensive example showing various constraint types and validation rules:

By default all properties are of type string and not required. Always explicitly define your constraints for clarity and maintainability.

Constraint Profiles for Targeted Validation

Use constraint profiles to validate only specific fields for different scenarios:

Usage in Your Application

Basic Validation

Once constraints are defined, you can validate objects directly:

Profile-Based Validation

Use constraint profiles for specific validation scenarios:

Exception-Based Validation

For APIs or scenarios where you want validation failures to throw exceptions:

Advanced Domain Object Examples

Complex Nested Objects

Domain objects can contain complex nested structures:

Best Practices

1. Use Meaningful Constraint Names

Match constraint field names exactly to your object properties for clarity and maintainability.

2. Provide Custom Messages

Always include custom error messages that are user-friendly and actionable.

3. Leverage Constraint Profiles

Use profiles to validate only relevant fields for different business scenarios.

Organize constraints logically within your object definition for better readability.

5. Handle Nested Data

Use dot notation and asterisk wildcards for complex data structures.

6. Validate Early and Often

Validate at the domain object level to catch issues before they propagate through your application.

Domain object constraints provide the most maintainable and reusable approach to validation in CBValidation, ensuring your business objects are always self-validating and consistent across your entire application.

Last updated

Was this helpful?