StringSchemaclass

new StringSchema({
		one = "string",
		min = 0,
		max = Number.POSITIVE_INFINITY,
		value = "",
		rows = 1,
		match,
		case: _case,
		input = "text",
		...options
	}: StringSchemaOptions)
ParamType
optionsStringSchemaOptions
Options for StringSchema. required
    .valuestring
Default string value used when the input is undefined. Defaults to "" readonly
    .minnumber
Minimum allowed character length. Defaults to 0 readonly
    .maxnumber
Maximum allowed character length. Defaults to Number.POSITIVE_INFINITY readonly
    .rowsnumber
Number of rows; more than one enables multiline sanitization. Defaults to 1 readonly
    .matchRegExp
Regular expression the sanitized string must match. readonly
    .case"upper"
"lower"
Force the result to "upper" or "lower" case. readonly
    .inputStringInputType
HTML <input /> type="" hint for downstream UIs. Defaults to "text" readonly
Return
StringSchema
Schema that defines a valid string.
PropertyType
.inputStringInputType
type="" prop for HTML <input /> tags that are relevant for strings. required readonly
.minnumber
required readonly
.maxnumber
required readonly
.rowsnumber
required readonly
.matchRegExp
readonly
.case"upper"
"lower"
readonly

Schema that defines a valid string.

  • Numbers are coerced to strings; all other non-string values are rejected.
  • The value is sanitized (and optionally case-folded), then checked against match, min, and max.

Validates a value into a string. Numbers are coerced to their string form, and a missing value falls back to the schema's value default (an empty string by default). Construct a StringSchema directly to add constraints like min, max, and match.

STRING is the ready-made sugar instance for an unconstrained string; REQUIRED_STRING is a StringSchema with min: 1, so an empty string fails validation.

Usage

Sugar instances

To save creating a new instance of StringSchema for trivial uses, you can use the STRING or REQUIRED_STRING sugar instances instead. The module also ships TITLE and NAME (1–100 characters) and their nullable variants NULLABLE_TITLE and NULLABLE_NAME.

ts
import { STRING, REQUIRED_STRING } from "shelving/schema";

STRING.validate("hello");        // "hello"
STRING.validate(42);             // "42"   (numbers coerced)
STRING.validate(undefined);      // ""     (default)
REQUIRED_STRING.validate("");    // throws "Required"

Custom string schemas

Construct a StringSchema to apply length and pattern constraints:

ts
import { StringSchema } from "shelving/schema";

const USERNAME = new StringSchema({ title: "Username", min: 3, max: 20, match: /^[a-z0-9_]+$/ });
USERNAME.validate("alice_99");   // "alice_99"
USERNAME.validate("al");         // throws "Minimum 3 characters"
USERNAME.validate("ALICE");      // throws "Invalid format"

Examples

const schema = new StringSchema({ min: 2, max: 6, match: /^[a-z0-9]+$/ });
 schema.validate("def"); // Returns "def"
 schema.validate(1234); // Returns "1234" (numbers are coerced)
 schema.validate("j"); // Throws "Minimum 2 characters"

Methods

Go

StringSchema.sanitize()method

Sanitize the string by removing unwanted characters.

sanitize(str: string): string