Skip to content

feat(validation): add In<T> and InList<T extends Enum> whitelist rules#82

Merged
anilcancakir merged 2 commits into
masterfrom
feat/in-inlist-rule
Apr 18, 2026
Merged

feat(validation): add In<T> and InList<T extends Enum> whitelist rules#82
anilcancakir merged 2 commits into
masterfrom
feat/in-inlist-rule

Conversation

@anilcancakir
Copy link
Copy Markdown
Contributor

Closes #81

Summary

Adds two Laravel-style whitelist rules to the validation system:

  • In<T>(List<T> values) — primitive whitelist. The generic T makes type mismatches fail explicitly instead of silently coercing.
  • InList<T extends Enum>(values, {caseInsensitive, wire}) — enum-aware variant. Accepts the enum instance itself or a string compared against Enum.name. Pass wire: to map enums onto snake_case or bespoke wire codes, caseInsensitive: true for loose string matching.

Both rules short-circuit on null (pair with Required() for presence) and emit the shared validation.in message with a comma-joined :values placeholder.

'visibility': [In<String>(['public', 'private'])],
'priority':   [In<int>([1, 2, 3, 5, 8])],
'severity':   [InList(Severity.values)],
'direction':  [InList(ThresholdDirection.values,
                wire: (d) => d == ThresholdDirection.highBad ? 'high_bad' : 'low_bad')],

Test plan

  • 21 new tests in test/validation/in_rule_test.dart covering both rules (type guards, null short-circuit, Validator integration, wire/caseInsensitive combos, params exposure)
  • flutter test — all 1019 tests green
  • dart analyze — zero warnings
  • dart format — clean
  • CHANGELOG [Unreleased] updated
  • doc/digging-deeper/validation.md — new Whitelist Rules section
  • skills/magic-framework/references/forms-validation.md — rule table + gotcha

Closes #81

Introduces two Laravel-style whitelist rules:

- `In<T>(List<T> values)` validates a primitive value against a typed
  whitelist. Type mismatches fail explicitly rather than silently coercing.
- `InList<T extends Enum>(values, {caseInsensitive, wire})` validates enum-backed
  fields, accepting the enum instance itself or a string compared against
  `Enum.name`. The optional `wire:` mapper swaps in snake_case or bespoke wire
  codes; `caseInsensitive:` relaxes string comparison.

Both short-circuit on `null` (pair with `Required()` for presence) and emit
`validation.in` with a comma-joined `:values` placeholder for consistent
error messages.

- CHANGELOG: [Unreleased] entry
- doc/digging-deeper/validation.md: new Whitelist Rules section
- skills/magic-framework/references/forms-validation.md: rule table + gotcha
Copilot AI review requested due to automatic review settings April 18, 2026 15:12
@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Laravel-style whitelist validation rules to Magic’s validation system, including a typed primitive whitelist and an enum-aware variant.

Changes:

  • Introduces new In<T> and InList<T extends Enum> validation rules with validation.in messaging + :values params.
  • Exports the new rules via lib/magic.dart and documents usage in framework docs/reference material.
  • Adds a dedicated test suite validating rule behavior and Validator integration.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
lib/src/validation/rules/in_rule.dart Implements In<T> and InList<T extends Enum> rules and their message params.
lib/magic.dart Exposes the new rule(s) through the public barrel export.
test/validation/in_rule_test.dart Adds end-to-end and unit tests for both whitelist rules.
doc/digging-deeper/validation.md Documents the new whitelist rules and provides examples.
skills/magic-framework/references/forms-validation.md Updates the built-in rules reference table and gotchas.
CHANGELOG.md Notes the new validation rules under Unreleased.

Comment thread lib/src/validation/rules/in_rule.dart Outdated
Comment thread lib/src/validation/rules/in_rule.dart Outdated
Comment thread lib/src/validation/rules/in_rule.dart Outdated
- Split `in_rule.dart` into `in.dart` and `in_list.dart` to match the
  one-rule-per-file convention used across `validation/rules/` (required.dart,
  email.dart, unique.dart, etc.).
- Wrap caller-provided whitelist in `List.unmodifiable()` inside both `In` and
  `InList` constructors — external mutation can no longer drift validation
  behavior or the `:values` params after the rule is built.
@anilcancakir anilcancakir merged commit cc03dc4 into master Apr 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Framework: In / InList<T extends Enum> validation rule

2 participants