UNPKG

@respeecher/respeecher-js

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Frespeecher%2Frespeecher-js) [![npm shield](

36 lines (35 loc) 1.27 kB
import { SchemaType } from "../../Schema.mjs"; import { createIdentitySchemaCreator } from "../../utils/createIdentitySchemaCreator.mjs"; import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.mjs"; export function enum_(values) { const validValues = new Set(values); const schemaCreator = createIdentitySchemaCreator(SchemaType.ENUM, (value, { allowUnrecognizedEnumValues, breadcrumbsPrefix = [] } = {}) => { if (typeof value !== "string") { return { ok: false, errors: [ { path: breadcrumbsPrefix, message: getErrorMessageForIncorrectType(value, "string"), }, ], }; } if (!validValues.has(value) && !allowUnrecognizedEnumValues) { return { ok: false, errors: [ { path: breadcrumbsPrefix, message: getErrorMessageForIncorrectType(value, "enum"), }, ], }; } return { ok: true, value: value, }; }); return schemaCreator(); }