or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

base32-utilities.mdgeneration.mdindex.mdtime-utilities.mduuid-conversion.mdvalidation.md

validation.mddocs/

0

# Validation

1

2

ULID format validation to ensure proper structure, length, and character encoding according to the ULID specification.

3

4

## Capabilities

5

6

### ULID Validation

7

8

Validates whether a string is a properly formatted ULID.

9

10

```typescript { .api }

11

/**

12

* Check if a ULID is valid

13

* @param id - The string to validate

14

* @returns True if the string is a valid ULID, false otherwise

15

*/

16

function isValid(id: string): boolean;

17

```

18

19

**Usage Examples:**

20

21

```typescript

22

import { isValid, ulid } from "ulid";

23

24

// Validate generated ULID

25

const id = ulid();

26

console.log(isValid(id)); // true

27

28

// Validate existing ULIDs

29

console.log(isValid("01HNZX8JGFACFA36RBXDHEQN6E")); // true

30

console.log(isValid("01ARYZ6S41TSV4RRFFQ69G5FAV")); // true

31

32

// Invalid cases

33

console.log(isValid("")); // false - empty string

34

console.log(isValid("invalid")); // false - wrong length

35

console.log(isValid("01HNZX8JGFACFA36RBXDHEQN6")); // false - too short

36

console.log(isValid("01HNZX8JGFACFA36RBXDHEQN6EX")); // false - too long

37

console.log(isValid("01HNZX8JGFACFA36RBXDHEQN6U")); // false - invalid character 'U'

38

console.log(isValid("81HNZX8JGFACFA36RBXDHEQN6E")); // false - invalid first character

39

40

// Use in conditional logic

41

function processULID(id: string) {

42

if (!isValid(id)) {

43

throw new Error(`Invalid ULID: ${id}`);

44

}

45

// Process valid ULID

46

return id.toLowerCase();

47

}

48

49

// Filter valid ULIDs from array

50

const ids = ["01HNZX8JGFACFA36RBXDHEQN6E", "invalid", "01ARYZ6S41TSV4RRFFQ69G5FAV"];

51

const validIds = ids.filter(isValid);

52

console.log(validIds); // ["01HNZX8JGFACFA36RBXDHEQN6E", "01ARYZ6S41TSV4RRFFQ69G5FAV"]

53

```

54

55

**Validation Rules:**

56

57

The `isValid` function checks that the input string:

58

59

1. **Is a string type** - Non-string values return false

60

2. **Has correct length** - Must be exactly 26 characters

61

3. **Uses valid characters** - All characters must be from Crockford Base32 alphabet

62

4. **Has valid first character** - Must be 0-7 (ensures timestamp doesn't exceed maximum)

63

64

**Crockford Base32 Alphabet:**

65

66

Valid characters: `0123456789ABCDEFGHJKMNPQRSTVWXYZ`

67

68

Excluded characters: `I`, `L`, `O`, `U` (to avoid confusion with similar-looking characters)

69

70

**Case Sensitivity:**

71

72

The validation function is case-insensitive. Both uppercase and lowercase characters are accepted:

73

74

```typescript

75

console.log(isValid("01hnzx8jgfacfa36rbxdheqn6e")); // true - lowercase

76

console.log(isValid("01HNZX8JGFACFA36RBXDHEQN6E")); // true - uppercase

77

console.log(isValid("01HnZx8JgFaCfA36RbXdHeQn6E")); // true - mixed case

78

```

79

80

**Performance Notes:**

81

82

- The validation is performed using regular expressions for efficiency

83

- Case conversion is handled internally without modifying input

84

- No exceptions are thrown - invalid inputs return `false`