string

Creates a string schema.

// String schema with an optional pipe
const Schema = string(pipe);

// String schema with an optional message and pipe
const Schema = string(message, pipe);

Parameters

Explanation

With string you can validate the data type of the input and with pipe you can transform and validate the further details of the string. If the input is not a string, you can use message to customize the error message.

Returns

Examples

The following examples show how string can be used.

Email schema

Schema to validate an email.

const EmailSchema = string([
  minLength(1, 'Please enter your email.'),
  email('The email is badly formatted.'),
  maxLength(30, 'Your email is too long.'),
]);

Password schema

Schema to validate a password.

const PasswordSchema = string([
  minLength(8, 'Your password is too short.'),
  maxLength(30, 'Your password is too long.'),
  regex(/[a-z]/, 'Your password must contain a lowercase letter.'),
  regex(/[A-Z]/, 'Your password must contain a uppercase letter.'),
  regex(/[0-9]/, 'Your password must contain a number.'),
]);

URL schema

Schema to validate a URL.

const UrlSchema = string('A URL must be string.', [
  url('The URL is badly formatted.'),
]);

The following APIs can be combined with string.

Schemas

Methods

Transformations

Validations

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of kazizi55

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of dailydotdev
  • GitHub profile picture of ivan-mihalic
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper