-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new ValidationError system
- Loading branch information
1 parent
349b5ef
commit 28aa0f2
Showing
6 changed files
with
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import z from 'zod' | ||
import { ValidationError } from '../error/ValidationError' | ||
|
||
describe('Validation error', () => { | ||
test('basic', () => { | ||
const schema = z.object({ | ||
name: z.string(), | ||
age: z.number(), | ||
object: z.object({ | ||
foo: z.object({ | ||
bar: z.discriminatedUnion('type', [ | ||
z.object({ type: z.literal('a'), a: z.string() }), | ||
z.object({ type: z.literal('b'), b: z.number() }), | ||
]), | ||
}), | ||
}), | ||
}) | ||
|
||
const result = schema.safeParse({ | ||
object: { foo: { bar: { type: 'z', a: 123 } } }, | ||
}) | ||
|
||
const error = new ValidationError('Validation failed', result.error) | ||
expect(error.message).toMatchInlineSnapshot(` | ||
"[ValidationError] Validation failed | ||
- Required at "name" | ||
- Required at "age" | ||
- Invalid discriminator value. Expected 'a' | 'b' at "object.foo.bar.type"" | ||
`) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import type z from 'zod' | ||
import { fromError } from 'zod-validation-error' | ||
|
||
export class ValidationError<Schema extends z.ZodTypeAny = z.ZodTypeAny> extends Error { | ||
export class ValidationError extends Error { | ||
public constructor( | ||
message: string, | ||
public readonly error?: z.ZodError<Schema> | ||
public readonly error?: z.ZodError | ||
) { | ||
/** | ||
* TODO: Before Zod, we were using some flattening logic to make the error message more readable. | ||
* We may want to do the same thing here again. | ||
*/ | ||
const errorDetails = JSON.stringify(error, null, 2) | ||
super(`${message}\n${errorDetails}`) | ||
const errorDetails = fromError(error, { | ||
issueSeparator: '\n\t- ', | ||
prefix: `[ValidationError] ${message}`, | ||
prefixSeparator: '\n\t- ', | ||
}) | ||
|
||
super(errorDetails.toString(), { cause: error }) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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