Hyperlinkv0.8.0-beta.28

Schema

Schema.Enumfunctioneffect/Schema.ts:2877
Enum<A>

Creates a schema from a TypeScript enum object. Validates that the input is one of the enum's values.

Example (Defining a direction enum)

import { Schema } from "effect"

enum Direction {
  Up = "Up",
  Down = "Down"
}

const schema = Schema.Enum(Direction)
// accepts "Up" or "Down"
constructors
Source effect/Schema.ts:287736 lines
export interface Enum<A extends { [x: string]: string | number }>
  extends Bottom<A[keyof A], A[keyof A], never, never, SchemaAST.Enum, Enum<A>>
{
  readonly enums: A
}

/**
 * Creates a schema from a TypeScript enum object. Validates that the input is one of the enum's values.
 *
 * **Example** (Defining a direction enum)
 *
 * ```ts
 * import { Schema } from "effect"
 *
 * enum Direction {
 *   Up = "Up",
 *   Down = "Down"
 * }
 *
 * const schema = Schema.Enum(Direction)
 * // accepts "Up" or "Down"
 * ```
 *
 * @category constructors
 * @since 4.0.0
 */
export function Enum<A extends { [x: string]: string | number }>(enums: A): Enum<A> {
  return make(
    new SchemaAST.Enum(
      Object.keys(enums).filter(
        (key) => typeof enums[enums[key]] !== "number"
      ).map((key) => [key, enums[key]])
    ),
    { enums }
  )
}
Referenced by 1 symbols