feat: validate collection and parsable query/header parameters#80
Open
graham-a3 wants to merge 3 commits into
Open
feat: validate collection and parsable query/header parameters#80graham-a3 wants to merge 3 commits into
graham-a3 wants to merge 3 commits into
Conversation
Header/query validation previously rejected any collection-typed parameter (string[], int[], ...) with a 400, and could not validate types outside a fixed scalar set. Two changes close both gaps: - HeaderOrQuery reads the full StringValues for collection parameters and validates each element, applying any ValidationAttribute to the materialised collection (matching DataAnnotations semantics, so [MinLength]/[MaxLength]/[Length] constrain item count). - Utils.TryCastValue falls back to a parse convention (enum, IParsable<T>, or a static TryParse(string, out T)) for types not in the scalar set, so any type the framework can bind is also validated. Includes support for string-backed "smart enum" types. Adds integration tests covering collections (query + header), closed/open smart enums, and a regression guard for un-attributed parameters.
Add a README section and an example endpoint covering repeated-key collection binding, collection-level validation attributes, and enum / parsable-type validation.
Scaffold a concise agent guide covering build/test commands, project layout, the two validation paths (middleware vs custom binders), and repo conventions.
Summary - Unit Tests
Summary - Test CoverageSummary
CoverageA3.MinimalApiValidation - 87%
|
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.
Summary
Header/query validation previously returned 400 for any collection-typed parameter (
string[],int[], …) and couldn't validate types outside a fixed scalar set. This adds both.What changed
[FromQuery]/[FromHeader]parameters likestring[]/int[]now bind from repeated keys (?tag=a&tag=b) and are validated per element. Validation attributes apply to the collection itself (matchingSystem.ComponentModel.DataAnnotations), so[MinLength]/[MaxLength]/[Length]constrain item count. Per-element rules remain the job ofFromQuery<T>+RuleForEach.Utils.TryCastValuenow falls back to a parse convention (enum→IParsable<T>→ staticTryParse(string, out T)) for types not in the scalar set, so any type the framework can bind is also validated. This covers regular enums and string-backed "smart enum" types.Behaviour notes
StringValuesbind for a bare[FromQuery];List<T>/IEnumerable<T>collection properties go through theFromQuery<T>model binder (unchanged).Testing
New integration tests across the full middleware pipeline for collections (query + header), closed/open smart enums (scalar + array), a real enum, and an un-attributed-parameter guard.
Full suite green: 1592 passed, 0 failed.