Skip to content

Latest commit

 

History

History
111 lines (88 loc) · 4.43 KB

File metadata and controls

111 lines (88 loc) · 4.43 KB
title description
string
Creates a string schema.

import { Link } from '@builder.io/qwik-city'; import { Property } from '~/components';

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);

Arguments

  • message <Property {...properties.message} />
  • pipe <Property {...properties.pipe}/>

Return

  • Schema <Property {...properties.Schema} />

Examples

With string you validate the data type of the incoming data and with the pipe you can then validate all further details. In the event of an error, the message argument allows you to customize the error message.

// 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.'),
]);

// 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.'),
]);

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

Related

The following APIs can be combined with string.

Methods

brand, coerce, fallback, is, parse, safeParse, transform, unwrap

Transformations

toCustom, toCustomAsync, toLowerCase, toMaxValue, toMinValue, toTrimmed, toTrimmedEnd, toTrimmedStart, toUpperCase

Validations

bytes, cuid2, custom, customAsync, email, emoji, endsWith, equal, excludes, imei, includes, ip, ipv4, isoDate, isoDateTime, isoTime, isoTimeSecond, isoTimestamp, isoWeek, length, maxBytes, maxLength, maxValue, minBytes, minLength, minValue, notBytes, notLength, notValue, regex, startsWith, ulid, url, uuid, value

export const properties = { message: { type: [ { type: 'custom', name: 'ErrorMessage', href: '../ErrorMessage/', }, 'undefined', ], default: { type: 'string', value: 'Invalid type', }, }, pipe: { type: [ { type: 'custom', name: 'Pipe', href: '../Pipe/', generics: { withDefault: false, type: ['string'], }, }, 'undefined', ], }, Schema: { type: [ { type: 'custom', name: 'StringSchema', href: '../StringSchema/', }, ], }, };