Has key¶
hasKey validates that a plain object contains a specific required own enumerable key.
It performs a strict structural check ensuring the value is a plain object, then validates that the specified key exists as an own property. If the value is not a plain object or is missing the required key, the rule emits a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.hasKey(requiredKey: string)
And internally:
export const hasKey = (requiredKey: 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 does not contain the required key |
Design rationale¶
- Provides a strict, predictable required key validation.
- First ensures the value is a plain object, then checks for key presence.
- 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¶
hasKey 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 the required key, emits
object.has.missing-key. - If the value is a plain object containing the required key → returns an empty result set.
Examples¶
Valid object with required key¶
await hasKey("name")({ name: "John", age: 30 }, "$");
// → []
Object missing required key¶
await hasKey("name")({ age: 30 }, "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.has.missing-key",
// path: "$",
// ...
// }
// ]
Array (invalid)¶
await hasKey("name")([1, 2, 3], "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.not.plain-object",
// path: "$",
// ...
// }
// ]