UNPKG

disjsx

Version:

A library for creating Discord messages in JSX

236 lines 8.79 kB
import { DISJSX } from "./disjsxTypes"; import { type MessageProps, type ModalProps } from "./types"; import type { ReactElement } from "react"; /** * Represents a validation error or warning found during component validation. */ export interface ValidationError { /** The severity level of the validation issue */ type: "error" | "warning"; /** Human-readable description of the validation issue */ message: string; /** The DISJSX component type where the issue occurred, if applicable */ component?: string; /** Internal component ID for tracking, if applicable */ componentId?: number; /** Path through the component tree where the issue was found */ path?: string[]; } /** * Result object returned from component validation operations. */ export interface ValidationResult { /** Whether the component tree passed validation without errors */ isValid: boolean; /** Array of validation errors that must be fixed */ errors: ValidationError[]; /** Array of validation warnings that should be addressed */ warnings: ValidationError[]; } /** * Component placement rules based on Discord documentation. */ export declare const COMPONENT_PLACEMENT_RULES: Readonly<{ V2_MESSAGE_ALLOWED: Set<DISJSX>; LEGACY_MESSAGE_ALLOWED: Set<DISJSX>; ACTION_ROW_ALLOWED: Set<DISJSX>; SECTION_TEXT_ALLOWED: Set<DISJSX>; SECTION_ACCESSORY_ALLOWED: Set<DISJSX>; CONTAINER_ALLOWED: Set<DISJSX>; EMBED_ALLOWED: Set<DISJSX>; EMBED_SINGULAR_COMPONENTS: Set<DISJSX>; }>; /** * Validation limits based on Discord documentation * * I think we generally should have a way to easily modify the limits via like a config or something in case you are stuck on an old version * but womp womp */ export declare const VALIDATION_LIMITS: Readonly<{ MESSAGE_V2_MAX_COMPONENTS: 40; MESSAGE_LEGACY_MAX_ACTION_ROWS: 5; ACTION_ROW_MAX_BUTTONS: 5; ACTION_ROW_MAX_SELECTS: 1; STRING_SELECT_MAX_OPTIONS: 25; SELECT_MIN_VALUES_MIN: 0; SELECT_MIN_VALUES_MAX: 25; SELECT_MAX_VALUES_MIN: 1; SELECT_MAX_VALUES_MAX: 25; MEDIA_GALLERY_MIN_ITEMS: 1; MEDIA_GALLERY_MAX_ITEMS: 10; SECTION_MIN_TEXT_COMPONENTS: 1; SECTION_MAX_TEXT_COMPONENTS: 3; EMBED_MAX_FIELDS: 25; BUTTON_LABEL_MAX_LENGTH: 80; SELECT_PLACEHOLDER_MAX_LENGTH: 150; TEXT_INPUT_MAX_LENGTH: 4000; CUSTOM_ID_MAX_LENGTH: 100; EMBED_TITLE_MAX_LENGTH: 256; EMBED_DESCRIPTION_MAX_LENGTH: 4096; EMBED_FIELD_NAME_MAX_LENGTH: 256; EMBED_FIELD_VALUE_MAX_LENGTH: 1024; EMBED_AUTHOR_NAME_MAX_LENGTH: 256; EMBED_FOOTER_TEXT_MAX_LENGTH: 2048; EMBED_TOTAL_CHARACTERS_MAX: 6000; THUMBNAIL_DESCRIPTION_MAX_LENGTH: 1024; MEDIA_DESCRIPTION_MAX_LENGTH: 1024; MODAL_TITLE_MAX_LENGTH: 45; MESSAGE_CONTENT_MAX_LENGTH: 2000; MESSAGE_MAX_EMBEDS: 10; ATTACHMENT_DESCRIPTION_MAX_LENGTH: 1024; }>; /** * Validates DISJSX component trees against Discord API requirements. */ export declare class ComponentValidator { private errors; private warnings; private componentCount; private path; /** * Validates a DISJSX component tree for messages or modals. * @param element The root React element to validate (Message or Modal) * @param isV2 Whether to validate as a V2 message format * @param context The validation context - "message" or "modal" * @returns ValidationResult containing validation status, errors, and warnings */ validate(element: ReactElement<MessageProps | ModalProps>, isV2?: boolean, context?: "message" | "modal"): ValidationResult; /** * Validates a Modal component and its children. * @param element The Modal component to validate */ private validateModal; /** * Validates a Message component for V2 or legacy format. * @param element The Message component to validate * @param isV2 Whether to validate as V2 format * @param context The validation context */ private validateMessage; /** * Validates components within a modal (only ActionRows with TextInputs). * @param children The modal's child components */ private validateModalComponents; /** * Validates ActionRow components specifically for modal context. * @param element The ActionRow component to validate */ private validateActionRowForModal; /** * Validates components for V2 message format. * @param children The message's child components */ private validateV2MessageComponents; /** * Validates components for legacy message format. * @param children The message's child components */ private validateLegacyMessageComponents; /** * Dispatches validation to the appropriate method based on component type. * @param element The component to validate * @param disjsxType The DISJSX component type */ private validateComponent; /** * Validates ActionRow components and their children. * @param element The ActionRow component to validate */ private validateActionRow; /** * Validates Button components based on their style and properties. * @param element The Button component to validate */ private validateButton; /** * Validates StringSelect components and their options. * @param element The StringSelect component to validate */ private validateStringSelect; /** * Validates auto-populated select components (User, Role, Mentionable, Channel). * @param element The select component to validate * @param selectType The type of select component */ private validateAutoPopulatedSelect; /** * Validates minValues and maxValues for select components. * @param minValues The minimum number of selections * @param maxValues The maximum number of selections * @param optionCount The total number of available options */ private validateSelectValues; /** * Validates Section components and their text/accessory children. * @param element The Section component to validate */ private validateSection; /** * Validates Container components and their children. * @param element The Container component to validate */ private validateContainer; /** * Validates MediaGallery components and item count limits. * @param element The MediaGallery component to validate */ private validateMediaGallery; /** * Validates Embed components and their properties. * @param element The Embed component to validate */ private validateEmbed; /** * Validates Embed children and enforces singular component rules. * @param element The Embed component whose children to validate */ private validateEmbedChildren; /** * Validates individual embed field name and value lengths. * @param element The EmbedField component to validate */ private validateEmbedField; /** * Validates TextInput components for modals. * @param element The TextInput component to validate */ private validateTextInput; /** * Validates total character count across all embeds in a message. * @param embeds Array of embed components to validate */ private validateEmbedTotalCharacterCount; /** * Adds a validation error to the results. * @param message The error message * @param component The component type where the error occurred */ private addError; /** * Adds a validation warning to the results. * @param message The warning message * @param component The component type where the warning occurred */ private addWarning; /** * Returns the final validation result. * @returns The complete validation result with errors and warnings */ private getResult; } /** * Validates a DISJSX component tree before rendering. * @param element The root React element to validate (Message or Modal component) * @param isV2 Whether to validate as a V2 message format * @param context The validation context - "message" or "modal" * @returns ValidationResult containing validation status, errors, and warnings */ export declare const validateComponent: (element: ReactElement<MessageProps | ModalProps>, isV2?: boolean, context?: "message" | "modal") => ValidationResult; /** * Validates that all customId properties within a component tree are unique. * @param element The root React element to check for duplicate customIds * @returns Array of validation errors for any duplicate customIds found */ export declare const validateUniqueCustomIds: (element: ReactElement) => ValidationError[]; //# sourceMappingURL=validation.d.ts.map