struct Athena::Validator::Bundle
inherits Athena::DependencyInjection::AbstractBundle
#
Integrates the Validator component with the dependency injection component, such as within an Athena Framework application.
Require athena-validator/bundle to register the bundle; it does not require any configuration:
require "athena"
require "athena-validator/bundle"
The bundle registers the validator service, which may be injected via AVD::Validator::ValidatorInterface:
@[ADI::Register]
class ArticleImporter
def initialize(@validator : AVD::Validator::ValidatorInterface); end
def import(article : Article) : Nil
violations = @validator.validate article
# ...
end
end
When registered, the framework also uses it to validate request bodies, see ATHR::RequestBody.
Service Constraint Validators#
Constraint validators that need other services can inherit from AVD::ServiceConstraintValidator and be registered as a service.
The bundle tags these services with validator.constraint_validator, and provides them to the validator's AVD::ConstraintValidatorFactoryInterface:
class Athena::Validator::Constraints::UniqueEmail < AVD::Constraint
# ...
@[ADI::Register]
class Validator < AVD::ServiceConstraintValidator
def initialize(@user_repository : UserRepository); end
# :inherit:
def validate(value : _, constraint : AVD::Constraints::UniqueEmail) : Nil
# ...
end
end
end