Skip to main content
The validate command checks if an ID is valid and optionally identifies its type. It supports single ID validation, batch processing from stdin, and multiple output formats.

Basic Usage

Validate a single ID with automatic type detection:
uniku validate 018e5e5c-7c8a-7000-8000-000000000000
# => Valid UUID

uniku validate 01HW9T2W9W9YJ3JZ1H4P4M2T8Q
# => Valid ULID

uniku validate invalid-id
# => Invalid ID
# (exits with code 2)
The CLI automatically detects the ID type and validates against the appropriate format.

Explicit Type Validation

Specify the expected ID type using --type:
# Validate as UUID
uniku validate --type uuid 550e8400-e29b-41d4-a716-446655440000
# => Valid UUID

# Validate as ULID
uniku validate --type ulid 01HW9T2W9W9YJ3JZ1H4P4M2T8Q
# => Valid ULID

# Type mismatch
uniku validate --type uuid 01HW9T2W9W9YJ3JZ1H4P4M2T8Q
# => Invalid UUID
--type
uuid | ulid | nanoid | cuid | ksuid
Expected ID type. If omitted, type is auto-detected.
uniku validate --type ulid 01HW9T2W9W9YJ3JZ1H4P4M2T8Q
Auto-detection works by testing the ID format against all known patterns. Explicit type checking is faster and more precise.

Batch Validation

Validate multiple IDs from stdin using --stdin:
# From a file
cat ids.txt | uniku validate --stdin

# From echo
echo -e "018e5e5c-7c8a-7000-8000-000000000000\n01HW9T2W9W9YJ3JZ1H4P4M2T8Q" | uniku validate --stdin

# From command output
uniku uuid --count 5 | uniku validate --stdin
--stdin
boolean
default:"false"
Read IDs from stdin, one per line
cat ids.txt | uniku validate --stdin

Example: Batch Validation

Create a file with multiple IDs:
cat > ids.txt <<EOF
018e5e5c-7c8a-7000-8000-000000000000
01HW9T2W9W9YJ3JZ1H4P4M2T8Q
V1StGXR8_Z5jdHi6B-myT
invalid-id
EOF

# Validate all IDs
cat ids.txt | uniku validate --stdin
Output:
018e5e5c-7c8a-7000-8000-000000000000: Valid UUID
01HW9T2W9W9YJ3JZ1H4P4M2T8Q: Valid ULID
V1StGXR8_Z5jdHi6B-myT: Valid Nanoid
invalid-id: Invalid ID
The command exits with code 2 if any ID is invalid.
When using --stdin, the command fails if any ID is invalid. Use --json or --quiet for programmatic error handling.

Quiet Mode

Use --quiet for silent validation with exit codes only:
# Valid ID (exits with 0)
uniku validate --quiet 018e5e5c-7c8a-7000-8000-000000000000
echo $?  # => 0

# Invalid ID (exits with 2)
uniku validate --quiet invalid-id
echo $?  # => 2
--quiet
boolean
default:"false"
No output, exit code only
  • 0 — ID is valid
  • 2 — ID is invalid
uniku validate --quiet 01HW9T2W9W9YJ3JZ1H4P4M2T8Q
This is useful in shell scripts:
if uniku validate --quiet "$ID"; then
  echo "Valid ID"
else
  echo "Invalid ID"
  exit 1
fi

JSON Output

Get structured validation results with --json:
uniku validate --json 018e5e5c-7c8a-7000-8000-000000000000
{
  "id": "018e5e5c-7c8a-7000-8000-000000000000",
  "valid": true,
  "type": "uuid"
}
For invalid IDs:
uniku validate --json invalid-id
{
  "id": "invalid-id",
  "valid": false,
  "type": null
}
--json
boolean
default:"false"
Output validation results as JSON
uniku validate --json 01HW9T2W9W9YJ3JZ1H4P4M2T8Q

Batch JSON Output

Combine --stdin and --json for structured batch results:
echo -e "018e5e5c-7c8a-7000-8000-000000000000\ninvalid-id" | uniku validate --stdin --json
[
  {
    "id": "018e5e5c-7c8a-7000-8000-000000000000",
    "valid": true,
    "type": "uuid"
  },
  {
    "id": "invalid-id",
    "valid": false,
    "type": null
  }
]
Parse with jq:
# Count valid IDs
cat ids.txt | uniku validate --stdin --json | jq '[.[] | select(.valid)] | length'

# Extract invalid IDs
cat ids.txt | uniku validate --stdin --json | jq -r '.[] | select(.valid == false) | .id'

Exit Codes

The validate command uses these exit codes:
CodeMeaning
0All IDs are valid
1Command error (missing input, invalid arguments)
2One or more IDs are invalid
Example usage:
if uniku validate "$ID"; then
  echo "Proceeding with valid ID"
else
  CODE=$?
  if [ $CODE -eq 2 ]; then
    echo "Invalid ID format"
  else
    echo "Validation error"
  fi
  exit $CODE
fi

Examples

Pipeline Validation

Validate IDs generated in a pipeline:
# Generate and validate
uniku uuid --version 7 --count 10 | uniku validate --stdin

# Generate, validate, and inspect
uniku ulid --count 5 | tee ids.txt | uniku validate --stdin

Filter Valid IDs

Use jq to filter valid IDs from batch validation:
cat ids.txt | uniku validate --stdin --json | jq -r '.[] | select(.valid) | .id'

Validation in CI/CD

Validate IDs in a CI pipeline:
#!/bin/bash
set -e

# Validate required ID
if ! uniku validate --quiet --type uuid "$UUID"; then
  echo "Error: Invalid UUID in environment variable"
  exit 1
fi

echo "UUID validation passed"

Type Detection Report

Generate a report of ID types:
cat mixed_ids.txt | uniku validate --stdin --json | jq -r '.[] | "\(.id): \(.type // "unknown")"'

Supported ID Types

The validator supports these ID formats:
TypeFormatExample
UUID8-4-4-4-12 hex with hyphens550e8400-e29b-41d4-a716-446655440000
ULID26 characters, Crockford base3201HW9T2W9W9YJ3JZ1H4P4M2T8Q
NanoidConfigurable length, URL-safeV1StGXR8_Z5jdHi6B-myT
CUID22-32 characters, starts with letterclhw9t2w9w9yj3jz1h4p4m2t
KSUID27 characters, base622HbcvN9VFGzP8jR5mK3QxW4d
Auto-detection is pattern-based. For ambiguous formats (like Nanoid), use --type to ensure correct validation.

Next Steps

Generate IDs

Generate IDs to validate

Inspect IDs

Extract metadata from valid IDs

Build docs developers (and LLMs) love