mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +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:
21
packages/builders/src/util/ValidationError.ts
Normal file
21
packages/builders/src/util/ValidationError.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
/**
|
||||
* An error that is thrown when validation fails.
|
||||
*/
|
||||
export class ValidationError extends Error {
|
||||
/**
|
||||
* The underlying cause of the validation error.
|
||||
*/
|
||||
public override readonly cause: z.ZodError;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public constructor(error: z.ZodError) {
|
||||
super(z.prettifyError(error));
|
||||
|
||||
this.name = 'ValidationError';
|
||||
this.cause = error;
|
||||
}
|
||||
}
|
||||
@@ -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