Skip to content

parseRecord

Parses a value into a normalized record. This parser combines the validateRecord structural validator with the normalizeRecord value normalizer to ensure that the input is a record-like object with string keys and that each value can be normalized successfully.

The parser never throws and never mutates input. It returns a structured ParseResult<Record<string, T>> describing success or failure.

Signature

function parseRecord<T>(
    value: unknown,
    normalizeValue: (v: unknown) => T | null,
    field?: string
): ParseResult<Record<string, T>>

Behavior

  • Structurally validates that the input is a record-like object:
  • Non-null.
  • Non-array.
  • Own keys are strings.
  • No symbol keys.
  • Normalizes only plain objects (Object.prototype or null prototype).
  • Applies the provided value normalizer to each property.
  • Rejects the entire record if:
  • The object is not plain.
  • Any value normalizes to null.
  • Returns a new object.
  • Never throws and never mutates input.

Returns

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

Examples

parseRecord({ a: 1, b: 2 }, v => typeof v === "number" ? v : null)
// { ok: true, value: { a: 1, b: 2 }, issues: [] }

parseRecord({ a: 1, b: "x" }, v => typeof v === "number" ? v : null)
// { ok: false, value: null, issues: ["Record contains invalid structure or un-normalizable values"] }

class Foo { x = 1 }
parseRecord(new Foo(), v => v)
// { ok: false, value: null, issues: ["Record contains invalid structure or un-normalizable values"] }

Notes

  • This parser enforces both structural correctness and semantic normalization.
  • Useful for configuration objects, maps, and schema-driven record structures.
  • Pure, side-effect-free, and safe for all runtime environments.