Best Practices
Guidelines and recommendations for using Bantai
Best Practices
Guidelines and recommendations for effectively using Bantai in your applications.
Strategy Selection
- Use Preemptive Strategy for critical checks that should fail fast (e.g., authentication, rate limiting)
- Use Exhaustive Strategy for validation where you want to show all errors (e.g., form validation, quota information)
Code Organization
- Provide Clear Reasons in your
allow()anddeny()calls to help with debugging and user feedback - Keep Rules Focused - each rule should check one specific thing
- Use Context Defaults for optional fields with sensible defaults
- Leverage Extensions for common functionality like storage and rate limiting
Example: Preemptive Strategy
// Good for security checks
const securityPolicy = definePolicy(context, 'security', [authRule, permissionRule], {
defaultStrategy: 'preemptive', // Fail fast
});Example: Exhaustive Strategy
// Good for form validation
const validationPolicy = definePolicy(
context,
'validation',
[emailRule, passwordRule, termsRule],
{
defaultStrategy: 'exhaustive', // Collect all errors
}
);Related Documentation
- Concepts - Best Practices - Detailed best practices guide
- Examples - See these practices in action