Skip to content

Commit 04a1cf7

Browse files
committed
feat: add input validation to validateAgentContent for non-string input
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent d5ea69e commit 04a1cf7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/paths.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,14 @@ export function parseFrontmatter(content) {
270270
*
271271
* @param {string} content - The agent file content to validate
272272
* @returns {{ valid: boolean, error?: string }} Validation result with optional error message
273+
* @throws {TypeError} If content is not a string
273274
*/
274275
export function validateAgentContent(content) {
276+
if (typeof content !== "string") {
277+
throw new TypeError(
278+
`validateAgentContent: content must be a string, got ${content === null ? "null" : typeof content}`,
279+
)
280+
}
275281
// Check minimum length
276282
if (content.length < MIN_CONTENT_LENGTH) {
277283
return {

tests/paths.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,48 @@ This is a test agent that handles various tasks.
791791
const result = validateAgentContent(content)
792792
expect(result.valid).toBe(true)
793793
})
794+
795+
it("should throw TypeError for null input", () => {
796+
expect(() => validateAgentContent(null as unknown as string)).toThrow(TypeError)
797+
expect(() => validateAgentContent(null as unknown as string)).toThrow(
798+
"validateAgentContent: content must be a string, got null",
799+
)
800+
})
801+
802+
it("should throw TypeError for undefined input", () => {
803+
expect(() => validateAgentContent(undefined as unknown as string)).toThrow(TypeError)
804+
expect(() => validateAgentContent(undefined as unknown as string)).toThrow(
805+
"validateAgentContent: content must be a string, got undefined",
806+
)
807+
})
808+
809+
it("should throw TypeError for number input", () => {
810+
expect(() => validateAgentContent(123 as unknown as string)).toThrow(TypeError)
811+
expect(() => validateAgentContent(123 as unknown as string)).toThrow(
812+
"validateAgentContent: content must be a string, got number",
813+
)
814+
})
815+
816+
it("should throw TypeError for object input", () => {
817+
expect(() => validateAgentContent({} as unknown as string)).toThrow(TypeError)
818+
expect(() => validateAgentContent({} as unknown as string)).toThrow(
819+
"validateAgentContent: content must be a string, got object",
820+
)
821+
})
822+
823+
it("should throw TypeError for array input", () => {
824+
expect(() => validateAgentContent([] as unknown as string)).toThrow(TypeError)
825+
expect(() => validateAgentContent([] as unknown as string)).toThrow(
826+
"validateAgentContent: content must be a string, got object",
827+
)
828+
})
829+
830+
it("should throw TypeError for boolean input", () => {
831+
expect(() => validateAgentContent(true as unknown as string)).toThrow(TypeError)
832+
expect(() => validateAgentContent(true as unknown as string)).toThrow(
833+
"validateAgentContent: content must be a string, got boolean",
834+
)
835+
})
794836
})
795837

796838
describe("checkVersionCompatibility", () => {

0 commit comments

Comments
 (0)