Skip to content

parsePlainObject

Parses a value into a plain object. This parser uses the validatePlainObject structural validator to ensure that the input is a non-null object whose prototype is exactly Object.prototype. It then applies the normalizePlainObject normalizer to ensure that the value is safe to treat as a plain object.

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

Signature

function parsePlainObject(
    value: unknown,
    field?: string
): ParseResult<Record<string, unknown>>

Behavior

  • Accepts:
  • Objects created with an object literal.
  • Objects created using new Object().
  • Rejects:
  • Arrays..
  • Functions.
  • Class instances.
  • Objects with custom prototypes.
  • Objects with null prototype.
  • Primitives.
  • null and undefined.
  • Never mutates input.
  • Never throws.

Returns

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

Examples

parsePlainObject({ a: 1 })
// { ok: true, value: { a: 1 }, issues: [] }

parsePlainObject(Object.create(null))
// { ok: false, value: null, issues: ["Value must be a plain object"] }

Notes