Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 657 Bytes

readme.md

File metadata and controls

36 lines (28 loc) · 657 Bytes

enum

Back to root readme.md

This function uses the rulr.isEnum guard to check the input matches an enum value as shown in the example below. It should only throw rulr.InvalidEnumError.

import * as rulr from 'rulr'

enum TrafficLight {
	Red,
	Amber,
	Green,
}

const constrainToExample = rulr.object({
	required: {
		example: rulr.enum(TrafficLight),
	},
})

type Example = rulr.Static<typeof constrainToExample>
// {
//   example: TrafficLight
// }

// Valid
const example1: Example = constrainToExample({
	example: TrafficLight.Red,
})

// Invalid
const example2: Example = constrainToExample({
	example: 'red',
})