Skip to content

Commit 3d4345b

Browse files
authored
fix(validators): isSchemaKind no longer accepts inherited prototype keys (#14)
'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 73e2464 commit 3d4345b

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", () => {
@@ -49,3 +50,19 @@ describe("LogicSRC validators", () => {
4950
expect(result.ok).toBe(false);
5051
});
5152
});
53+
54+
describe("isSchemaKind / assertSchemaKind prototype safety", () => {
55+
it("rejects inherited Object.prototype keys", () => {
56+
for (const key of ["toString", "constructor", "valueOf", "hasOwnProperty"]) {
57+
expect(isSchemaKind(key)).toBe(false);
58+
expect(() => assertSchemaKind(key)).toThrow(/Unknown schema kind/);
59+
}
60+
});
61+
62+
it("still accepts every real schema kind", () => {
63+
for (const key of Object.keys(schemas)) {
64+
expect(isSchemaKind(key)).toBe(true);
65+
expect(assertSchemaKind(key)).toBe(key);
66+
}
67+
});
68+
});

packages/validators/src/schemas.ts

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

4343
export function isSchemaKind(value: string): value is SchemaKind {
44-
return value in schemas;
44+
return Object.hasOwn(schemas, value);
4545
}

0 commit comments

Comments
 (0)