parseNonEmptyRecord
Parses a value into a normalized non-empty record. This parser combines the validateNonEmptyRecord structural validator with the normalizeNonEmptyRecord value normalizer to ensure that the input is a non-empty plain object 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 parseNonEmptyRecord<T>(
value: unknown,
normalizeValue: (v: unknown) => T | null
): ParseResult<Record<string, T>>
Behavior
- Structurally validates that the input is:
- A plain object (
Object.prototypeornullprototype). - Non-null.
- Non-array.
- Non-exotic (no class instances).
- Containing at least one own enumerable string key.
- Applies the provided value normalizer to each property.
- Rejects the entire record if:
- The object is empty.
- 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
parseNonEmptyRecord({ a: 1 }, v => typeof v === "number" ? v : null)
// { ok: true, value: { a: 1 }, issues: [] }
parseNonEmptyRecord({}, v => v)
// { ok: false, value: null, issues: ["Value must be a non-empty plain object"] }
parseNonEmptyRecord({ a: "x" }, v => typeof v === "number" ? v : null)
// { 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, validated maps, and schema-driven records.
- Pure, side-effect-free, and safe for all runtime environments.