Validation predicates for TypeID components.
Manual validation predicates with zero external dependencies.
This namespace provides low-level validation functions used internally
by typeid.core and typeid.codec. Most users should use the high-level
API in typeid.core instead.
valid-prefix? - Check if a prefix matches the TypeID patternvalidate-prefix - Validate prefix with detailed error reportingvalid-base32-suffix? - Check if a suffix is valid base32valid-typeid-string? - Check if a string has valid TypeID formatvalid-uuid-bytes? - Check if bytes represent a valid 16-byte UUIDValidation predicates for TypeID components. Manual validation predicates with zero external dependencies. ## Overview This namespace provides low-level validation functions used internally by `typeid.core` and `typeid.codec`. Most users should use the high-level API in [[typeid.core]] instead. ## Main Functions - [[valid-prefix?]] - Check if a prefix matches the TypeID pattern - [[validate-prefix]] - Validate prefix with detailed error reporting - [[valid-base32-suffix?]] - Check if a suffix is valid base32 - [[valid-typeid-string?]] - Check if a string has valid TypeID format - [[valid-uuid-bytes?]] - Check if bytes represent a valid 16-byte UUID
(valid-base32-suffix? s)Check if suffix is valid base32 TypeID suffix.
0-9a-hjkmnp-tv-z)0-7 (prevents 128-bit overflow)(valid-base32-suffix? "01h5fskfsk4fpeqwnsyz5hj55t")
;;=> true
(valid-base32-suffix? "81h5fskfsk4fpeqwnsyz5hj55t")
;;=> false ; first char > 7
(valid-base32-suffix? "01h5fskfsk4fpeqwnsyz5hj55")
;;=> false ; too short
Check if suffix is valid base32 TypeID suffix. ## Requirements - Exactly 26 characters - All characters in Crockford base32 alphabet (`0-9a-hjkmnp-tv-z`) - First character must be `0-7` (prevents 128-bit overflow) ## Examples ```clojure (valid-base32-suffix? "01h5fskfsk4fpeqwnsyz5hj55t") ;;=> true (valid-base32-suffix? "81h5fskfsk4fpeqwnsyz5hj55t") ;;=> false ; first char > 7 (valid-base32-suffix? "01h5fskfsk4fpeqwnsyz5hj55") ;;=> false ; too short ```
(valid-prefix? s)Check if prefix matches the TypeID prefix pattern.
[a-z]([a-z_]{0,61}[a-z])?(valid-prefix? "user")
;;=> true
(valid-prefix? "user_account")
;;=> true
(valid-prefix? "")
;;=> true
(valid-prefix? "User")
;;=> false ; uppercase not allowed
(valid-prefix? "user_")
;;=> false ; cannot end with underscore
Check if prefix matches the TypeID prefix pattern.
## Valid Prefixes
- Empty string
- 1-63 lowercase characters matching pattern: `[a-z]([a-z_]{0,61}[a-z])?`
## Examples
```clojure
(valid-prefix? "user")
;;=> true
(valid-prefix? "user_account")
;;=> true
(valid-prefix? "")
;;=> true
(valid-prefix? "User")
;;=> false ; uppercase not allowed
(valid-prefix? "user_")
;;=> false ; cannot end with underscore
```(valid-typeid-string? s)Check if string has valid TypeID format (basic checks).
Note: This only checks basic format. Use typeid.core/explain for complete validation.
(valid-typeid-string? "user_01h5fskfsk4fpeqwnsyz5hj55t")
;;=> true
(valid-typeid-string? "01h5fskfsk4fpeqwnsyz5hj55t")
;;=> true
(valid-typeid-string? "User_01h5fskfsk4fpeqwnsyz5hj55t")
;;=> false ; uppercase
Check if string has valid TypeID format (basic checks). ## Requirements - Length between 26 and 90 characters - 26 characters: suffix only - Up to 90 characters: 63 (max prefix) + 1 (underscore) + 26 (suffix) - All lowercase **Note:** This only checks basic format. Use [[typeid.core/explain]] for complete validation. ## Examples ```clojure (valid-typeid-string? "user_01h5fskfsk4fpeqwnsyz5hj55t") ;;=> true (valid-typeid-string? "01h5fskfsk4fpeqwnsyz5hj55t") ;;=> true (valid-typeid-string? "User_01h5fskfsk4fpeqwnsyz5hj55t") ;;=> false ; uppercase ```
(valid-uuid-bytes? b)Check if bytes represent a valid UUID.
bytes?)js/Uint8Array(def uuid-bytes (byte-array 16))
(valid-uuid-bytes? uuid-bytes)
;;=> true
(valid-uuid-bytes? (byte-array 15))
;;=> false ; too short
Check if bytes represent a valid UUID. ## Requirements - Exactly 16 bytes - Platform-specific byte array type: - JVM: byte array (`bytes?`) - ClojureScript: `js/Uint8Array` ## Examples ```clojure (def uuid-bytes (byte-array 16)) (valid-uuid-bytes? uuid-bytes) ;;=> true (valid-uuid-bytes? (byte-array 15)) ;;=> false ; too short ```
(valid-uuidv7-bytes? b)Stricter validation for UUIDv7 format.
Checks that bytes conform to UUIDv7 specification:
0111 (version 7)10 (RFC 4122 variant)(require '[typeid.impl.uuid :as uuid])
(def v7-bytes (uuid/generate-uuidv7))
(valid-uuidv7-bytes? v7-bytes)
;;=> true
;; Random bytes won't pass version check
(valid-uuidv7-bytes? (byte-array 16))
;;=> false ; version/variant bits not set correctly
Stricter validation for UUIDv7 format. Checks that bytes conform to UUIDv7 specification: - Exactly 16 bytes - Version bits (bits 48-51) = `0111` (version 7) - Variant bits (bits 64-65) = `10` (RFC 4122 variant) ## Examples ```clojure (require '[typeid.impl.uuid :as uuid]) (def v7-bytes (uuid/generate-uuidv7)) (valid-uuidv7-bytes? v7-bytes) ;;=> true ;; Random bytes won't pass version check (valid-uuidv7-bytes? (byte-array 16)) ;;=> false ; version/variant bits not set correctly ```
(validate-prefix prefix)Validate prefix and return detailed result.
{:ok prefix} if valid{:error error-map} if invalidThe error map contains :type, :message, and :data keys.
(validate-prefix "user")
;;=> {:ok "user"}
(validate-prefix "User")
;;=> {:error {:type :invalid-prefix-format, :message "...", ...}}
(validate-prefix (str/join (repeat 64 "a")))
;;=> {:error {:type :prefix-too-long, :message "...", ...}}
Validate prefix and return detailed result.
## Returns
- `{:ok prefix}` if valid
- `{:error error-map}` if invalid
The error map contains `:type`, `:message`, and `:data` keys.
## Examples
```clojure
(validate-prefix "user")
;;=> {:ok "user"}
(validate-prefix "User")
;;=> {:error {:type :invalid-prefix-format, :message "...", ...}}
(validate-prefix (str/join (repeat 64 "a")))
;;=> {:error {:type :prefix-too-long, :message "...", ...}}
```cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |