Skip to content

Add support for patternProperties - #1105

Open
ChrisJr404 wants to merge 1 commit into
danielgtaylor:mainfrom
ChrisJr404:feat/pattern-properties
Open

Add support for patternProperties#1105
ChrisJr404 wants to merge 1 commit into
danielgtaylor:mainfrom
ChrisJr404:feat/pattern-properties

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #1102.

Adds a PatternProperties field to huma.Schema so object schemas can validate properties whose names match a regular expression, as described by the JSON Schema / OpenAPI 3.1 patternProperties keyword. This lets you express things like "any key matching ^str_[a-z]+$ must be a string" that weren't previously possible.

Behaviour

  • PatternProperties map[string]*Schema marshals into the generated OpenAPI document as patternProperties.
  • Patterns are compiled once in PrecomputeMessages (stored next to the existing pattern regex) and iterated in sorted order, so validation stays allocation-free on the happy path and error ordering is deterministic.
  • During validation, each instance property is checked against every patternProperties schema whose regex matches its name (both the map[string]any and map[any]any paths).
  • A property matched by a pattern is no longer treated as an additional property, so it's exempt from both additionalProperties: false and the additionalProperties: <schema> cases — matching the spec.
  • autopatch carries the field through to generated PATCH schemas.

Tests

  • TestSchemaPatternProperties covers marshaling.
  • Eight TestValidate cases cover value type/constraint failures, the additionalProperties interaction (bool and schema forms), and the map[any]any path.

Kept the scope small per the note in #1040 about smaller PRs being easier to review. Happy to adjust the field naming or the additionalProperties interaction if you'd prefer a different approach.

Adds a PatternProperties field to huma.Schema so object schemas can validate
properties whose names match a regular expression, per the JSON Schema /
OpenAPI 3.1 patternProperties keyword.

The field is serialized into the generated OpenAPI document and compiled
patterns are precomputed alongside the existing pattern regex so validation
avoids per-request allocations. During validation each instance property is
checked against every matching pattern schema, and properties matched by a
pattern are exempt from additionalProperties handling. autopatch carries the
field through to generated PATCH schemas.

Closes danielgtaylor#1102
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.

Add support for patternProperties

1 participant