-
Notifications
You must be signed in to change notification settings - Fork 301
feat(express): improve typed routes validation error messages #8178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,23 @@ | ||
| import { createRouter, WrappedRouter } from '@api-ts/typed-express-router'; | ||
|
|
||
| import { ExpressApi } from './api'; | ||
| import { createValidationError } from './utils'; | ||
|
|
||
| const { version: bitgoJsVersion } = require('bitgo/package.json'); | ||
| const { version: bitgoExpressVersion } = require('../../package.json'); | ||
|
|
||
| export default function (): WrappedRouter<ExpressApi> { | ||
| const router: WrappedRouter<ExpressApi> = createRouter(ExpressApi); | ||
| const router: WrappedRouter<ExpressApi> = createRouter(ExpressApi, { | ||
| decodeErrorFormatter: (errors) => { | ||
| const err = createValidationError(errors); | ||
| return { | ||
| error: err.message, | ||
| message: err.message, | ||
| name: err.name, | ||
| bitgoJsVersion, | ||
| bitgoExpressVersion, | ||
| }; | ||
| }, | ||
| }); | ||
| return router; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import * as t from 'io-ts'; | ||
|
|
||
| import { ValidationError } from '../errors'; | ||
|
|
||
| /** | ||
| * Formats io-ts validation errors into clear, human-readable messages. | ||
| */ | ||
| export function formatValidationErrors(errors: t.Errors): string { | ||
| const seen = new Set<string>(); | ||
| const messages: string[] = []; | ||
|
|
||
| for (const error of errors) { | ||
| // Build field path, filtering out internal keys | ||
| const path = error.context | ||
| .map((c) => c.key) | ||
| .filter((key) => key && !/^\d+$/.test(key) && key !== 'body') | ||
| .join('.'); | ||
|
|
||
| if (!path || seen.has(path)) continue; | ||
| seen.add(path); | ||
|
|
||
| const expected = error.context[error.context.length - 1]?.type.name; | ||
| if (expected === 'undefined') continue; | ||
|
|
||
| if (error.value === undefined) { | ||
| messages.push(`Missing required field '${path}'`); | ||
| } else { | ||
| const value = typeof error.value === 'object' ? JSON.stringify(error.value) : String(error.value); | ||
| messages.push(`Invalid value for '${path}': expected ${expected}, got '${value}'`); | ||
| } | ||
| } | ||
|
|
||
| return messages.join('. ') + (messages.length ? '.' : ''); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a ValidationError from io-ts validation errors. | ||
| */ | ||
| export function createValidationError(errors: t.Errors): ValidationError { | ||
| return new ValidationError(formatValidationErrors(errors)); | ||
| } |
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
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
76 changes: 76 additions & 0 deletions
76
modules/express/test/unit/typedRoutes/formatValidationErrors.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import * as assert from 'assert'; | ||
| import * as t from 'io-ts'; | ||
|
|
||
| import { formatValidationErrors } from '../../../src/typedRoutes/utils'; | ||
|
|
||
| describe('formatValidationErrors', function () { | ||
| it('should format missing required field', function () { | ||
| const errors: t.Errors = [{ value: undefined, context: [{ key: 'name', type: t.string }] }]; | ||
| assert.strictEqual(formatValidationErrors(errors), "Missing required field 'name'."); | ||
| }); | ||
|
|
||
| it('should format wrong type', function () { | ||
| const errors: t.Errors = [{ value: 123, context: [{ key: 'field', type: t.string }] }]; | ||
| assert.strictEqual(formatValidationErrors(errors), "Invalid value for 'field': expected string, got '123'."); | ||
| }); | ||
|
|
||
| it('should format nested paths', function () { | ||
| const errors: t.Errors = [ | ||
| { | ||
| value: 123, | ||
| context: [ | ||
| { key: 'memo', type: t.object }, | ||
| { key: 'type', type: t.string }, | ||
| ], | ||
| }, | ||
| ]; | ||
| assert.strictEqual(formatValidationErrors(errors), "Invalid value for 'memo.type': expected string, got '123'."); | ||
| }); | ||
|
|
||
| it('should filter numeric indices', function () { | ||
| const errors: t.Errors = [ | ||
| { | ||
| value: 'x', | ||
| context: [ | ||
| { key: 'recipients', type: t.array(t.unknown) }, | ||
| { key: '0', type: t.object }, | ||
| { key: 'amount', type: t.number }, | ||
| ], | ||
| }, | ||
| ]; | ||
| assert.strictEqual( | ||
| formatValidationErrors(errors), | ||
| "Invalid value for 'recipients.amount': expected number, got 'x'." | ||
| ); | ||
| }); | ||
|
|
||
| it('should filter body from path', function () { | ||
| const errors: t.Errors = [ | ||
| { | ||
| value: 123, | ||
| context: [ | ||
| { key: 'body', type: t.object }, | ||
| { key: 'name', type: t.string }, | ||
| ], | ||
| }, | ||
| ]; | ||
| assert.strictEqual(formatValidationErrors(errors), "Invalid value for 'name': expected string, got '123'."); | ||
| }); | ||
|
|
||
| it('should skip undefined type errors', function () { | ||
| const errors: t.Errors = [{ value: 123, context: [{ key: 'optional', type: t.undefined }] }]; | ||
| assert.strictEqual(formatValidationErrors(errors), ''); | ||
| }); | ||
|
|
||
| it('should return empty string for empty errors', function () { | ||
| assert.strictEqual(formatValidationErrors([]), ''); | ||
| }); | ||
|
|
||
| it('should deduplicate same path', function () { | ||
| const errors: t.Errors = [ | ||
| { value: {}, context: [{ key: 'value', type: t.string }] }, | ||
| { value: {}, context: [{ key: 'value', type: t.number }] }, | ||
| ]; | ||
| assert.strictEqual((formatValidationErrors(errors).match(/'value'/g) || []).length, 1); | ||
| }); | ||
| }); | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.