A-la-carte
A-la-carte validation allows you to define constraints dynamically within your event handlers, providing maximum flexibility for validation scenarios that don't warrant permanent constraint definitions. This approach is perfect for:
API endpoints with specific validation requirements
Form processing with dynamic validation rules
Data import/export operations with varying formats
Administrative functions with custom validation logic
Temporary or one-off validation scenarios
Overview
With a-la-carte validation, you can:
Create constraints on-the-fly within handler methods
Store constraint definitions in databases, JSON files, or services
Dynamically build validation rules based on business logic
Validate any struct, object, or request collection
Control exactly which fields to validate using inclusion/exclusion parameters
Basic A-la-carte Validation
Simple Request Collection Validation
Exception-Based Validation
For cleaner API code, use validateOrFail() to automatically throw exceptions on validation failure:
Field Inclusion and Exclusion
Control exactly which fields get validated using the includeFields and excludeFields parameters:
Dynamic Constraint Building
Build constraints programmatically based on business logic or user roles:
External Constraint Sources
Load constraints from external sources like databases, JSON files, or services:
Complex Data Structure Validation
Validate nested objects and arrays using a-la-carte constraints:
Best Practices
1. Use for Specific Scenarios
A-la-carte validation is best for endpoint-specific requirements, not general business rules.
2. Keep Constraints Close to Usage
Define constraints within the handler method for better maintainability and context.
3. Leverage Field Control Parameters
Use includeFields and excludeFields to validate only relevant data.
4. Handle Complex Data Structures
Use dot notation and asterisk wildcards for nested objects and arrays.
5. Consider External Sources
For dynamic forms or configurable validation, load constraints from databases or configuration files.
6. Provide Meaningful Error Messages
Include custom error messages that guide users toward resolution.
A-la-carte validation provides the flexibility to handle unique validation scenarios while maintaining the power and consistency of CBValidation's constraint system. It's perfect for API endpoints, dynamic forms, and any situation where domain object constraints aren't sufficient or appropriate.
Last updated
Was this helpful?