Skip to main content

keyof

Creates an enumeration schema that parses input as a key of the given object schema.

Example​

import { keyof, object, number } from "@sodd/core";

const reactionsSchema = object({
thumbsUp: number(),
thumbsDown: number(),
heart: number(),
});

const reactionSchema = keyof(reactionsSchema);

reactionSchema.parse("thumbsUp");
// =>
{
ok: true,
data: "thumbsUp"
}

reactionSchema.parse("foo");
// =>
{
ok: false,
issues: [{
code: "invalid_enum_value",
expected: ["thumbsUp", "thumbsDown", "heart"],
received: "foo",
path: []
}]
}