ChoiceSchemaclass

new ChoiceSchema<O, I>({ one = "choice", title = "Choice", placeholder = `No ${one}`, options, value, ...rest }: ChoiceSchemaOptions<O, I>)
ParamType
restChoiceSchemaOptions<O, I>
Options for ChoiceSchema. required
    .optionsPossibleChoiceOptions<O>
Specify correct options using a dictionary of entries. required readonly
    .valueO
I
Default option for the value. readonly
Return
ChoiceSchema<O, I>
Schema that validates a value against a fixed set of allowed string choices.
PropertyType
.optionsChoiceOptions<O>
Dictionary of allowed options for a ChoiceSchema in { key: title } format. required readonly

Schema that validates a value against a fixed set of allowed string choices.

  • The input must be one of the keys in options, otherwise it is rejected.
  • Each choice has a human-readable title used by format().

Validates that a value is one of a known set of options — designed to power a <select> field in HTML. The validated type is the union of the option keys.

CHOICE is the sugar factory that builds a ChoiceSchema from either an array of strings (keys and labels are the same) or an object (keys are the validated values, object values are display labels).

Usage

ts
import { CHOICE } from "shelving/schema";

// Array form — keys and labels are the same.
const STATUS = CHOICE(["draft", "published", "archived"] as const);
STATUS.validate("published"); // "published"
STATUS.validate("deleted");   // throws "Unknown value"

// Object form — keys are validated values, object values are display labels.
const Priority = CHOICE({ low: "Low priority", high: "High priority" });

ChoiceSchema is iterable and exposes .keys() and .entries() for building select menus. It does not implicitly default to the first option; pass value if you want a preselected choice.

Examples

const schema = new ChoiceSchema({ options: { yes: "Yes", no: "No" } });
 schema.validate("yes"); // "yes"

Methods

Go

ChoiceSchema.validate()method

Validate an unknown value as one of the allowed choices.

validate(unsafeValue: unknown = this.value): O