@respeecher/respeecher-js
Version:
[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Frespeecher%2Frespeecher-js) [ • 1.27 kB
JavaScript
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();
}