Skip to main content

validate_components

Function validate_components 

Source
pub fn validate_components(
    components: &[Component],
    version: ComponentsVersion,
) -> Result<(), ValidationError>
Expand description

Checks the limits that apply to all components of one message together (D-199), and each component itself.

  • Legacy: at most 5 components, all action rows.
  • V2: only layout and content components at the top level; at most 40 components counting nested ones, and at most 4000 characters (code points after trimming) across the text displays — as measured on the live API (D-200, D-201).
  • Both: every custom_id is unique in the message (D-200).

Discord also refuses a request that is too large in bytes (413) — for example a single text display of 4000 emoji —, which this does not check.

discord.js counterpart: none (@discordjs/builders checks each component alone).

§Errors

The first ValidationError found: the number or type of top-level components, a component’s own rules, a repeated custom_id, then the V2 totals (TooMany / TooLong with the field components).

§Examples

use djs_builders::{ActionRowBuilder, ButtonBuilder, ComponentsVersion, validate_components};

let row = |id: &str| {
    ActionRowBuilder::new()
        .component(ButtonBuilder::primary(id).label("Go"))
        .build()
        .unwrap()
};
assert!(validate_components(&[row("a"), row("b")], ComponentsVersion::Legacy).is_ok());

let error = validate_components(&[row("a"), row("a")], ComponentsVersion::Legacy).unwrap_err();
assert_eq!(error.field(), "components[1].components[0].custom_id");