Skip to content

Commit 6abc3ce

Browse files
committed
fix(validators): isSchemaKind no longer accepts inherited prototype keys
'value in schemas' walks the prototype chain, so inherited keys like "toString" passed assertSchemaKind and then crashed ajv.compile with a misleading 'schema must be object or boolean' error (e.g. via the CLI). Use Object.hasOwn for an own-property check. Adds unit tests rejecting prototype keys and asserting every real schema kind still passes. Fixes #13
1 parent 850cf5e commit 6abc3ce

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/validators/src/index.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { readFileSync } from "node:fs";
22
import { dirname, resolve } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { describe, expect, it } from "vitest";
5-
import { parseDocument, validate } from "./index.js";
5+
import { assertSchemaKind, parseDocument, schemas, validate } from "./index.js";
6+
import { isSchemaKind } from "./schemas.js";
67

78
describe("LogicSRC validators", () => {
89
it("validates the task fixture", () => {
@@ -26,3 +27,19 @@ describe("LogicSRC validators", () => {
2627
expect(result.ok).toBe(false);
2728
});
2829
});
30+
31+
describe("isSchemaKind / assertSchemaKind prototype safety", () => {
32+
it("rejects inherited Object.prototype keys", () => {
33+
for (const key of ["toString", "constructor", "valueOf", "hasOwnProperty"]) {
34+
expect(isSchemaKind(key)).toBe(false);
35+
expect(() => assertSchemaKind(key)).toThrow(/Unknown schema kind/);
36+
}
37+
});
38+
39+
it("still accepts every real schema kind", () => {
40+
for (const key of Object.keys(schemas)) {
41+
expect(isSchemaKind(key)).toBe(true);
42+
expect(assertSchemaKind(key)).toBe(key);
43+
}
44+
});
45+
});

packages/validators/src/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export const schemas = {
2727
export type SchemaKind = keyof typeof schemas;
2828

2929
export function isSchemaKind(value: string): value is SchemaKind {
30-
return value in schemas;
30+
return Object.hasOwn(schemas, value);
3131
}

0 commit comments

Comments
 (0)