Skip to content

Commit

Permalink
fix: force yaml loading to confirm to json-compatible types
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Oct 11, 2021
1 parent f6886ab commit 402904c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/parsers/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { ParserError } = require("../util/errors");
const yaml = require("js-yaml");
const { JSON_SCHEMA } = require("js-yaml");

module.exports = {
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ module.exports = {

if (typeof data === "string") {
try {
return yaml.load(data);
return yaml.load(data, { schema: JSON_SCHEMA });
}
catch (e) {
throw new ParserError(e.message, file.url);
Expand Down
22 changes: 22 additions & 0 deletions test/specs/date-strings/date-strings.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const { expect } = require("chai");
const $RefParser = require("../../..");
const helper = require("../../utils/helper");
const path = require("../../utils/path");
const parsedSchema = require("./parsed");

describe("Schema with date strings", () => {
it("should parse successfully", async () => {
let parser = new $RefParser();
const schema = await parser.parse(path.rel("specs/date-strings/date-strings.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema.schema);
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/date-strings/date-strings.yaml")]);
});

it("should resolve successfully", helper.testResolve(
path.rel("specs/date-strings/date-strings.yaml"),
path.abs("specs/date-strings/date-strings.yaml"), parsedSchema.schema,
));
});
6 changes: 6 additions & 0 deletions test/specs/date-strings/date-strings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: Date Strings
type: object
properties:
name:
description: 2015-04-22T10:03:19.323-07:00
type: string
14 changes: 14 additions & 0 deletions test/specs/date-strings/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

module.exports = {
schema: {
title: "Date Strings",
type: "object",
properties: {
name: {
description: "2015-04-22T10:03:19.323-07:00",
type: "string"
}
}
}
};

0 comments on commit 402904c

Please sign in to comment.