Skip to content

Commit d5ea69e

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

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/paths.d.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export interface ParseFrontmatterResult {
147147
*
148148
* @param content - The file content to parse
149149
* @returns Parse result with found status, fields, and end index
150+
* @throws {TypeError} If content is not a string
150151
*/
151152
export function parseFrontmatter(content: string): ParseFrontmatterResult
152153

src/paths.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,14 @@ export async function retryOnTransientError(fn, options = {}) {
208208
*
209209
* @param {string} content - The file content to parse
210210
* @returns {{ found: boolean, reason?: "missing" | "unclosed", fields: Record<string, string>, endIndex: number }} Parse result
211+
* @throws {TypeError} If content is not a string
211212
*/
212213
export function parseFrontmatter(content) {
214+
if (typeof content !== "string") {
215+
throw new TypeError(
216+
`parseFrontmatter: content must be a string, got ${content === null ? "null" : typeof content}`,
217+
)
218+
}
213219
// Frontmatter must start at the beginning of the file
214220
if (!content.startsWith("---")) {
215221
return { found: false, reason: "missing", fields: {}, endIndex: 0 }

tests/paths.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,48 @@ url: https://example.com
613613
const result = parseFrontmatter(content)
614614
expect(result.fields.url).toBe("https://example.com")
615615
})
616+
617+
it("should throw TypeError for null input", () => {
618+
expect(() => parseFrontmatter(null as unknown as string)).toThrow(TypeError)
619+
expect(() => parseFrontmatter(null as unknown as string)).toThrow(
620+
"parseFrontmatter: content must be a string, got null",
621+
)
622+
})
623+
624+
it("should throw TypeError for undefined input", () => {
625+
expect(() => parseFrontmatter(undefined as unknown as string)).toThrow(TypeError)
626+
expect(() => parseFrontmatter(undefined as unknown as string)).toThrow(
627+
"parseFrontmatter: content must be a string, got undefined",
628+
)
629+
})
630+
631+
it("should throw TypeError for number input", () => {
632+
expect(() => parseFrontmatter(123 as unknown as string)).toThrow(TypeError)
633+
expect(() => parseFrontmatter(123 as unknown as string)).toThrow(
634+
"parseFrontmatter: content must be a string, got number",
635+
)
636+
})
637+
638+
it("should throw TypeError for object input", () => {
639+
expect(() => parseFrontmatter({} as unknown as string)).toThrow(TypeError)
640+
expect(() => parseFrontmatter({} as unknown as string)).toThrow(
641+
"parseFrontmatter: content must be a string, got object",
642+
)
643+
})
644+
645+
it("should throw TypeError for array input", () => {
646+
expect(() => parseFrontmatter([] as unknown as string)).toThrow(TypeError)
647+
expect(() => parseFrontmatter([] as unknown as string)).toThrow(
648+
"parseFrontmatter: content must be a string, got object",
649+
)
650+
})
651+
652+
it("should throw TypeError for boolean input", () => {
653+
expect(() => parseFrontmatter(true as unknown as string)).toThrow(TypeError)
654+
expect(() => parseFrontmatter(true as unknown as string)).toThrow(
655+
"parseFrontmatter: content must be a string, got boolean",
656+
)
657+
})
616658
})
617659

618660
describe("validateAgentContent", () => {

0 commit comments

Comments
 (0)