-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from enormora/unrec
Format unrecognized-keys issues
- Loading branch information
Showing
8 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
53 changes: 53 additions & 0 deletions
53
source/zod-error-formatter/issue-specific/unrecognized-keys.test.ts
This file contains 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,53 @@ | ||
import { test } from '@sondr3/minitest'; | ||
import assert from 'node:assert'; | ||
import { formatUnrecognizedKeysIssueMessage } from './unrecognized-keys.js'; | ||
|
||
test('formats a message which says the property is unknown when the given keys is an empty array', () => { | ||
const message = formatUnrecognizedKeysIssueMessage({ | ||
code: 'unrecognized_keys', | ||
path: [], | ||
message: '', | ||
keys: [] | ||
}); | ||
assert.strictEqual(message, 'unexpected additional property: unknown'); | ||
}); | ||
|
||
test('properly escapes the keys so it doesn’t get confused with unknown when the key is unknown', () => { | ||
const message = formatUnrecognizedKeysIssueMessage({ | ||
code: 'unrecognized_keys', | ||
path: [], | ||
message: '', | ||
keys: ['unknown'] | ||
}); | ||
assert.strictEqual(message, 'unexpected additional property: "unknown"'); | ||
}); | ||
|
||
test('properly escapes the keys in case they contain special characters', () => { | ||
const message = formatUnrecognizedKeysIssueMessage({ | ||
code: 'unrecognized_keys', | ||
path: [], | ||
message: '', | ||
keys: ['foo"bar'] | ||
}); | ||
assert.strictEqual(message, 'unexpected additional property: "foo\\"bar"'); | ||
}); | ||
|
||
test('correctly formats two properties using "and" as a separator', () => { | ||
const message = formatUnrecognizedKeysIssueMessage({ | ||
code: 'unrecognized_keys', | ||
path: [], | ||
message: '', | ||
keys: ['foo', 'bar'] | ||
}); | ||
assert.strictEqual(message, 'unexpected additional properties: "foo" and "bar"'); | ||
}); | ||
|
||
test('correctly formats three properties using comma as separator and "and" for the last item', () => { | ||
const message = formatUnrecognizedKeysIssueMessage({ | ||
code: 'unrecognized_keys', | ||
path: [], | ||
message: '', | ||
keys: ['foo', 'bar', 'baz'] | ||
}); | ||
assert.strictEqual(message, 'unexpected additional properties: "foo", "bar" and "baz"'); | ||
}); |
32 changes: 32 additions & 0 deletions
32
source/zod-error-formatter/issue-specific/unrecognized-keys.ts
This file contains 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,32 @@ | ||
import type { ZodUnrecognizedKeysIssue } from 'zod'; | ||
import { isNonEmptyArray, type NonEmptyArray } from '../non-empty-array.js'; | ||
|
||
function joinList(values: NonEmptyArray<string>, separator: string, lastItemSeparator: string): string { | ||
const initialList = Array.from(values); | ||
const lastItem = initialList.pop() as string; | ||
|
||
if (initialList.length === 0) { | ||
return lastItem; | ||
} | ||
|
||
const joinedInitialList = initialList.join(separator); | ||
return `${joinedInitialList}${lastItemSeparator}${lastItem}`; | ||
} | ||
|
||
function stringify(value: unknown): string { | ||
return JSON.stringify(value); | ||
} | ||
|
||
function formatList(values: readonly string[]): string { | ||
const escapedValues = values.map(stringify); | ||
if (!isNonEmptyArray(escapedValues)) { | ||
return 'unknown'; | ||
} | ||
return joinList(escapedValues, ', ', ' and '); | ||
} | ||
|
||
export function formatUnrecognizedKeysIssueMessage(issue: ZodUnrecognizedKeysIssue): string { | ||
const formattedProperties = formatList(issue.keys); | ||
const label = issue.keys.length > 1 ? 'properties' : 'property'; | ||
return `unexpected additional ${label}: ${formattedProperties}`; | ||
} |
This file contains 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,5 @@ | ||
export type NonEmptyArray<Item> = readonly [Item, ...(readonly Item[])]; | ||
|
||
export function isNonEmptyArray<Item>(list: readonly Item[]): list is NonEmptyArray<Item> { | ||
return list.length > 0; | ||
} |
This file contains 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