user_management.domain.specifications package
Subpackages
- user_management.domain.specifications.user package
- Submodules
- user_management.domain.specifications.user.date_of_birth module
- user_management.domain.specifications.user.email module
- user_management.domain.specifications.user.name module
- user_management.domain.specifications.user.phone module
- Module contents
Submodules
user_management.domain.specifications.and_specification module
- class user_management.domain.specifications.and_specification.AndSpecification(*specs: Specification[T])[source]
Bases:
Specification[T]Composes multiple specifications using logical AND.
This specification is satisfied only if all composed specifications are satisfied by the candidate object. Useful for enforcing multiple business rules simultaneously during validation workflows.
Example
combined = ValidEmailSpecification().and_(ValidNameSpecification()) combined.is_satisfied_by(user_data) # True only if both pass
- __init__(*specs: Specification[T])[source]
Initializes the composite specification with one or more child specifications.
- Parameters:
*specs (Specification[T]) – One or more specifications to combine.
user_management.domain.specifications.not_specification module
- class user_management.domain.specifications.not_specification.NotSpecification(spec: Specification[T])[source]
Bases:
Specification[T]Composes a specification with logical negation (NOT).
This specification is satisfied if the wrapped specification is NOT satisfied. Useful for expressing rules like “user must not be locked” or “email must not be blacklisted”.
Example
active_user_spec = NotSpecification(IsLockedSpecification()) if active_user_spec.is_satisfied_by(user):
grant_access()
- __init__(spec: Specification[T])[source]
Initializes the negated specification with a child specification.
- Parameters:
spec (Specification[T]) – The specification to negate.
user_management.domain.specifications.or_specification module
- class user_management.domain.specifications.or_specification.OrSpecification(*specs: Specification[T])[source]
Bases:
Specification[T]Composes multiple specifications using logical OR.
This specification is satisfied if at least one of the composed specifications is satisfied by the candidate object. Useful for defining alternative valid states or flexible business rules during validation workflows.
Example
eligible_role = IsDoctorSpecification().or_(IsNurseSpecification()) if eligible_role.is_satisfied_by(user):
assign_to_unit()
- __init__(*specs: Specification[T])[source]
Initializes the composite specification with one or more child specifications.
- Parameters:
*specs (Specification[T]) – One or more specifications to combine.
user_management.domain.specifications.specification module
Base Specification pattern implementation.
Defines an abstract interface for business rule specifications, enabling validation and composition of domain rules through logical operations such as AND, OR, and NOT. Supports the creation of complex, reusable, and testable business logic in a clean and expressive way.
- class user_management.domain.specifications.specification.Specification[source]
Bases:
Generic[T],ABCAbstract base class for domain rule specifications.
Represents a business rule that evaluates whether a candidate object satisfies a given condition. Specifications can be composed using logical operators (and_, or_, not_) to build more complex validation logic.
This pattern promotes high cohesion, reusability, and clarity in enforcing domain invariants across different use cases.
- and_(other: Specification[T]) AndSpecification[T][source]
Composes this specification with another using logical AND.
The resulting specification is satisfied only if both this and the provided specification are satisfied.
- Parameters:
other (Specification[T]) – Another specification to combine.
- Returns:
A new composite specification.
- Return type:
- abstractmethod is_satisfied_by(candidate: T) bool[source]
Determines whether the candidate satisfies this specification.
- Parameters:
candidate (T) – The object to evaluate against the rule.
- Returns:
True if the rule is satisfied; False otherwise.
- Return type:
bool
- not_() NotSpecification[T][source]
Negates this specification using logical NOT.
The resulting specification is satisfied if this specification is NOT satisfied by the candidate.
- Returns:
A new negated specification.
- Return type:
- or_(other: Specification[T]) OrSpecification[T][source]
Composes this specification with another using logical OR.
The resulting specification is satisfied if at least one of the two specifications is satisfied.
- Parameters:
other (Specification[T]) – Another specification to combine.
- Returns:
A new composite specification.
- Return type:
Module contents
Public API for the Specification pattern in the domain layer.
This module provides the foundational building blocks for expressing and composing business rules as reusable, testable objects. It enables logical composition via and_, or_, and not_ methods, supporting complex validation workflows in a clean, readable way.
Import specification base classes directly using: from .specification import Specification from .and_specification import AndSpecification from .or_specification import OrSpecification from .not_specification import NotSpecification )
- class user_management.domain.specifications.AndSpecification(*specs: Specification[T])[source]
Bases:
Specification[T]Composes multiple specifications using logical AND.
This specification is satisfied only if all composed specifications are satisfied by the candidate object. Useful for enforcing multiple business rules simultaneously during validation workflows.
Example
combined = ValidEmailSpecification().and_(ValidNameSpecification()) combined.is_satisfied_by(user_data) # True only if both pass
- __init__(*specs: Specification[T])[source]
Initializes the composite specification with one or more child specifications.
- Parameters:
*specs (Specification[T]) – One or more specifications to combine.
- class user_management.domain.specifications.NotSpecification(spec: Specification[T])[source]
Bases:
Specification[T]Composes a specification with logical negation (NOT).
This specification is satisfied if the wrapped specification is NOT satisfied. Useful for expressing rules like “user must not be locked” or “email must not be blacklisted”.
Example
active_user_spec = NotSpecification(IsLockedSpecification()) if active_user_spec.is_satisfied_by(user):
grant_access()
- __init__(spec: Specification[T])[source]
Initializes the negated specification with a child specification.
- Parameters:
spec (Specification[T]) – The specification to negate.
- class user_management.domain.specifications.OrSpecification(*specs: Specification[T])[source]
Bases:
Specification[T]Composes multiple specifications using logical OR.
This specification is satisfied if at least one of the composed specifications is satisfied by the candidate object. Useful for defining alternative valid states or flexible business rules during validation workflows.
Example
eligible_role = IsDoctorSpecification().or_(IsNurseSpecification()) if eligible_role.is_satisfied_by(user):
assign_to_unit()
- __init__(*specs: Specification[T])[source]
Initializes the composite specification with one or more child specifications.
- Parameters:
*specs (Specification[T]) – One or more specifications to combine.
- class user_management.domain.specifications.Specification[source]
Bases:
Generic[T],ABCAbstract base class for domain rule specifications.
Represents a business rule that evaluates whether a candidate object satisfies a given condition. Specifications can be composed using logical operators (and_, or_, not_) to build more complex validation logic.
This pattern promotes high cohesion, reusability, and clarity in enforcing domain invariants across different use cases.
- and_(other: Specification[T]) AndSpecification[T][source]
Composes this specification with another using logical AND.
The resulting specification is satisfied only if both this and the provided specification are satisfied.
- Parameters:
other (Specification[T]) – Another specification to combine.
- Returns:
A new composite specification.
- Return type:
- abstractmethod is_satisfied_by(candidate: T) bool[source]
Determines whether the candidate satisfies this specification.
- Parameters:
candidate (T) – The object to evaluate against the rule.
- Returns:
True if the rule is satisfied; False otherwise.
- Return type:
bool
- not_() NotSpecification[T][source]
Negates this specification using logical NOT.
The resulting specification is satisfied if this specification is NOT satisfied by the candidate.
- Returns:
A new negated specification.
- Return type:
- or_(other: Specification[T]) OrSpecification[T][source]
Composes this specification with another using logical OR.
The resulting specification is satisfied if at least one of the two specifications is satisfied.
- Parameters:
other (Specification[T]) – Another specification to combine.
- Returns:
A new composite specification.
- Return type: