-
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 #10 from enormora/simple
Format issues with simple static messages
- Loading branch information
Showing
4 changed files
with
136 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 keys in map schemas correctly', () => { | ||
const schema = z.map(z.literal('a'), z.string()); | ||
const result = safeParse(schema, new Map([['b', 'foo']])); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'at [0].key: invalid literal: expected "a", but got string' | ||
]); | ||
}); | ||
|
||
test('formats messages for invalid values in map schemas correctly', () => { | ||
const schema = z.map(z.literal('a'), z.string()); | ||
const result = safeParse(schema, new Map([['a', 0]])); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'at [0].value: expected string, but got number' | ||
]); | ||
}); |
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,34 @@ | ||
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 items in tuple schemas correctly', () => { | ||
const schema = z.tuple([z.literal('a')]); | ||
const result = safeParse(schema, ['b']); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'at [0]: invalid literal: expected "a", but got string' | ||
]); | ||
}); | ||
|
||
test('formats messages for invalid rest values in tuple schemas correctly', () => { | ||
const schema = z.tuple([z.literal('a')]).rest(z.number()); | ||
const result = safeParse(schema, ['a', true]); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'at [1]: expected number, but got boolean' | ||
]); | ||
}); | ||
|
||
test('formats messages for extra items in fixed tuple schemas correctly', () => { | ||
const schema = z.tuple([z.literal('a')]); | ||
const result = safeParse(schema, ['a', 'b']); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'array must contain at most 1 element' | ||
]); | ||
}); |
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