Heap.Enum
Declares a field that can take on any value from the specified Typescript Enum.
Usage / Signature
import { Heap } from '@app/heap'
Heap.Enum(targetEnum, options)
Enums are not recommended for use in Typescript, but are supported for backward compatibility. Therefore, this schema type is also not recommended if you are not already using Enums in your data model. Consider Heap.Union as an alternative.
Arguments
-
<b id="targetEnum">targetEnum</b>:
Enum object
<b style="color:red">*</b>
The Enum declared in TypeScript code that this field should conform to. -
<b id="options">options</b>:
{
default}
An object with additional optional parameters for the field.- <b id="default">default</b>:
array
The default value for this field. For more information on how default values work, see Guide / Heap / Default Values
- <b id="default">default</b>:
Return Value
A JSON schema anyOf with string and/or numeric literals corresponding to all possible values of targetEnum.
Examples
Simple Enum with String Values
enum CustomerStatus {
New = 'New',
Returning = 'Returning',
Lost = 'Lost',
}
const customers = Heap.Table('customers', {
age: Heap.Enum(CustomerStatus, { default: CustomerStatus.New }),
})
Mixed Enum (Strings and Numbers) Based on Enum-like Object
const enumLikePossibleGrades = {
bad: 1,
better: 3,
good: 4,
great: 'excellent!',
}
const pupils = Heap.Table('pupils', {
grade: Heap.Enum(enumLikePossibleGrades),
})
await pupils.create(ctx, { grade: 4 })
await pupils.create(ctx, { grade: 'excellent!' })