Skip to content

docs: clarify multipart array validation#627

Open
barry166 wants to merge 1 commit into
fastify:mainfrom
barry166:docs/issue-318-array-field-validation
Open

docs: clarify multipart array validation#627
barry166 wants to merge 1 commit into
fastify:mainfrom
barry166:docs/issue-318-array-field-validation

Conversation

@barry166

Copy link
Copy Markdown

Summary

  • Document that multipart fields are converted before JSON Schema validation.
  • Clarify that repeated field names become arrays, while a single field remains one field object.
  • Show an anyOf schema for accepting either a single shared-schema field or repeated fields.

Fixes #318

Verification

  • npm test
  • npm run test:typescript
  • git diff --check

Notes

AI-assisted: I used Codex to inspect the issue/repository, prepare this docs-only change, and run verification. I manually reviewed the diff and test output before opening the PR.

The docs currently show array validation with shared multipart schemas but do not explain that a single submitted field remains one field object. Adding that note helps users avoid expecting AJV coerceTypes to wrap multipart field objects into arrays.

Constraint: Issue fastify#318 was labeled documentation/good first issue and maintainer guidance asked for a docs update.
Rejected: Change parser behavior based on route schema | this would require schema introspection and could alter existing multipart body shapes.
Confidence: high
Scope-risk: narrow
Directive: Do not change field grouping semantics from docs-only issues without adding explicit behavior tests.
Tested: npm test
Tested: npm run test:typescript
Tested: git diff --check
Not-tested: rendered README preview
@Tony133
Tony133 requested a review from a team July 16, 2026 10:39
@Fdawgs
Fdawgs requested a review from Copilot July 16, 2026 14:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This docs-only PR clarifies how @fastify/multipart’s attachFieldsToBody output is shaped before JSON Schema validation, and how repeated multipart field names become arrays while a single occurrence stays a single field object—addressing confusion seen in issue #318 around Ajv coerceTypes: 'array'.

Changes:

  • Document that multipart field conversion happens before validation.
  • Clarify the “single field object vs repeated field array” behavior for repeated field names.
  • Provide an anyOf schema pattern for accepting either a single field object or an array of field objects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread README.md

If provided, the `sharedSchemaId` parameter must be a string ID and a shared schema will be added to your fastify instance so you will be able to apply the validation to your service (like in the example mentioned above).

Multipart fields are converted before validation. Repeated field names are grouped into arrays, but a field sent once remains a single field object. This means AJV `coerceTypes: 'array'` will not wrap a single multipart field object in an array for a schema like `myFiles: { type: 'array', items: fastify.getSchema('#mySharedSchema') }`. If a field accepts one or more values, validate both the single-value and repeated-value shapes, then normalize the value in your handler if needed:
Comment thread README.md
Comment on lines +379 to +383
myFiles: {
anyOf: [
{ $ref: '#mySharedSchema' },
{ type: 'array', items: fastify.getSchema('#mySharedSchema') }
]
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.

The coerceTypes: 'array' doesn't coerce one parameter to a single element in array

2 participants