mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +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:
@@ -16,7 +16,7 @@ import type {
|
||||
import { ComponentType } from 'discord-api-types/v10';
|
||||
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 { actionRowPredicate } from './Assertions.js';
|
||||
import { ComponentBuilder } from './Component.js';
|
||||
import type { AnyActionRowComponentBuilder } from './Components.js';
|
||||
@@ -336,9 +336,7 @@ export class ActionRowBuilder extends ComponentBuilder<APIActionRowComponent<API
|
||||
components: components.map((component) => component.toJSON(validationOverride)),
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
actionRowPredicate.parse(data);
|
||||
}
|
||||
validate(actionRowPredicate, data, validationOverride);
|
||||
|
||||
return data as APIActionRowComponent<APIActionRowComponentTypes>;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { APIButtonComponent } from 'discord-api-types/v10';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { buttonPredicate } from '../Assertions.js';
|
||||
import { ComponentBuilder } from '../Component.js';
|
||||
|
||||
@@ -24,10 +24,7 @@ export abstract class BaseButtonBuilder<ButtonData extends APIButtonComponent> e
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): ButtonData {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
buttonPredicate.parse(clone);
|
||||
}
|
||||
validate(buttonPredicate, clone, validationOverride);
|
||||
|
||||
return clone as ButtonData;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
SelectMenuDefaultValueType,
|
||||
} from 'discord-api-types/v10';
|
||||
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { selectMenuChannelPredicate } from '../Assertions.js';
|
||||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
||||
|
||||
@@ -108,10 +108,7 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSe
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): APIChannelSelectComponent {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuChannelPredicate.parse(clone);
|
||||
}
|
||||
validate(selectMenuChannelPredicate, clone, validationOverride);
|
||||
|
||||
return clone as APIChannelSelectComponent;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
SelectMenuDefaultValueType,
|
||||
} from 'discord-api-types/v10';
|
||||
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { selectMenuMentionablePredicate } from '../Assertions.js';
|
||||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
||||
|
||||
@@ -119,10 +119,7 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): APIMentionableSelectComponent {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuMentionablePredicate.parse(clone);
|
||||
}
|
||||
validate(selectMenuMentionablePredicate, clone, validationOverride);
|
||||
|
||||
return clone as APIMentionableSelectComponent;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
SelectMenuDefaultValueType,
|
||||
} from 'discord-api-types/v10';
|
||||
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { selectMenuRolePredicate } from '../Assertions.js';
|
||||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
||||
|
||||
@@ -82,10 +82,7 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectCo
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): APIRoleSelectComponent {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuRolePredicate.parse(clone);
|
||||
}
|
||||
validate(selectMenuRolePredicate, clone, validationOverride);
|
||||
|
||||
return clone as APIRoleSelectComponent;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ComponentType } from 'discord-api-types/v10';
|
||||
import type { APIStringSelectComponent, APISelectMenuOption } from 'discord-api-types/v10';
|
||||
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 { selectMenuStringPredicate } from '../Assertions.js';
|
||||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
||||
import { StringSelectMenuOptionBuilder } from './StringSelectMenuOption.js';
|
||||
@@ -156,9 +156,7 @@ export class StringSelectMenuBuilder extends BaseSelectMenuBuilder<APIStringSele
|
||||
options: options.map((option) => option.toJSON(false)),
|
||||
};
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuStringPredicate.parse(data);
|
||||
}
|
||||
validate(selectMenuStringPredicate, data, validationOverride);
|
||||
|
||||
return data as APIStringSelectComponent;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { JSONEncodable } from '@discordjs/util';
|
||||
import type { APIMessageComponentEmoji, APISelectMenuOption } from 'discord-api-types/v10';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { selectMenuStringOptionPredicate } from '../Assertions.js';
|
||||
|
||||
/**
|
||||
@@ -106,10 +106,7 @@ export class StringSelectMenuOptionBuilder implements JSONEncodable<APISelectMen
|
||||
*/
|
||||
public toJSON(validationOverride?: boolean): APISelectMenuOption {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuStringOptionPredicate.parse(clone);
|
||||
}
|
||||
validate(selectMenuStringOptionPredicate, clone, validationOverride);
|
||||
|
||||
return clone as APISelectMenuOption;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
SelectMenuDefaultValueType,
|
||||
} from 'discord-api-types/v10';
|
||||
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { selectMenuUserPredicate } from '../Assertions.js';
|
||||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
||||
|
||||
@@ -82,10 +82,7 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
|
||||
*/
|
||||
public override toJSON(validationOverride?: boolean): APIUserSelectComponent {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
selectMenuUserPredicate.parse(clone);
|
||||
}
|
||||
validate(selectMenuUserPredicate, clone, validationOverride);
|
||||
|
||||
return clone as APIUserSelectComponent;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ComponentType, type TextInputStyle, type APITextInputComponent } from 'discord-api-types/v10';
|
||||
import { isValidationEnabled } from '../../util/validation.js';
|
||||
import { validate } from '../../util/validation.js';
|
||||
import { ComponentBuilder } from '../Component.js';
|
||||
import { textInputPredicate } from './Assertions.js';
|
||||
|
||||
@@ -154,10 +154,7 @@ export class TextInputBuilder extends ComponentBuilder<APITextInputComponent> {
|
||||
*/
|
||||
public toJSON(validationOverride?: boolean): APITextInputComponent {
|
||||
const clone = structuredClone(this.data);
|
||||
|
||||
if (validationOverride ?? isValidationEnabled()) {
|
||||
textInputPredicate.parse(clone);
|
||||
}
|
||||
validate(textInputPredicate, clone, validationOverride);
|
||||
|
||||
return clone as APITextInputComponent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user