Derives: Validation (reference)¶
This page specifies Validate. The derive catalog and the automatic derives are in Derives and traits.
Validate¶
- Provides:
TypeName.new(...) -> Result[TypeName, E], validated construction. - Provided by:
@derive(Validate)on a model. On a class, enum or newtype it is refused (INCAN-T0001). - Behavior:
newconstructs the model from its arguments, callsvalidate(self), and returns its result. - Dunder: none.
- Requires: the model defines
validate(self) -> Result[Self, E], whereResult[TypeName, E]is the same return type andEis any type. A model withoutvalidate, or whosevalidatehas another receiver, takes parameters, isasync, or returns anything but aResultof the model, is refused (INCAN-T0001).
Rules:
newtakes the model's fields that declare no default, by name or positionally in declaration order; each field that declares a default takes it. Passing a field that declares a default tonewis refused (INCAN-T0001).- A model that defines its own
newkeeps it:TypeName.new(...)calls that method, and nonewis derived. - Constructing the model directly,
TypeName(...), is refused (INCAN-T0001), inside the model's own methods too.
@derive(Validate)
model EmailUser:
email: str
is_active: bool = true
def validate(self) -> Result[EmailUser, str]:
if "@" not in self.email:
return Err("invalid email")
return Ok(self)
def make_user(email: str) -> Result[EmailUser, str]:
return EmailUser.new(email=email) # accepted
def make_inactive(email: str) -> Result[EmailUser, str]:
return EmailUser.new(email=email, is_active=false) # refused: is_active declares a default (INCAN-T0001)
def make_raw(email: str) -> EmailUser:
return EmailUser(email=email) # refused: direct construction of a Validate model (INCAN-T0001)