Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/validators/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { parseDocument, validate } from "./index.js";
import { assertSchemaKind, parseDocument, schemas, validate } from "./index.js";
import { isSchemaKind } from "./schemas.js";

describe("LogicSRC validators", () => {
it("validates the task fixture", () => {
Expand All @@ -26,3 +27,19 @@ describe("LogicSRC validators", () => {
expect(result.ok).toBe(false);
});
});

describe("isSchemaKind / assertSchemaKind prototype safety", () => {
it("rejects inherited Object.prototype keys", () => {
for (const key of ["toString", "constructor", "valueOf", "hasOwnProperty"]) {
expect(isSchemaKind(key)).toBe(false);
expect(() => assertSchemaKind(key)).toThrow(/Unknown schema kind/);
}
});

it("still accepts every real schema kind", () => {
for (const key of Object.keys(schemas)) {
expect(isSchemaKind(key)).toBe(true);
expect(assertSchemaKind(key)).toBe(key);
}
});
});
2 changes: 1 addition & 1 deletion packages/validators/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export const schemas = {
export type SchemaKind = keyof typeof schemas;

export function isSchemaKind(value: string): value is SchemaKind {
return value in schemas;
return Object.hasOwn(schemas, value);
}
Loading