Skip to content

Commit 402904c

Browse files
committed
fix: force yaml loading to confirm to json-compatible types
1 parent f6886ab commit 402904c

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

lib/parsers/yaml.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { ParserError } = require("../util/errors");
44
const yaml = require("js-yaml");
5+
const { JSON_SCHEMA } = require("js-yaml");
56

67
module.exports = {
78
/**
@@ -45,7 +46,7 @@ module.exports = {
4546

4647
if (typeof data === "string") {
4748
try {
48-
return yaml.load(data);
49+
return yaml.load(data, { schema: JSON_SCHEMA });
4950
}
5051
catch (e) {
5152
throw new ParserError(e.message, file.url);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
const $RefParser = require("../../..");
5+
const helper = require("../../utils/helper");
6+
const path = require("../../utils/path");
7+
const parsedSchema = require("./parsed");
8+
9+
describe("Schema with date strings", () => {
10+
it("should parse successfully", async () => {
11+
let parser = new $RefParser();
12+
const schema = await parser.parse(path.rel("specs/date-strings/date-strings.yaml"));
13+
expect(schema).to.equal(parser.schema);
14+
expect(schema).to.deep.equal(parsedSchema.schema);
15+
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/date-strings/date-strings.yaml")]);
16+
});
17+
18+
it("should resolve successfully", helper.testResolve(
19+
path.rel("specs/date-strings/date-strings.yaml"),
20+
path.abs("specs/date-strings/date-strings.yaml"), parsedSchema.schema,
21+
));
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
title: Date Strings
2+
type: object
3+
properties:
4+
name:
5+
description: 2015-04-22T10:03:19.323-07:00
6+
type: string

test/specs/date-strings/parsed.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
module.exports = {
4+
schema: {
5+
title: "Date Strings",
6+
type: "object",
7+
properties: {
8+
name: {
9+
description: "2015-04-22T10:03:19.323-07:00",
10+
type: "string"
11+
}
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)