Validation and error handling in middleware  hooks.