StringSchema.sanitize()method
Sanitize the string by removing unwanted characters.
sanitize(str: string): string
new StringSchema({
one = "string",
min = 0,
max = Number.POSITIVE_INFINITY,
value = "",
rows = 1,
match,
case: _case,
input = "text",
...options
}: StringSchemaOptions)| Param | Type | |
|---|---|---|
options | StringSchemaOptions | Options for StringSchema. required |
.value | string | Default string value used when the input is undefined. Defaults to "" readonly |
.min | number | Minimum allowed character length. Defaults to 0 readonly |
.max | number | Maximum allowed character length. Defaults to Number.POSITIVE_INFINITY readonly |
.rows | number | Number of rows; more than one enables multiline sanitization. Defaults to 1 readonly |
.match | RegExp | Regular expression the sanitized string must match. readonly |
.case | "upper""lower" | Force the result to "upper" or "lower" case. readonly |
.input | StringInputType | HTML <input /> type="" hint for downstream UIs. Defaults to "text" readonly |
| Return | |
|---|---|
StringSchema | Schema that defines a valid string. |
| Property | Type | |
|---|---|---|
.input | StringInputType | type="" prop for HTML <input /> tags that are relevant for strings. required readonly |
.min | number | required readonly |
.max | number | required readonly |
.rows | number | required readonly |
.match | RegExp | readonly |
.case | "upper""lower" | readonly |
Schema that defines a valid string.
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.
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.
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"Construct a StringSchema to apply length and pattern constraints:
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"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"Sanitize the string by removing unwanted characters.
sanitize(str: string): string