mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
feat!: use zod v4 (#10922)
* feat: zod 4 * feat: zod v3, but v4 feat: validation error class Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com> * chore: bump --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { z } from 'zod';
|
||||
import { fromZodError } from 'zod-validation-error';
|
||||
import type { z } from 'zod/v4';
|
||||
import { ValidationError } from './ValidationError.js';
|
||||
|
||||
let validationEnabled = true;
|
||||
|
||||
@@ -35,21 +35,23 @@ export function isValidationEnabled() {
|
||||
* @param value - The value to parse
|
||||
* @param validationOverride - Force validation to run/not run regardless of your global preference
|
||||
* @returns The result from parsing
|
||||
* @throws {@link ValidationError}
|
||||
* Throws if the value does not pass validation, if enabled.
|
||||
* @internal
|
||||
*/
|
||||
export function validate<Validator extends z.ZodTypeAny>(
|
||||
export function validate<Validator extends z.ZodType>(
|
||||
validator: Validator,
|
||||
value: unknown,
|
||||
validationOverride?: boolean,
|
||||
): z.output<Validator> {
|
||||
if (validationOverride === false || !isValidationEnabled()) {
|
||||
return value;
|
||||
if (validationOverride === false || (validationOverride === undefined && !isValidationEnabled())) {
|
||||
return value as z.output<Validator>;
|
||||
}
|
||||
|
||||
const result = validator.safeParse(value);
|
||||
|
||||
if (!result.success) {
|
||||
throw fromZodError(result.error);
|
||||
throw new ValidationError(result.error);
|
||||
}
|
||||
|
||||
return result.data;
|
||||
|
||||
Reference in New Issue
Block a user