Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

String

String scalar type for text data with auto-generation, validation, and native type options

Basic Usage

import { s } from "viborm";

s.string(); // Required string
s.string().nullable(); // string | null
s.string().array(); // string[] (native on PG, JSON on MySQL/SQLite)

Common modifiers (.nullable(), .unique(), .default(), .map(), .schema()) are covered in the modifier reference. To override the column type (varchar(n), uuid, citext, …), see Native Types.

IDs and Auto-Generation

.id() marks the field as the primary key and auto-generates a ULID by default:

s.string().id(); // PK, auto-generated ULID: "01ARZ3NDEKTSV4RRFFQ69G5FAV"
s.string().id("user"); // Prefixed: "user-01ARZ3NDEKTSV4RRFFQ69G5FAV"

Chain a generator to pick a different format:

s.string().id().uuid(); // UUIDv4: "550e8400-e29b-41d4-a716-446655440000"
s.string().id().ulid(); // ULID: "01ARZ3NDEKTSV4RRFFQ69G5FAV" (sortable, the default)
s.string().id().nanoid(); // NanoID: "V1StGXR8_Z5jdHi6B-myT"
s.string().id().nanoid(10); // NanoID with custom length
s.string().id().cuid(); // CUID: "cjld2cjxh0000qzrmn831i7rn"

Every generator accepts a prefix — .ulid("user") produces "user-01ARZ...". For .nanoid(), the prefix is the second argument: .nanoid(21, "user").

Custom Validation

.schema() accepts any Standard Schema. Valibot actions like email() are not schemas on their own — wrap them in pipe():

import { email, maxLength, minLength, pipe, regex, string } from "valibot";

s.string().schema(pipe(string(), email()));
s.string().schema(pipe(string(), minLength(3), maxLength(100)));
s.string().schema(pipe(string(), regex(/^[a-z]+$/)));

Examples

import { email, pipe, string, minLength, maxLength, regex } from "valibot";

// Email with validation
const userEmail = s.string()
  .unique()
  .schema(pipe(string(), email()));

// Username with constraints
const username = s.string()
  .unique()
  .schema(pipe(string(), minLength(3), maxLength(30), regex(/^[a-z0-9_]+$/)));

// Primary key with ULID
const id = s.string().id().ulid();

// Optional bio
const bio = s.string().nullable().default(null);

// Tags array (native array on PostgreSQL)
const tags = s.string().array().default([]);

Was this page helpful?