Skip to content

Max keys

maxKeys validates that a plain object contains at most a specified maximum number of own enumerable keys.

It performs a strict structural check ensuring the value is a plain object, then validates that it has no more than the allowed number of keys. If the value is not a plain object or has too many keys, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.maxKeys(maximum: number)

And internally:

export const maxKeys = (maximum: number): 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.too.many-keys Object contains more than the maximum allowed keys

Design rationale

  • Provides a strict, predictable maximum key count validation.
  • First ensures the value is a plain object, then checks key count.
  • 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

maxKeys 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 has more than the maximum keys, emits object.too.many-keys.
  • If the value is a plain object with at most the maximum keys → returns an empty result set.

Examples

Valid object within maximum keys

await maxKeys(3)({ name: "John", age: 30, city: "NYC" }, "$");
// → []

Object with too many keys

await maxKeys(2)({ name: "John", age: 30, city: "NYC" }, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.too.many-keys",
//       path: "$",
//       ...
//     }
//   ]

Array (invalid)

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