string

Creates a string schema.

const Schema = v.string<TMessage>(message);

Generics

Parameters

  • message TMessage

Explanation

With string you can validate the data type of the input. 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 = v.pipe(
  v.string(),
  v.nonEmpty('Please enter your email.'),
  v.email('The email is badly formatted.'),
  v.maxLength(30, 'Your email is too long.')
);

Password schema

Schema to validate a password.

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

URL schema

Schema to validate a URL.

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

The following APIs can be combined with string.

Schemas

Methods

Actions

Utils

Contributors

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

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of morinokami
  • GitHub profile picture of jasperteo
  • 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 antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of Unique-Pixels
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of nickytonline
  • GitHub profile picture of KubaJastrz
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of dslatkin