Skip to main content
DiscordKit provides a comprehensive set of validation utilities built on top of Valibot for validating Discord API data structures.

Schemas

snowflake

Validates Discord snowflake IDs - unique 64-bit unsigned integers used as identifiers throughout the Discord API.
import { snowflake } from '@discordkit/core/validations';
Type Signature
const snowflake: GenericSchema<string>
Details Validates whether a number, bigint, or numeric string is a valid Snowflake by checking if its derived timestamp is at or after the Discord epoch (January 1, 2015). Example
import * as v from 'valibot';
import { snowflake } from '@discordkit/core/validations';

const UserSchema = v.object({
  id: snowflake,
  username: v.string()
});

v.parse(UserSchema, {
  id: '123456789012345678',
  username: 'discord_user'
});

snowflakeToDate

Converts a snowflake string to a JavaScript Date object.
import { snowflakeToDate } from '@discordkit/core/validations';
Type Signature
const snowflakeToDate: (
  val: string | number | bigint,
  epoch?: bigint
) => Date
val
string | number | bigint
required
The snowflake value to convert
epoch
bigint
default:"1420070400000n"
Time in milliseconds to use as the epoch. Defaults to Discord’s epoch (January 1, 2015)
Example
import { snowflakeToDate } from '@discordkit/core/validations';

const date = snowflakeToDate('123456789012345678');
console.log(date.toISOString()); // Converted date

bitfield

Creates a schema that validates bitfield numeric values - data structures for efficiently serializing groups of boolean flags.
import { bitfield } from '@discordkit/core/validations';
Type Signature
const bitfield: <TName extends string>(
  name: TName,
  flags: Flags,
  message?: string
) => SchemaWithPipe<...>
name
string
required
A name to differentiate this custom schema
flags
Flags
required
An enum of bitwise flags. The Flags type is { [key: string]: number | bigint | string }
message
string
default:"Invalid Bitfield"
Error message to display when validation fails
Example
import { bitfield } from '@discordkit/core/validations';

enum UserFlags {
  Staff = 1 << 0,
  Partner = 1 << 1,
  BugHunter = 1 << 2,
  Premium = 1 << 3
}

const userFlagsSchema = bitfield('UserFlags', UserFlags);

// Validates bitfield values
v.parse(userFlagsSchema, 5); // Staff + BugHunter

asDigits

Transforms a bitfield schema into a numeric string.
import { asDigits } from '@discordkit/core/validations';
Type Signature
const asDigits: (
  schema: ReturnType<typeof bitfield>
) => GenericSchema<string>
schema
ReturnType<typeof bitfield>
required
A bitfield schema to transform
Example
import { bitfield, asDigits } from '@discordkit/core/validations';

const flags = bitfield('Flags', MyFlags);
const flagsAsString = asDigits(flags);

asInteger

Transforms a bitfield schema into an integer.
import { asInteger } from '@discordkit/core/validations';
Type Signature
const asInteger: (
  schema: ReturnType<typeof bitfield>
) => GenericSchema<number>
schema
ReturnType<typeof bitfield>
required
A bitfield schema to transform
Example
import { bitfield, asInteger } from '@discordkit/core/validations';

const flags = bitfield('Flags', MyFlags);
const flagsAsNumber = asInteger(flags);

boundedString

Validates a non-empty string with length constraints.
import { boundedString } from '@discordkit/core/validations';
Type Signature
const boundedString: (
  req?: number | { min?: number; max?: number }
) => GenericSchema<string>
req
number | { min?: number; max?: number }
Exact length (if number) or min/max bounds (if object)
Example
import { boundedString } from '@discordkit/core/validations';

// Exact length
const exactString = boundedString(10);

// Min/max bounds
const usernameSchema = boundedString({ min: 2, max: 32 });

// Min only (defaults to 1)
const descriptionSchema = boundedString({ max: 100 });

boundedArray

Validates a non-empty array with length constraints.
import { boundedArray } from '@discordkit/core/validations';
Type Signature
const boundedArray: <TItem>(
  items: GenericSchema<TItem>,
  req?: number | { min?: number; max?: number }
) => GenericSchema<TItem[]>
items
GenericSchema<TItem>
required
Schema for validating array items
req
number | { min?: number; max?: number }
Exact length (if number) or min/max bounds (if object)
Example
import * as v from 'valibot';
import { boundedArray } from '@discordkit/core/validations';

// Exact length
const threeStrings = boundedArray(v.string(), 3);

