UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

31 lines (30 loc) 1.66 kB
import type { Validator } from "../util/validate.js"; /** Options allowed by a `Schema` instance. */ export type SchemaOptions = { /** Title of the schema, e.g. for using as the title of a corresponding field. */ readonly title?: string | undefined; /** Description of the schema, e.g. for using as a description in a corresponding field. */ readonly description?: string | undefined; /** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */ readonly placeholder?: string | undefined; /** Default value for the schema if `validate()` is called with an `undefined` value. */ readonly value?: unknown; }; /** * Schema is an object instance with a `validate()` method. * - Type `T` represents the type of value `validate()` returns. * - `validate()` returns `Invalid` if value was not valid. */ export declare abstract class Schema<T = unknown> implements Validator<T> { /** Title of the schema, e.g. for using as the title of a corresponding field. */ readonly title: string | undefined; /** Description of the schema, e.g. for using as a description in a corresponding field. */ readonly description: string | undefined; /** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */ readonly placeholder: string | undefined; /** Default value for the schema if `validate()` is called with an `undefined` value. */ readonly value: unknown; constructor({ title, description, placeholder, value }: SchemaOptions); /** Every schema must implement a `validate()` method. */ abstract validate(unsafeValue: unknown): T; }