-
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 #7 from enormora/string
Format invalid_string issues
- Loading branch information
Showing
5 changed files
with
212 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,22 @@ | ||
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 email validation', () => { | ||
const schema = z.string().email(); | ||
const result = safeParse(schema, 'foo'); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, ['invalid email']); | ||
}); | ||
|
||
test('formats messages for invalid string includes validation', () => { | ||
const schema = z.string().includes('foo', { position: 2 }); | ||
const result = safeParse(schema, 'foo'); | ||
|
||
assert.strictEqual(result.success, false); | ||
assert.deepStrictEqual(result.error.issues, [ | ||
'string must include "foo" at one ore more positions greater than or equal to 2' | ||
]); | ||
}); |
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
143 changes: 143 additions & 0 deletions
143
source/zod-error-formatter/issue-specific/invalid-string.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,143 @@ | ||
import { test } from '@sondr3/minitest'; | ||
import assert from 'node:assert'; | ||
import { formatInvalidStringIssueMessage } from './invalid-string.js'; | ||
|
||
test('formats the invalid string issue correctly when validation is "regex"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'regex' | ||
}); | ||
assert.strictEqual(message, 'string doesn’t match expected pattern'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "email"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'email' | ||
}); | ||
assert.strictEqual(message, 'invalid email'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "url"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'url' | ||
}); | ||
assert.strictEqual(message, 'invalid url'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "emoji"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'emoji' | ||
}); | ||
assert.strictEqual(message, 'invalid emoji'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "uuid"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'uuid' | ||
}); | ||
assert.strictEqual(message, 'invalid uuid'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "cuid"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'cuid' | ||
}); | ||
assert.strictEqual(message, 'invalid cuid'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "cuid2"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'cuid2' | ||
}); | ||
assert.strictEqual(message, 'invalid cuid2'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "ulid"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'ulid' | ||
}); | ||
assert.strictEqual(message, 'invalid ulid'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "datetime"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'datetime' | ||
}); | ||
assert.strictEqual(message, 'invalid datetime'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is "ip"', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: 'ip' | ||
}); | ||
assert.strictEqual(message, 'invalid ip'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is requires an includes term without position', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: { includes: 'foo' } | ||
}); | ||
assert.strictEqual(message, 'string must include "foo"'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is requires an includes term with position', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: { includes: 'foo', position: 42 } | ||
}); | ||
assert.strictEqual(message, 'string must include "foo" at one ore more positions greater than or equal to 42'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is requires a starts-with term', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: { startsWith: 'foo' } | ||
}); | ||
assert.strictEqual(message, 'string must start with "foo"'); | ||
}); | ||
|
||
test('formats the invalid string issue correctly when validation is requires a ends-with term', () => { | ||
const message = formatInvalidStringIssueMessage({ | ||
code: 'invalid_string', | ||
path: [], | ||
message: '', | ||
validation: { endsWith: 'foo' } | ||
}); | ||
assert.strictEqual(message, 'string must end with "foo"'); | ||
}); |
34 changes: 34 additions & 0 deletions
34
source/zod-error-formatter/issue-specific/invalid-string.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,34 @@ | ||
import type { ZodInvalidStringIssue } from 'zod'; | ||
|
||
function hasProperty<ObjectType extends Record<string, unknown>, Key extends string>( | ||
object: ObjectType, | ||
key: Key | ||
): object is Extract<ObjectType, Record<Key, unknown>> { | ||
return Object.hasOwn(object, key); | ||
} | ||
|
||
function formatIncludesValidation(includes: string, position?: number): string { | ||
if (position !== undefined) { | ||
return `string must include "${includes}" at one ore more positions greater than or equal to ${position}`; | ||
} | ||
|
||
return `string must include "${includes}"`; | ||
} | ||
|
||
export function formatInvalidStringIssueMessage(issue: ZodInvalidStringIssue): string { | ||
const { validation } = issue; | ||
|
||
if (validation === 'regex') { | ||
return 'string doesn’t match expected pattern'; | ||
} | ||
if (typeof validation === 'string') { | ||
return `invalid ${validation}`; | ||
} | ||
if (hasProperty(validation, 'includes')) { | ||
return formatIncludesValidation(validation.includes, validation.position); | ||
} | ||
if (hasProperty(validation, 'startsWith')) { | ||
return `string must start with "${validation.startsWith}"`; | ||
} | ||
return `string must end with "${validation.endsWith}"`; | ||
} |