Skip to content
Open
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
15 changes: 9 additions & 6 deletions lib/parsers/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { JSON_SCHEMA, load } from "js-yaml";
import type { FileInfo, Plugin } from "../types/index.js";
import { ParserError } from "../util/errors.js";
import yaml from "js-yaml";
import { JSON_SCHEMA } from "js-yaml";
import type { FileInfo } from "../types/index.js";
import type { Plugin } from "../types/index.js";

export default {
/**
Expand Down Expand Up @@ -39,12 +37,17 @@ export default {
}

if (typeof data === "string") {
// Handle empty strings - js-yaml 5.x throws an error for empty input
if (data.trim() === "") {
return undefined;
}

try {
return yaml.load(data, { schema: JSON_SCHEMA });
return load(data, { schema: JSON_SCHEMA });
} catch {
try {
// fallback to non JSON_SCHEMA
return yaml.load(data);
return load(data);
} catch (e: any) {
throw new ParserError(e?.message || "Parser Error", file.url);
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@
"@types/json-schema": "^7.0.15"
},
"dependencies": {
"js-yaml": "^4.2.0"
"js-yaml": "^5.1.0"
},
"devDependencies": {
"@eslint/compat": "^2.1.0",
"@eslint/js": "^10.0.1",
"@types/eslint": "^9.6.1",
"@types/js-yaml": "^4.0.9",
"@types/json-schema": "^7.0.15",
"@types/node": "^25.9.3",
"@typescript-eslint/eslint-plugin": "^8.61.0",
Expand Down
18 changes: 5 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/specs/bundle-null-value/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const bundledSchema = {
Pet: {
example: {
breed: "dog",
name: null,
name: "",
},
properties: {
breed: {
Expand Down
8 changes: 4 additions & 4 deletions test/specs/invalid/invalid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("Invalid syntax", () => {
name: ParserError.name,
message: (message: any) =>
message.includes(
"invalid.yaml: incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line (1:1)",
"invalid.yaml: unexpected end of the stream within a flow collection (2:1)",
),
path: [],
source: (message: any) => message.endsWith("test/specs/invalid/invalid.yaml"),
Expand Down Expand Up @@ -269,7 +269,7 @@ describe("Invalid syntax", () => {

// Because the JSON and YAML parsers were disabled, the invalid YAML file got parsed as plain text
expect(schema).to.deep.equal({
foo: ":\n",
foo: "{ invalid yaml: [\n",
});
});

Expand Down Expand Up @@ -319,7 +319,7 @@ describe("Invalid syntax", () => {
name: ParserError.name,
message: (message: any) =>
message.includes(
"invalid.yaml: incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line (1:1)",
"invalid.yaml: unexpected end of the stream within a flow collection (2:1)",
),
path: ["foo"],
// source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
Expand Down Expand Up @@ -397,7 +397,7 @@ describe("Invalid syntax", () => {
{ foo: { $ref: path.rel("test/specs/invalid/invalid.yaml") } },
{ continueOnError: true, parse: { yaml: false, json: false } },
);
expect(result).to.deep.equal({ foo: ":\n" });
expect(result).to.deep.equal({ foo: "{ invalid yaml: [\n" });
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/specs/invalid/invalid.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
:
{ invalid yaml: [
10 changes: 5 additions & 5 deletions test/specs/working-directory/nested/working-dir.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fsp from "fs/promises";
import { load } from "js-yaml";
import { describe, expect, it } from "vitest";
import $RefParser from "../../../../lib";
import path from "../../../utils/path";
import yaml from "js-yaml";
import fsp from "fs/promises";
import helper from "../../../utils/helper";
import { JSONParserError } from "../../../../lib/util/errors";
import helper from "../../../utils/helper";
import path from "../../../utils/path";

describe("Working directory", () => {
it("should parse using the relative directory of the resolved file", async () => {
Expand All @@ -16,7 +16,7 @@ describe("Working directory", () => {
const parser = new $RefParser();
const schema = path.rel("test/specs/working-directory/api/example.yaml");
const contents = await fsp.readFile(schema, "utf-8");
const parsed = await yaml.load(contents);
const parsed = await load(contents);
try {
await parser.dereference(parsed);
helper.shouldNotGetCalled();
Expand Down