Skip to content

String

import * as p from '@vbudovski/paseri';
const schema = p.string();
const data = 'foo';
const result = schema.safeParse(data);
if (result.ok) {
// result.value typed as `string`.
}
Schema
Data
Result

Consists of at least length characters.

p.string().min(3);

Consists of at most length characters.

p.string().max(3);

Consists of exactly length characters.

p.string().length(3);

A valid email address.

p.string().email();

A valid emoji character.

p.string().emoji();

A valid UUID 🔗.

p.string().uuid();

A valid Nano ID 🔗.

p.string().nanoid();

Contains the searchString.

p.string().includes('foo');

Starts with the searchString.

p.string().startsWith('foo');

Ends with the searchString.

p.string().endsWith('foo');

A valid ISO 8601 🔗 date string YYYY-MM-DD.

p.string().date();

A valid ISO 8601 🔗 time string hh:mm:ss[.s+].

p.string().time();

You can require a fixed precision by setting the precision option.

p.string().time({ precision: 3 });
// 01:02:03.123 ✅
// 01:02:03.1234 ❌

A valid ISO 8601 🔗 UTC datetime string YYYY-MM-DDThh:mm:ss[.s+]Z.

p.string().datetime();

You can require a fixed precision by setting the precision option.

p.string().datetime({ precision: 3 });
// 2020-01-02T01:02:03.123Z ✅
// 2020-01-02T01:02:03.1234Z ❌

Non-UTC offsets are accepted by setting the offset option.

p.string().datetime({ offset: true });
// 2020-01-02T01:02:03.123Z ✅
// 2020-01-02T01:02:03.123+02:30 ✅
// 2020-01-02T01:02:03.123-0430 ✅
// 2020-01-02T01:02:03.123 ❌

Offset-less (naïve) values are accepted by setting the local option.

p.string().datetime({ local: true });
// 2020-01-02T01:02:03.123Z ✅
// 2020-01-02T01:02:03.123 ✅

A valid IPv4 🔗 or IPv6 🔗 address.

p.string().ip();
// 127.0.0.1 ✅
// ::1 ✅

The protocol version can be restricted.

p.string().ip({ version: 4 });
// 127.0.0.1 ✅
// ::1 ❌

A valid IP address range in CIDR 🔗 notation.

p.string().cidr();
// 127.0.0.0/8 ✅
// ::1/128 ✅

The protocol version can be restricted.

p.string().cidr({ version: 4 });
// 127.0.0.0/8 ✅
// ::1/128 ❌

Matches the provided regex.

p.string().regex(/^a+$/);
// aaa ✅
// aab ❌