.agents/skills/flet-validation/SKILL.md
Use this skill when you:
before_update() checks to V rules,Raises sections tied to validation,Do not use this skill for unrelated doc-only edits outside control validation.
Do not use this skill for deprecation authoring conventions; use
flet-deprecation.
sdk/python/packages/flet/src/flet/utils/validation.pyvalidate(instance);
this skill focuses on the control-specific authoring conventions.from flet.utils.validation import V
and when needed
from flet.utils.validation import ValidationRulesUse this order and stop at the first option that keeps logic clear:
Annotated[..., V.*] field rules (default).__validation_rules__: ValidationRules for cross-field invariants that do
not map cleanly to one field.before_update() only for normalization/mutation, or for truly non-ruleable
invariants.Never duplicate the same invariant in more than one layer.
Prefer field-level validation with Annotated[...] metadata.
@dataclass
class Sample:
opacity: Annotated[
Optional[Number],
V.between(0.0, 1.0),
] = None
value: Annotated[
Optional[Number],
V.ge_field("min"),
V.le_field("max"),
] = None
min: Annotated[
Number,
V.le_field("max"),
V.le_field("value"),
] = 0.0
max: Annotated[
Number,
V.ge_field("min"),
V.ge_field("value"),
] = 1.0
Use class-level __validation_rules__ only for invariants that cannot be
expressed cleanly with field rules.
__validation_rules__: ValidationRules = (...)V.ensure(...) with an explicit message only when the predicate is
short and readable.V.ensure(lambda ...) becomes hard to read, prefer either:
V.ensure(...), orbefore_update() check when it cannot be represented well with
existing V.* rules.Keep the before_update() override for normalization/mutation first.
V rules.validate()), not
from ad-hoc before_update() checks, unless the invariant is genuinely
non-ruleable with current validation primitives.Cross-field comparisons use field rules only.
V.gt_field, V.ge_field, V.lt_field, V.le_fieldNone handling is inferred from type hints.
Optional[T]), None is allowed.None fails validation.*_field comparisons, if either side is optional and currently None, the
comparison is skipped; if a non-optional side is None, validation fails.Match Dart effective behavior.
packages/flet/lib/src/controls/<control>.dart or
extension wrapper),min/max).For new properties, validate against base widget assertions before wiring.
Annotated[...], __validation_rules__,
or readable before_update()) to prevent Dart assertions from being the first
failure point.Keep error ownership clear.
ValueError.V.* arguments) should raise
ValidationDeclarationError.Optional[T] over T | None,Union[...] when needed.When a property has validation, document it in that property’s docstring (google style).
Add a Raises: block under the property docstring.
Use one ValueError entry per logical rule, except between(...).
V.between(a, b), use one entry:
ValueError: If it is not between \a` and `b`, inclusive.`Start each entry with If it ..., where 'it' refers to the property name.
Use canonical wording from validation helper docstrings.
sdk/python/packages/flet/src/flet/utils/validation.py.V.* helper includes Property docstring Raises wording.Raises entries as negations of the annotation rule."strictly".
For example: V.gt(x) -> ValueError: If it is not strictly greater than \x`.`factor_of, multiple_of), add
explicit sign rules (V.gt(0) or V.lt(0)) when direction matters, and
include separate Raises entries for those sign rules.Mention conditional applicability when needed.
min/max checks against optional value):
Raises:
ValueError: If it is not less than or equal to [`value`][(c).],
when [`value`][(c).] is set.
Keep examples and wording aligned with real control files.
min, max, value,
start_value, end_value, min_lines, max_lines.min][(c).], [max][(c).].Follow the cross-reference guidance in:
docs-conventions.
Most common pattern to use in control property docstrings:
[\prop`][(c).]`Keep symbol labels wrapped in backticks.
When adding/changing validation, include tests that cover:
==, min/max edges) where applicable;Prefer placing tests in:
sdk/python/packages/flet/tests/test_validation.py for validation runtime;before_update() validations after migration.Raises entries on validated properties.ValueError sentence.V.ensure(lambda ...) expressions when before_update() would be cleaner.