Skip to content

Plain object

plainObject validates that a value is a JSON-compatible plain object.

It performs a strict structural check using JavaScript's prototype chain and rejects any non-plain objects including arrays, class instances, Maps, Sets, Dates, and other complex objects. If the value is not a plain object, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.plainObject()

And internally:

export const plainObject: ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
object.not.plain-object Value is not a JSON-compatible plain object

Design rationale

  • Provides a strict, predictable plain object validation.
  • Ensures JSON compatibility by rejecting complex object types.
  • Rejects arrays, class instances, built-in objects (Map, Set, Date, etc.).
  • Accepts only objects with Object.prototype as their prototype.
  • Never coerces or normalizes — validation is explicit and opt-in.
  • Emits exactly one event per failure for clarity and composability.
  • Async-compatible and returns a readonly array of JaneEvent objects.

Invoke

plainObject runs only when explicitly included in a boundary or pipeline. It does not run automatically.

The rule activates when:

  • The value is any JavaScript value.
  • If the value is not a plain object, emits object.not.plain-object.
  • If the value is a plain object → returns an empty result set.

Examples

Valid plain object

await plainObject({ name: "John", age: 30 }, "$");
// → []

Array (invalid)

await plainObject([1, 2, 3], "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.not.plain-object",
//       path: "$",
//       ...
//     }
//   ]

Class instance (invalid)

class Person {}
await plainObject(new Person(), "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.not.plain-object",
//       path: "$",
//       ...
//     }
//   ]

Date object (invalid)

await plainObject(new Date(), "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.not.plain-object",
//       path: "$",
//       ...
//     }
//   ]