string
A string. The empty string '' is treated as a missing value.
Signature
ts
function string(msg?: Message): Schema<string>Parameters
| Parameter | Type | Description |
|---|---|---|
msg | Message | Custom message. Replaces the default messages for all errors of this schema |
Accepts
Any non-empty string. Numbers are not converted to strings — use coerce.string for that.
undefined, null and '' are treated as empty — see Empty values.
Error codes
| Code | When | Default message |
|---|---|---|
required | The value is empty | Required |
string | Not a string | Must be a string |
You can change the texts: see Messages & i18n.
Notes
A whitespace-only string ' ' passes. To reject it: pipe(string(), trim(), minLength(1)).
To allow an empty string as a value: optional(string(), '').
Example
Required field with rules
ts
const name = pipe(string('Please enter your name'), trim(), minLength(2), maxLength(50));
validate(name, ''); // Please enter your name
validate(name, ' Ann '); // valid, data: 'Ann'See also
- pipe — add rules
- coerce.string — numbers to strings
- Empty values