// Min/max bounds
const roles = boundedArray(v.string(), { min: 1, max: 10 });

boundedInteger

Validates an integer with value constraints.
import { boundedInteger } from '@discordkit/core/validations';
Type Signature
const boundedInteger: (
  req?: number | { min?: number; max?: number }
) => GenericSchema<number>
req
number | { min?: number; max?: number }
Exact value (if number) or min/max bounds (if object)
Example
import { boundedInteger } from '@discordkit/core/validations';

// Exact value
const constantValue = boundedInteger(42);

// Range
const percentageSchema = boundedInteger({ min: 0, max: 100 });

datauri

Validates that a string is a data URI scheme.
import { datauri } from '@discordkit/core/validations';
Type Signature
const datauri: GenericSchema<string>
Example
import { datauri } from '@discordkit/core/validations';

const imageSchema = v.object({
  data: datauri
});

v.parse(imageSchema, {
  data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...'
});

extractDataURIMetadata

Extracts metadata from a data URI string.
import { extractDataURIMetadata } from '@discordkit/core/validations';
Type Signature
const extractDataURIMetadata: (
  val: string
) => Partial<{
  mediaType: string;
  mimeType: `${string}/${string}`;
  params: string;
  encoding: string;
  data: string;
}>
val
string
required
The data URI string to parse
Returns An object containing extracted metadata, or an empty object if the data URI is invalid. Example
import { extractDataURIMetadata } from '@discordkit/core/validations';

const metadata = extractDataURIMetadata(
  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...'
);

console.log(metadata.mimeType); // 'image/png'
console.log(metadata.encoding); // ';base64'

hasMimeType

Validation action to check if a data URI has one of the provided MIME types.
import { hasMimeType } from '@discordkit/core/validations';
Type Signature
const hasMimeType: (
  requirement: Array<`${string}/${string}`>,
  message?: string
) => CheckAction<string, typeof message>
requirement
Array<string>
required
Array of allowed MIME types (e.g., ['image/png', 'image/jpeg'])
message
string
default:"Received badly formatted Data URI"
Error message on failed validation
Example
import * as v from 'valibot';
import { datauri, hasMimeType } from '@discordkit/core/validations';

const imageSchema = v.pipe(
  datauri,
  hasMimeType(['image/png', 'image/jpeg', 'image/gif'])
);

hasSize

Validation action to check if a data URI’s decoded data is within size constraints.
import { hasSize } from '@discordkit/core/validations';
Type Signature
const hasSize: (
  size: number | { min?: number; max?: number },
  message?: string
) => CheckAction<string, typeof message>
size
number | { min?: number; max?: number }
required
Exact size in bytes (if number) or min/max bounds (if object)
message
string
default:"Data URI is the incorrect size"
Error message on failed validation
Example
import * as v from 'valibot';
import { datauri, hasSize } from '@discordkit/core/validations';

const avatarSchema = v.pipe(
  datauri,
  hasSize({ max: 1024 * 1024 }) // Max 1MB
);

toBlob

Transforms a data URI string to a Blob object.
import { toBlob } from '@discordkit/core/validations';
Type Signature
const toBlob: TransformAction<string, Blob>
Example
import * as v from 'valibot';
import { datauri, toBlob } from '@discordkit/core/validations';

const imageSchema = v.pipe(datauri, toBlob);

const blob = v.parse(imageSchema, 'data:image/png;base64,iVBORw0KGg...');

timestamp

Validates an ISO8601 timestamp string.
import { timestamp } from '@discordkit/core/validations';
Type Signature
const timestamp: GenericSchema<string>
Example
import { timestamp } from '@discordkit/core/validations';

const eventSchema = v.object({
  name: v.string(),
  createdAt: timestamp
});

v.parse(eventSchema, {
  name: 'Guild Created',
  createdAt: '2024-01-15T10:30:00.000Z'
});

url

Validates a URL string.
import { url } from '@discordkit/core/validations';
Type Signature
const url: GenericSchema<string>
Example
import { url } from '@discordkit/core/validations';

const profileSchema = v.object({
  website: url
});

v.parse(profileSchema, {
  website: 'https://discord.com'
});

Constants

DISCORD_EPOCH

The Discord epoch as a BigInt - represents the first second of 2015 (January 1, 2015, 00:00:00 UTC).
import { DISCORD_EPOCH } from '@discordkit/core/validations';

console.log(DISCORD_EPOCH); // 1420070400000n

Build docs developers (and LLMs) love