ChoiceSchema.validate()method
Validate an unknown value as one of the allowed choices.
validate(unsafeValue: unknown = this.value): O
new ChoiceSchema<O, I>({ one = "choice", title = "Choice", placeholder = `No ${one}`, options, value, ...rest }: ChoiceSchemaOptions<O, I>)| Param | Type | |
|---|---|---|
rest | ChoiceSchemaOptions<O, I> | Options for ChoiceSchema. required |
.options | PossibleChoiceOptions<O> | Specify correct options using a dictionary of entries. required readonly |
.value | OI | Default option for the value. readonly |
| Return | |
|---|---|
ChoiceSchema<O, I> | Schema that validates a value against a fixed set of allowed string choices. |
| Property | Type | |
|---|---|---|
.options | ChoiceOptions<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.
options, otherwise it is rejected.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).
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.
const schema = new ChoiceSchema({ options: { yes: "Yes", no: "No" } });
schema.validate("yes"); // "yes"Validate an unknown value as one of the allowed choices.
validate(unsafeValue: unknown = this.value): O