-
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.
- Loading branch information
Showing
5 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { test } from '@sondr3/minitest'; | ||
import assert from 'node:assert'; | ||
import { z } from 'zod'; | ||
import { safeParse } from '../../source/zod-error-formatter/formatter.js'; | ||
|
||
test('formats messages for invalid string literals correctly', () => { | ||
const schema = z.literal('foo'); | ||
const result = safeParse(schema, 'bar'); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'invalid literal: expected "foo", but got string' | ||
]); | ||
}); |
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
47 changes: 47 additions & 0 deletions
47
source/zod-error-formatter/issue-specific/invalid-literal.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,47 @@ | ||
import { test } from '@sondr3/minitest'; | ||
import assert from 'node:assert'; | ||
import { formatInvalidLiteralIssueMessage } from './invalid-literal.js'; | ||
|
||
test('formats the issue by using the expected value as is and only the type of the received value', () => { | ||
const message = formatInvalidLiteralIssueMessage({ | ||
code: 'invalid_literal', | ||
path: [], | ||
message: '', | ||
expected: 42, | ||
received: { foo: 'bar' } | ||
}); | ||
assert.strictEqual(message, 'invalid literal: expected 42, but got object'); | ||
}); | ||
|
||
test('wraps the expected value in double quotes when it is a string', () => { | ||
const message = formatInvalidLiteralIssueMessage({ | ||
code: 'invalid_literal', | ||
path: [], | ||
message: '', | ||
expected: 'foo', | ||
received: null | ||
}); | ||
assert.strictEqual(message, 'invalid literal: expected "foo", but got null'); | ||
}); | ||
|
||
test('correctly works with undefined as expected value', () => { | ||
const message = formatInvalidLiteralIssueMessage({ | ||
code: 'invalid_literal', | ||
path: [], | ||
message: '', | ||
expected: undefined, | ||
received: null | ||
}); | ||
assert.strictEqual(message, 'invalid literal: expected undefined, but got null'); | ||
}); | ||
|
||
test('correctly works with bigint as expected value', () => { | ||
const message = formatInvalidLiteralIssueMessage({ | ||
code: 'invalid_literal', | ||
path: [], | ||
message: '', | ||
expected: 9_007_199_254_740_993n, | ||
received: null | ||
}); | ||
assert.strictEqual(message, 'invalid literal: expected 9007199254740993, but got null'); | ||
}); |
14 changes: 14 additions & 0 deletions
14
source/zod-error-formatter/issue-specific/invalid-literal.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,14 @@ | ||
import { getParsedType, type Primitive, type ZodInvalidLiteralIssue } from 'zod'; | ||
|
||
function formatPrimitiveValue(value: Primitive): string { | ||
if (typeof value === 'bigint') { | ||
return value.toString(); | ||
} | ||
return JSON.stringify(value); | ||
} | ||
|
||
export function formatInvalidLiteralIssueMessage(issue: ZodInvalidLiteralIssue): string { | ||
const received = getParsedType(issue.received); | ||
const expected = formatPrimitiveValue(issue.expected as Primitive); | ||
return `invalid literal: expected ${expected}, but got ${received}`; | ||
} |