Skip to content

string ​

A string. The empty string '' is treated as a missing value.

Signature ​

ts
function string(msg?: Message): Schema<string>

Parameters ​

ParameterTypeDescription
msgMessageCustom 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 ​

CodeWhenDefault message
requiredThe value is emptyRequired
stringNot a stringMust 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 ​