feat(validation): add In<T> and InList<T extends Enum> whitelist rules#82
Merged
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
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>andInList<T extends Enum>validation rules withvalidation.inmessaging +:valuesparams. - Exports the new rules via
lib/magic.dartand 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. |
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #81
Summary
Adds two Laravel-style whitelist rules to the validation system:
In<T>(List<T> values)— primitive whitelist. The genericTmakes 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 againstEnum.name. Passwire:to map enums onto snake_case or bespoke wire codes,caseInsensitive: truefor loose string matching.Both rules short-circuit on
null(pair withRequired()for presence) and emit the sharedvalidation.inmessage with a comma-joined:valuesplaceholder.Test plan
test/validation/in_rule_test.dartcovering both rules (type guards, null short-circuit, Validator integration, wire/caseInsensitive combos, params exposure)flutter test— all 1019 tests greendart analyze— zero warningsdart format— clean[Unreleased]updateddoc/digging-deeper/validation.md— new Whitelist Rules sectionskills/magic-framework/references/forms-validation.md— rule table + gotcha