Function serenity::utils::validate_token

source ·
pub fn validate_token(token: impl AsRef<str>) -> Result<(), InvalidToken>
Available on crate feature utils only.
Expand description

Validates that a token is likely in a valid format.

This performs the following checks on a given token:

  • Is not empty;
  • Contains 3 parts (split by the period char '.');
  • The second part of the token is at least 6 characters long;

§Examples

Validate that a token is valid and that a number of malformed tokens are actually invalid:

use serenity::utils::token::validate;

// ensure a valid token is in fact a valid format:
assert!(validate("Mjg4NzYwMjQxMzYzODc3ODg4.C_ikow.j3VupLBuE1QWZng3TMGH0z_UAwg").is_ok());

assert!(validate("Mjg4NzYwMjQxMzYzODc3ODg4").is_err());
assert!(validate("").is_err());

§Errors

Returns a InvalidToken when one of the above checks fail. The type of failure is not specified.