Enum
Validates that a value is one of a fixed set of literals of the same primitive type (string, number, bigint, or
boolean). The result is typed as a union of those values.
Example
Section titled “Example”import * as p from '@vbudovski/paseri';
const schema = p.enum('red', 'green', 'blue');const data = 'red';
const result = schema.safeParse(data);if (result.ok) { // result.value typed as `'red' | 'green' | 'blue'`.}Playground
Section titled “Playground”Methods
Section titled “Methods”extract
Section titled “extract”Returns a new enum schema containing only the listed values. Each argument must be a value in the original set, or the call fails to type-check.
import * as p from '@vbudovski/paseri';
const Color = p.enum('red', 'green', 'blue');const Primary = Color.extract('red', 'green');/*The resulting schema will be:p.enum('red', 'green');*/exclude
Section titled “exclude”Returns a new enum schema with the listed values removed. Each argument must be a value in the original set, or the call fails to type-check.
import * as p from '@vbudovski/paseri';
const Color = p.enum('red', 'green', 'blue');const NonPrimary = Color.exclude('red');/*The resulting schema will be:p.enum('green', 'blue');*/