mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
chore: typos Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
57 lines
1.2 KiB
TypeScript
57 lines
1.2 KiB
TypeScript
import type { z } from 'zod';
|
|
import { fromZodError } from 'zod-validation-error';
|
|
|
|
let validationEnabled = true;
|
|
|
|
/**
|
|
* Enables validators.
|
|
*
|
|
* @returns Whether validation is occurring.
|
|
*/
|
|
export function enableValidators() {
|
|
return (validationEnabled = true);
|
|
}
|
|
|
|
/**
|
|
* Disables validators.
|
|
*
|
|
* @returns Whether validation is occurring.
|
|
*/
|
|
export function disableValidators() {
|
|
return (validationEnabled = false);
|
|
}
|
|
|
|
/**
|
|
* Checks whether validation is occurring.
|
|
*/
|
|
export function isValidationEnabled() {
|
|
return validationEnabled;
|
|
}
|
|
|
|
/**
|
|
* Parses a value with a given validator, accounting for whether validation is enabled.
|
|
*
|
|
* @param validator - The zod validator to use
|
|
* @param value - The value to parse
|
|
* @param validationOverride - Force validation to run/not run regardless of your global preference
|
|
* @returns The result from parsing
|
|
* @internal
|
|
*/
|
|
export function validate<Validator extends z.ZodTypeAny>(
|
|
validator: Validator,
|
|
value: unknown,
|
|
validationOverride?: boolean,
|
|
): z.output<Validator> {
|
|
if (validationOverride === false || !isValidationEnabled()) {
|
|
return value;
|
|
}
|
|
|
|
const result = validator.safeParse(value);
|
|
|
|
if (!result.success) {
|
|
throw fromZodError(result.error);
|
|
}
|
|
|
|
return result.data;
|
|
}
|