UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

57 lines (56 loc) 2.72 kB
import type { SchemaOptions } from "./Schema.js"; import { Schema } from "./Schema.js"; /** `type=""` prop for HTML `<input />` tags that are relevant for strings. */ export type StringInputType = "text" | "password" | "color" | "email" | "number" | "tel" | "search" | "url"; /** Options for `StringSchema` */ export interface StringSchemaOptions extends SchemaOptions { readonly value?: string | undefined; readonly min?: number | undefined; readonly max?: number | undefined; readonly multiline?: boolean | undefined; readonly match?: RegExp | undefined; readonly case?: "upper" | "lower" | undefined; readonly input?: StringInputType | undefined; } /** * Schema that defines a valid string. * * @example * const schema = new StringSchema({ default: 'abc', required: true, min: 2, max: 6, match: /^[a-z0-9]+$/, trim: true }); * schema.validate('def'); // Returns 'def' * schema.validate('abcdefghijk'); // Returns 'abcdef' (due to max) * schema.validate(undefined); // Returns 'abc' (due to value) * schema.validate(' ghi '); // Returns 'ghi' (due to schema.trim, defaults to true) * schema.validate(1234); // Returns '1234' (numbers are converted to strings) * schema.validate('---'); // Throws 'Incorrect) * schema.validate(true); // Throws 'Must be) * schema.validate(''); // Throws Required * schema.validate('j'); // Throws 'Minimum 3 chaacters' */ export declare class StringSchema extends Schema<string> { readonly value: string; readonly input: StringInputType; readonly min: number; readonly max: number; readonly multiline: boolean; readonly match: RegExp | undefined; readonly case: "upper" | "lower" | undefined; constructor({ min, max, value, multiline, match, case: _case, input, ...options }: StringSchemaOptions); validate(unsafeValue?: unknown): string; /** Sanitize the string by removing unwanted characters. */ sanitize(str: string): string; } /** Valid string, e.g. `Hello there!` */ export declare const STRING: StringSchema; /** Valid string, `Hello there!`, with more than one character. */ export declare const REQUIRED_STRING: StringSchema; /** Title string, e.g. `Title of something` */ export declare const TITLE: StringSchema; /** Optional name string, e.g. `Title of something` or `null` */ export declare const NULLABLE_TITLE: import("./NullableSchema.js").NullableSchema<string>; /** Name string, e.g. `Name of Something` */ export declare const NAME: StringSchema; /** Optional name string, e.g. `Name of Something` or `null` */ export declare const NULLABLE_NAME: import("./NullableSchema.js").NullableSchema<string>; /** Password string. */ export declare const PASSWORD: StringSchema;