Missing key¶
missingKey validates that a plain object contains all of the specified required own enumerable keys.
It performs a strict structural check ensuring the value is a plain object, then validates that all required keys exist as own properties. If the value is not a plain object or is missing any required keys, the rule emits a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.missingKey(requiredKeys: readonly string[])
And internally:
export const missingKey = (requiredKeys: 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.missing-key |
Object is missing one or more required keys |
Design rationale¶
- Provides a strict, predictable multiple required keys validation.
- First ensures the value is a plain object, then checks for key presence.
- Accepts an array of required keys and reports all missing keys at once.
- Uses
Object.prototype.hasOwnProperty.call()for reliable own property detection. - 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
JaneEventobjects.
Invoke¶
missingKey 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 missing any required keys, emits
object.has.missing-key. - If the value is a plain object containing all required keys → returns an empty result set.
Examples¶
Valid object with all required keys¶
await missingKey(["name", "age"])({ name: "John", age: 30, city: "NYC" }, "$");
// → []
Object missing some required keys¶
await missingKey(["name", "age"])({ name: "John" }, "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.has.missing-key",
// path: "$",
// ...
// }
// ]
Array (invalid)¶
await missingKey(["name", "age"])([1, 2, 3], "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.not.plain-object",
// path: "$",
// ...
// }
// ]