string
Creates a string schema.
const Schema = v.string<TMessage>(message);
Generics
TMessageextends ErrorMessage<StringIssue> | undefined
Parameters
messageTMessage
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
SchemaStringSchema<TMessage>
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.')
);
Related
The following APIs can be combined with string.
Schemas
array, exactOptional, intersect, lazy, looseObject, looseTuple, map, nonNullable, nonNullish, nonOptional, nullable, nullish, object, objectWithRest, optional, record, set, strictObject, strictTuple, tuple, tupleWithRest, undefinedable, union
Methods
assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser
Actions
base64, bic, brand, bytes, codePoints, creditCard, cuid2, check, decimal, description, digits, domain, email, emoji, empty, endsWith, excludes, flavor, graphemes, gtValue, guard, hash, hexadecimal, hexColor, imei, includes, ip, ipv4, ipv6, isbn, isrc, isoDate, isoDateTime, isoDateTimeSecond, isoTime, isoTimeSecond, isoTimestamp, isoWeek, ksuid, length, ltValue, mac, mac48, mac64, maxBytes, maxCodePoints, maxGraphemes, maxLength, maxValue, maxWords, metadata, minBytes, minCodePoints, minGraphemes, minLength, minValue, minWords, nanoid, nonEmpty, notBytes, notCodePoints, notEntries, notGraphemes, notLength, notValue, notValues, notWords, octal, parseJson, rawCheck, rawTransform, readonly, regex, rfcEmail, slug, startsWith, stringifyJson, title, toBigint, toBoolean, toCamelCase, toDate, toKebabCase, toLowerCase, toMaxValue, toMinValue, toNumber, toPascalCase, toSnakeCase, toUpperCase, transform, trim, trimEnd, trimStart, ulid, url, uuid, value, values, words
Utils
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/string/