Skip to content

Keys equal

keysEqual validates that a plain object contains exactly the specified set of keys (same keys, same count).

It performs a strict structural check ensuring the value is a plain object, then validates that the object's key set matches exactly with the expected keys. If the value is not a plain object or has a different key set, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.keysEqual(expectedKeys: readonly string[])

And internally:

export const keysEqual = (expectedKeys: readonly string[]): 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.has.invalid-key Object does not contain exactly the expected set of keys

Design rationale

  • Provides a strict, predictable exact key set validation.
  • First ensures the value is a plain object, then checks key set equality.
  • Requires exact match: same keys and same count (order doesn't matter).
  • Useful for enforcing strict schemas where the exact key set is mandatory.
  • 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

keysEqual 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 a different key set, emits object.has.invalid-key.
  • If the value is a plain object with exactly the expected keys → returns an empty result set.

Examples

Valid object with exact key set

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

Object with different keys

await keysEqual(["name", "age"])({ name: "John", city: "NYC" }, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.has.invalid-key",
//       path: "$",
//       ...
//     }
//   ]

Object with extra keys

await keysEqual(["name", "age"])({ name: "John", age: 30, city: "NYC" }, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "object.has.invalid-key",
//       path: "$",
//       ...
//     }
//   ]

Array (invalid)

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