Skip to content

validateNonEmptyRecord

Validates that a value is a non-empty plain object (record).

This helper ensures that the input is:

  • A plain JavaScript object (Object.prototype or null prototype)
  • Not an array, class instance, Map, Set, or other exotic object
  • Not a boxed primitive (new String("x"), new Number(1))
  • Contains at least one own enumerable property

It never mutates input and never throws.

Signature

function validateNonEmptyRecord<K extends string = string, V = unknown>(
  value: unknown
): Record<K, V> | null

Parameters

Name Data type Description
value unknown The value to validate.

Returns

One of:

  • The original object typed as Record<K, V>, if valid.
  • null: If the value is not a non-empty plain object.

Behavior

  • Uses isNonEmptyRecord internally to perform strict type checks.
  • Accepts plain objects with at least one own enumerable property.
  • Rejects empty objects, arrays, primitives, class instances, Maps, Sets, and objects with non-standard prototypes.
  • Never throws and never mutates input.

Examples

validateNonEmptyRecord({ a: 1 })
// { a: 1 }

validateNonEmptyRecord({ key: "value", foo: true })
// { key: "value", foo: true }

validateNonEmptyRecord({})
// null

validateNonEmptyRecord([1, 2])
// null

validateNonEmptyRecord(null)
// null

Notes

  • This helper is shallow — it does not validate nested objects.
  • Ideal for validating configuration objects, API payloads, or any input where an empty object is invalid.
  • Use validateRecord if empty objects are acceptable.