mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
feat: implement zod-validation-error (#10534)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { ApplicationCommandType, type RESTPostAPIChatInputApplicationCommandsJSONBody } from 'discord-api-types/v10';
|
||||
import { Mixin } from 'ts-mixer';
|
||||
import { isValidationEnabled } from '../../../util/validation.js';
|
||||
import { validate } from '../../../util/validation.js';
|
||||
import { CommandBuilder } from '../Command.js';
|
||||
import { SharedNameAndDescription } from '../SharedNameAndDescription.js';
|
||||
import { chatInputCommandPredicate } from './Assertions.js';
|
||||
@@ -28,9 +28,7 @@ export class ChatInputCommandBuilder extends Mixin(
|
||||
options: options?.map((option) => option.toJSON(validationOverride)),
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
chatInputCommandPredicate.parse(data);
|
||||
}
|
||||
validate(chatInputCommandPredicate, data, validationOverride);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { ApplicationCommandOptionType } from 'discord-api-types/v10';
|
||||
import { Mixin } from 'ts-mixer';
|
||||
import { normalizeArray, type RestOrArray } from '../../../util/normalizeArray.js';
|
||||
import { resolveBuilder } from '../../../util/resolveBuilder.js';
|
||||
import { isValidationEnabled } from '../../../util/validation.js';
|
||||
import { validate } from '../../../util/validation.js';
|
||||
import type { SharedNameAndDescriptionData } from '../SharedNameAndDescription.js';
|
||||
import { SharedNameAndDescription } from '../SharedNameAndDescription.js';
|
||||
import { chatInputCommandSubcommandGroupPredicate, chatInputCommandSubcommandPredicate } from './Assertions.js';
|
||||
@@ -69,9 +69,7 @@ export class ChatInputCommandSubcommandGroupBuilder
|
||||
options: options?.map((option) => option.toJSON(validationOverride)) ?? [],
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
chatInputCommandSubcommandGroupPredicate.parse(data);
|
||||
}
|
||||
validate(chatInputCommandSubcommandGroupPredicate, data, validationOverride);
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -102,9 +100,7 @@ export class ChatInputCommandSubcommandBuilder
|
||||
options: options?.map((option) => option.toJSON(validationOverride)) ?? [],
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
chatInputCommandSubcommandPredicate.parse(data);
|
||||
}
|
||||
validate(chatInputCommandSubcommandPredicate, data, validationOverride);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
ApplicationCommandOptionType,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { z } from 'zod';
|
||||
import { isValidationEnabled } from '../../../../util/validation.js';
|
||||
import { validate } from '../../../../util/validation.js';
|
||||
import type { SharedNameAndDescriptionData } from '../../SharedNameAndDescription.js';
|
||||
import { SharedNameAndDescription } from '../../SharedNameAndDescription.js';
|
||||
import { basicOptionPredicate } from '../Assertions.js';
|
||||
@@ -49,10 +49,7 @@ export abstract class ApplicationCommandOptionBase
|
||||
*/
|
||||
public toJSON(validationOverride?: boolean): APIApplicationCommandBasicOption {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
(this.constructor as typeof ApplicationCommandOptionBase).predicate.parse(clone);
|
||||
}
|
||||
validate((this.constructor as typeof ApplicationCommandOptionBase).predicate, clone, validationOverride);
|
||||
|
||||
return clone as APIApplicationCommandBasicOption;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApplicationCommandType, type RESTPostAPIContextMenuApplicationCommandsJSONBody } from 'discord-api-types/v10';
|
||||
import { isValidationEnabled } from '../../../util/validation.js';
|
||||
import { validate } from '../../../util/validation.js';
|
||||
import { messageCommandPredicate } from './Assertions.js';
|
||||
import { ContextMenuCommandBuilder } from './ContextMenuCommand.js';
|
||||
|
||||
@@ -9,10 +9,7 @@ export class MessageContextCommandBuilder extends ContextMenuCommandBuilder {
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): RESTPostAPIContextMenuApplicationCommandsJSONBody {
|
||||
const data = { ...structuredClone(this.data), type: ApplicationCommandType.Message };
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
messageCommandPredicate.parse(data);
|
||||
}
|
||||
validate(messageCommandPredicate, data, validationOverride);
|
||||
|
||||
return data as RESTPostAPIContextMenuApplicationCommandsJSONBody;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApplicationCommandType, type RESTPostAPIContextMenuApplicationCommandsJSONBody } from 'discord-api-types/v10';
|
||||
import { isValidationEnabled } from '../../../util/validation.js';
|
||||
import { validate } from '../../../util/validation.js';
|
||||
import { userCommandPredicate } from './Assertions.js';
|
||||
import { ContextMenuCommandBuilder } from './ContextMenuCommand.js';
|
||||
|
||||
@@ -9,10 +9,7 @@ export class UserContextCommandBuilder extends ContextMenuCommandBuilder {
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): RESTPostAPIContextMenuApplicationCommandsJSONBody {
|
||||
const data = { ...structuredClone(this.data), type: ApplicationCommandType.User };
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
userCommandPredicate.parse(data);
|
||||
}
|
||||
validate(userCommandPredicate, data, validationOverride);
|
||||
|
||||
return data as RESTPostAPIContextMenuApplicationCommandsJSONBody;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ActionRowBuilder } from '../../components/ActionRow.js';
|
||||
import { createComponentBuilder } from '../../components/Components.js';
|
||||
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js';
|
||||
import { resolveBuilder } from '../../util/resolveBuilder.js';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { modalPredicate } from './Assertions.js';
|
||||
|
||||
export interface ModalBuilderData extends Partial<Omit<APIModalInteractionResponseCallbackData, 'components'>> {
|
||||
@@ -162,9 +162,7 @@ export class ModalBuilder implements JSONEncodable<APIModalInteractionResponseCa
|
||||
components: components.map((component) => component.toJSON(validationOverride)),
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
modalPredicate.parse(data);
|
||||
}
|
||||
validate(modalPredicate, data, validationOverride);
|
||||
|
||||
return data as APIModalInteractionResponseCallbackData;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user