Skip to main content

ValidationError

Enum ValidationError 

Source
#[non_exhaustive]
pub enum ValidationError { TooLong { field: Cow<'static, str>, max: usize, actual: usize, }, TooMany { field: Cow<'static, str>, max: usize, actual: usize, }, TooFew { field: Cow<'static, str>, min: usize, actual: usize, }, Missing { field: Cow<'static, str>, }, OutOfRange { field: Cow<'static, str>, min: i64, max: i64, actual: i64, }, InvalidUrl { field: Cow<'static, str>, allowed: &'static [&'static str], }, InvalidName { field: Cow<'static, str>, character: char, }, Invalid { field: Cow<'static, str>, reason: &'static str, }, Empty { field: Cow<'static, str>, needs: &'static str, }, }
Expand description

A Discord limit or rule broken by a builder’s content, returned by build() (D-179).

build() stops at the first violation it finds and returns it (D-182). field is the path of the offending value in the JSON sent to Discord, written the way Discord writes it in its own 50035 Invalid Form Body errors — title, fields[3].name, author.url — so both kinds of error point at the same place.

Texts are measured like Discord measures them: in Unicode code points (str::chars), without leading and trailing whitespace (D-183, confirmed on the live API). An emoji such as 🔥 counts once — discord.js would count it twice.

discord.js counterpart: the errors thrown by the setters of @discordjs/builders (ValidationError / CombinedError from @sapphire/shapeshift).

§Examples

use djs_builders::{EmbedBuilder, ValidationError};

let error = EmbedBuilder::new().title("x".repeat(300)).build().unwrap_err();
assert_eq!(
    error,
    ValidationError::TooLong { field: "title".into(), max: 256, actual: 300 }
);
assert_eq!(error.field(), "title");
assert_eq!(error.to_string(), "title is too long: 300 characters, at most 256 allowed");

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

TooLong

A text is longer than Discord allows.

Fields

§field: Cow<'static, str>

The path of the text, e.g. description or fields[2].value.

§max: usize

The most characters (code points after trimming) Discord allows.

§actual: usize

The characters the text has, counted the same way.

§

TooMany

A list has more items than Discord allows.

Fields

§field: Cow<'static, str>

The path of the list, e.g. fields.

§max: usize

The most items Discord allows.

§actual: usize

The items the list has.

§

TooFew

A list has fewer items than Discord requires — for example an empty action row.

Fields

§field: Cow<'static, str>

The path of the list, e.g. components.

§min: usize

The fewest items Discord allows.

§actual: usize

The items the list has.

§

Missing

A value that Discord requires is not set.

Fields

§field: Cow<'static, str>

The path of the value.

§

OutOfRange

A number is outside the range Discord allows.

Fields

§field: Cow<'static, str>

The path of the number, e.g. color.

§min: i64

The smallest value allowed.

§max: i64

The largest value allowed.

§actual: i64

The value given.

§

InvalidUrl

A URL does not start with one of the schemes Discord allows for it (D-187, D-190).

Only the scheme is checked; Discord itself rejects a malformed address such as a bare https://.

Fields

§field: Cow<'static, str>

The path of the URL, e.g. author.url.

§allowed: &'static [&'static str]

The allowed scheme prefixes, e.g. ["http://", "https://"] (compared without regard to case).

§

InvalidName

A command, subcommand or option name has a character Discord does not allow (D-194): anything but letters, numbers, -, _ and ʼ, or a letter that has a lower-case form.

Fields

§field: Cow<'static, str>

The path of the name, e.g. options[0].name.

§character: char

The first character that is not allowed.

§

Invalid

Values that are valid one by one do not work together — for example a required option after an optional one, or choices together with autocomplete.

Fields

§field: Cow<'static, str>

The path of the value that breaks the rule, e.g. options[1].required.

§reason: &'static str

What is wrong, for the error message.

§

Empty

A value has no content at all, which Discord rejects — for example an embed with nothing but a color.

Fields

§field: Cow<'static, str>

The path of the value, e.g. embed.

§needs: &'static str

What the value needs at least one of, for the error message.

Implementations§

Source§

impl ValidationError

Source

pub fn field(&self) -> &str

Returns the path of the value that breaks the rule, e.g. fields[3].name.

discord.js counterpart: none (the path inside a validation error of @sapphire/shapeshift).

§Examples
use djs_builders::EmbedBuilder;

let error = EmbedBuilder::new().field("ok", "y".repeat(2000)).build().unwrap_err();
assert_eq!(error.field(), "fields[0].value");

Trait Implementations§

Source§

impl Clone for ValidationError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ValidationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ValidationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ValidationError

Source§

impl Error for ValidationError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl Hash for ValidationError

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ValidationError

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ValidationError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToCompactString for T
where T: Display,

§

fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>

Fallible version of [ToCompactString::to_compact_string()] Read more
§

fn to_compact_string(&self) -> CompactString

Converts the given value to a [CompactString]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.