UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

33 lines (32 loc) 1.73 kB
import type { Entry } from "../util/entry.js"; import type { ImmutableMap, PossibleMap, PossibleStringMap } from "../util/map.js"; import type { SchemaOptions } from "./Schema.js"; import { Schema } from "./Schema.js"; /** Allowed options for `AllowSchama` */ export interface AllowSchemaOptions<K, T> extends Omit<SchemaOptions, "value"> { /** Specify correct options using a `Map` or iterable set of entries. */ allow: PossibleMap<K, T>; } /** Define a valid value from an allowed set of values. */ export declare class AllowSchema<K, T> extends Schema<K> implements Iterable<Entry<K, T>> { readonly value: K; readonly allow: ImmutableMap<K, T>; constructor(options: AllowSchemaOptions<K, T>); validate(unsafeValue?: unknown): K; /** Iterate over the the allowed options in `[key, value]` format. */ [Symbol.iterator](): Iterator<Entry<K, T>>; } /** Allowed options for `AllowStringSchama` */ export type AllowStringSchemaOptions<K extends string, T> = Omit<SchemaOptions, "value"> & { /** Specify correct options using a `Map`, iterable set of entries, or an object with string keys. */ allow: PossibleStringMap<K, T>; }; /** Define a valid string value from an allowed set of string values. */ export declare class AllowStringSchema<K extends string, T> extends AllowSchema<K, T> { constructor({ allow, ...options }: AllowStringSchemaOptions<K, T>); validator(unsafeValue?: unknown): K; } /** Valid value from an allowed set of values. */ export declare function ALLOW<K, T>(allow: PossibleMap<K, T>): AllowSchema<K, T>; /** Valid string from an allowed set of values. */ export declare function ALLOW_STRING<K extends string, T>(allow: PossibleStringMap<K, T>): AllowSchema<K, T>;