Validation
ValidationPipeline
- class formatparse.ValidationPipeline[source]
Ordered registry of per-field validators and whole-result hooks (issue #11).
Build with
add_validator()and/oradd_hook(), thenapply()on aParseResult. Per-field keys followapply_validators()(strfor named fields,intforfixedindices). If the same field is registered twice, the last registration wins. Hooks run in registration order after the per-field validator pass completes (on success, or inlenientmode after every field validator has been attempted).Async validators and inline
{...:validator(...)}syntax remain deferred (see issue #11).- add_hook(fn)[source]
Register a whole-result hook; runs after per-field validators. Chainable.
- Return type:
- apply(result, *, mode='strict')[source]
Run per-field validators, then registered hooks.
If
resultisNone, returnsNoneimmediately (no validators or hooks).Hooks receive the full
ParseResultand use the same raise-based contract asapply_validators(). Failures areValidationError(fieldpreserved when the raised error had one; otherwiseNonefor generic hook failures). Other exceptions becomeValidationErrorwithfield=None.modematchesapply_validators():strictstops on the first failure (field or hook).collectruns all per-field validators and all hooks, then raises a singleMultipleValidationErrorslisting field failures first (same key order asapply_validators()), then hook failures in hook registration order.lenientruns all field validators (warning on each failure), then all hooks (warning on each failure), and returnsresultwithout raising validation exceptions.- Return type:
apply_validators
- formatparse.apply_validators(result, validators, *, mode='strict')[source]
Run post-parse validators on
named/fixedvalues.Validators are raise-based: on success the callable returns (typically
None). On failure raiseValidationError(recommended) or any exception (wrapped inValidationError). Replacing values infixedslots is not supported; use named fields or mutateresult.namedyourself after validation if you need coercion.- Parameters:
result (
Optional[ParseResult]) – Output ofparse()/FormatParser.parse(), orNone.validators (
Optional[Mapping[Union[str,int],Callable[...,Any]]]) – Map from field key to validator. Keys arestrfield names forParseResult.namedorintindices forParseResult.fixed.mode (
Literal['strict','collect','lenient']) –"strict"— stop on first error."collect"— run all validators, then raiseMultipleValidationErrorsif any failed."lenient"— run all validators, emitValidationWarningfor each failure, and always returnresult.
- Return type:
- Returns:
The same
resultreference after validation (including lenient runs with failures).- Raises:
ValidationError – In
strictmode when a validator fails.MultipleValidationErrors – In
collectmode when any validator fails.
validator
- formatparse.validator(func)[source]
Mark a function as a post-parse validator (metadata for tooling / docs).
Validators are invoked with the parsed value for one field; they should raise
ValidationErroron failure or return normally on success.
Built-in validators
- formatparse.in_range(min_value=None, max_value=None)[source]
Return a validator that accepts numeric
valuewithin[min_value, max_value].
- formatparse.non_empty_str(value)[source]
Reject
None, non-strings, or blank/whitespace-only strings.- Return type:
- formatparse.is_valid_email(value)[source]
Reject values that are not plausible
user@domainmailbox strings.Uses a simple ASCII pattern suitable for post-parse validation only. It does not implement full RFC 5322 (no quoted-string local parts, no comments) and is not a deliverability or security check.
- Raises:
ValidationError – If
valueis not a non-empty string matching the pattern.- Return type:
- formatparse.is_valid_url(value)[source]
Reject values that are not
httporhttpsURLs with a non-empty host.Uses
urllib.parse.urlparse(). This does not verify reachability, TLS, or that the resource exists.- Raises:
ValidationError – If the string is empty, the scheme is not
http/https, or the parsed URL has no network location (host).- Return type: