Skip to content

Non-empty object

nonEmptyObject validates that a plain object contains at least one own enumerable key.

It performs a strict structural check ensuring the value is a plain object, then validates that it has at least one key. If the value is not a plain object or is empty, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.nonEmptyObject()

And internally:

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

Events

Event code Description
object.not.plain-object Value is not a JSON-compatible plain object
object.is.empty Object contains zero own enumerable keys

Design rationale

  • Provides a strict, predictable non-empty object validation.
  • First ensures the value is a plain object, then checks for emptiness.
  • Counts only own enumerable keys using Object.keys().
  • 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

nonEmptyObject 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 but empty, emits object.is.empty.
  • If the value is a non-empty plain object → returns an empty result set.

Examples

Valid non-empty object

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

Empty object

await nonEmptyObject({}, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.is.empty",
//       path: "$",
//       ...
//     }
//   ]

Array (invalid)

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