Only keys¶
onlyKeys validates that a plain object contains only the specified allowed keys.
It performs a strict structural check ensuring the value is a plain object, then validates that all own keys are present in the allowed list. If the value is not a plain object or contains any disallowed keys, the rule emits a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.onlyKeys(allowedKeys: readonly string[])
And internally:
export const onlyKeys = (allowedKeys: 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.extra-key |
Object contains keys not in the allowed list |
Design rationale¶
- Provides a strict, predictable allowed keys validation.
- First ensures the value is a plain object, then checks key membership.
- Accepts an array of allowed keys and reports all extra keys at once.
- Useful for enforcing strict schemas where only specific keys are permitted.
- 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¶
onlyKeys 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 contains any keys not in the allowed list, emits
object.has.extra-key. - If the value is a plain object containing only allowed keys → returns an empty result set.
Examples¶
Valid object with only allowed keys¶
await onlyKeys(["name", "age"])({ name: "John", age: 30 }, "$");
// → []
Object with extra keys¶
await onlyKeys(["name", "age"])({ name: "John", age: 30, city: "NYC" }, "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.has.extra-key",
// path: "$",
// ...
// }
// ]
Array (invalid)¶
await onlyKeys(["name", "age"])([1, 2, 3], "$");
// → [
// JaneEvent{
// kind: "error",
// code: "object.not.plain-object",
// path: "$",
// ...
// }
// ]