-
Notifications
You must be signed in to change notification settings - Fork 56
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
1 parent
68b91cc
commit 7c2edd1
Showing
7 changed files
with
47 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ZodError, type z } from 'zod'; | ||
|
||
/** | ||
* Parse a value with error formatting. | ||
*/ | ||
export function parseWithSchema<T extends z.ZodType>( | ||
schema: T, | ||
value: unknown, | ||
): z.infer<T> { | ||
try { | ||
return schema.parse(value); | ||
} catch (err) { | ||
if (err instanceof ZodError && err.errors.length) { | ||
const [firstError] = err.errors; | ||
const { message, path } = firstError!; | ||
const fullPath = path.join('.'); | ||
|
||
throw new Error( | ||
`Property '${fullPath}' in config isn't valid.\n` + | ||
`⚠️ ${message}`, | ||
); | ||
} | ||
|
||
throw new Error('Failed to parse config.'); | ||
} | ||
} |