Flet validation

From flet-dev/flet · View interactive version

Flet validation is an AI agent skill from the GitHub repository flet-dev/flet. Use whenever editing validation for Python controls in sdk/python/packages/: adding/changing constrained properties, `Raises: ValueError` docstrings, `before_update()` checks, `raise ValueError`, Annotated/V rules, or __validation_rules__.

How to use this skill

Skill file

When To Use

Use this skill when you:

Do not use this skill for unrelated doc-only edits outside control validation.

Do not use this skill for deprecation authoring conventions; use

CODE0.

Source Of Truth

sdk/python/packages/flet/src/flet/utils/validation.py

this skill focuses on the control-specific authoring conventions.

from flet.utils.validation import V

and when needed

from flet.utils.validation import ValidationRules

Validation Decision Order

Use this order and stop at the first option that keeps logic clear:

  1. Annotated[..., V.*] field rules (default).
  2. __validation_rules__: ValidationRules for cross-field invariants that do

not map cleanly to one field.

  1. before_update() only for normalization/mutation, or for truly non-ruleable

invariants.

Never duplicate the same invariant in more than one layer.

Validation Authoring Rules

  1. 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

```

  1. Use class-level __validation_rules__ only for invariants that cannot be

expressed cleanly with field rules.

__validation_rules__: ValidationRules = (...)

short and readable.

1. a named predicate function passed to V.ensure(...), or

2. a clear before_update() check when it cannot be represented well with

existing V.* rules.

  1. Keep the before_update() override for normalization/mutation first.

from ad-hoc before_update() checks, unless the invariant is genuinely

non-ruleable with current validation primitives.

  1. Cross-field comparisons use field rules only.
  1. None handling is inferred from type hints.

comparison is skipped; if a non-optional side is None, validation fails.

  1. Match Dart effective behavior.

1. Flet Dart wrapper (packages/flet/lib/src/controls/<control>.dart or

extension wrapper),

2. wrapped Flutter widget source assertions/invariants.

to Dart.

min/max).

bounds/rounding/division logic in wrapper formatting code).

  1. For new properties, validate against base widget assertions before wiring.

Dart wrapper and underlying widget both enforce constraints for that property.

or readable before_update()) to prevent Dart assertions from being the first

failure point.

  1. Keep error ownership clear.

ValidationDeclarationError.

Typing Style

Property Docstring Style

When a property has validation, document it in that property’s docstring (google style).

  1. Add a Raises: block under the property docstring.
  1. Use one ValueError entry per logical rule, except between(...).

ValueError: If it is not between \a\ and \b\, inclusive.

  1. Start each entry with If it ..., where 'it' refers to the property name.
  1. Use canonical wording from validation helper docstrings.

sdk/python/packages/flet/src/flet/utils/validation.py.

For example: V.gt(x) -> ValueError: If it is not strictly greater than \x\.

explicit sign rules (V.gt(0) or V.lt(0)) when direction matters, and

include separate Raises entries for those sign rules.

  1. Mention conditional applicability when needed.

```

Raises:

ValueError: If it is not less than or equal to [value][(c).],

when [value][(c).] is set.

```

  1. Keep examples and wording aligned with real control files.

start_value, end_value, min_lines, max_lines.

Cross-Referencing Conventions

Follow the cross-reference guidance in:

CODE0.

Most common pattern to use in control property docstrings:

Keep symbol labels wrapped in backticks.

Required Test Matrix

When adding/changing validation, include tests that cover:

omitted.

Prefer placing tests in:

Common Pitfalls

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.