Skip to content

parseValueOf

Parses a value into a valid member of the values of a given object or enum. This parser uses the validateValueOf semantic validator to ensure that the provided value appears among the object's own enumerable values.

The parser never throws and never mutates input. It returns a structured ParseResult<T[keyof T]> describing success or failure.

Signature

function parseValueOf<T extends Record<PropertyKey, unknown>>(
    value: unknown,
    obj: T
): ParseResult<T[keyof T]>

Behavior

  • Accepts values that appear among the object's own enumerable values.
  • Works with:
  • Plain objects.
  • Record-like structures.
  • TypeScript enums (string, numeric, or mixed).
  • Rejects:
  • Values not present in the object.
  • Invalid value types.
  • Non-object targets.
  • Never mutates input.
  • Never throws.

Returns

  • ok: true: And the validated value when parsing succeeds.
  • ok: false, value: null: And a list of issues when parsing fails.

Examples

parseValueOf(2, { a: 1, b: 2 })
// { ok: true, value: 2, issues: [] }

parseValueOf(99, { a: 1, b: 2 })
// { ok: false, value: null, issues: ["Value is not one of the allowed values of the provided object"] }

enum Colors { Red = "red", Blue = "blue" }
parseValueOf("red", Colors)
// { ok: true, value: "red", issues: [] }

Notes

  • Useful for validating enum values, configuration options, and discriminated union members.
  • This parser is pure, side-effect-free, and safe for all runtime environments.