[][src]Function serenity::client::validate_token

pub fn validate_token(token: impl AsRef<str>) -> Result<()>

Validates that a token is likely in a valid format.

This performs the following checks on a given token:

  • At least one character long;
  • Contains 3 parts (split by the period char '.');
  • The second part of the token is at least 6 characters long;
  • The token does not contain any whitespace prior to or after the token.

Examples

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

use serenity::client::validate_token;

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

// "cat" isn't a valid token:
assert!(validate_token("cat").is_err());

// tokens must have three parts, separated by periods (this is still
// actually an invalid token):
assert!(validate_token("aaa.abcdefgh.bbb").is_ok());

// the second part must be _at least_ 6 characters long:
assert!(validate_token("a.abcdef.b").is_ok());
assert!(validate_token("a.abcde.b").is_err());

Errors

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