craft_parts.constraints module

Pydantic constraints for various items.

craft_parts.constraints.get_validator_by_regex(regex, error_msg)

Get a string validator by regular expression with a known error message.

This allows providing better error messages for regex-based validation than the standard message provided by pydantic. Simply place the result of this function in a BeforeValidator attached to your annotated type.

Parameters:
  • regex (Pattern[str]) – a compiled regular expression on a string.

  • error_msg (str) – The error message to raise if the value is invalid.

Return type:

Callable[[str], str]

Returns:

A validator function ready to be used by pydantic.BeforeValidator