shelving
Version:
Toolkit for using data in JavaScript.
33 lines (32 loc) • 1.43 kB
TypeScript
import { type ImmutableArray } from "../util/array.js";
import type { SchemaOptions } from "./Schema.js";
import { Schema } from "./Schema.js";
/**
* Set of options for a `ChoiceSchema` can be either:
* - Dictionary of string options in `{ key: title }` format.
*/
export type ChoiceOptions<K extends string> = {
readonly [KK in K]: string;
};
/**
* Things that can be converted to a choice options dictionary.
* - Array of string options in `[key]` format (`key` will be used as the `title` too).
*/
export type PossibleChoiceOptions<K extends string> = ImmutableArray<K> | ChoiceOptions<K>;
/** Allowed options for `ChoiceSchema` */
export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
/** Specify correct options using a dictionary of entries. */
readonly options: PossibleChoiceOptions<K>;
/** Default option for the value. */
readonly value?: K;
}
/** Choose from an allowed set of values. */
export declare class ChoiceSchema<K extends string> extends Schema<K> {
readonly value: K | undefined;
readonly options: ChoiceOptions<K>;
constructor({ one, title, placeholder, options, value, ...rest }: ChoiceSchemaOptions<K>);
validate(unsafeValue?: unknown): K;
format(value: K): string;
}
/** Choose from an allowed set of values. */
export declare function CHOICE<K extends string>(options: PossibleChoiceOptions<K>): ChoiceSchema<K>;