From 402904c92c8465db788be70655c3773c6e42a051 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 11 Oct 2021 15:58:44 -0700 Subject: [PATCH] fix: force yaml loading to confirm to json-compatible types --- lib/parsers/yaml.js | 3 ++- test/specs/date-strings/date-strings.spec.js | 22 ++++++++++++++++++++ test/specs/date-strings/date-strings.yaml | 6 ++++++ test/specs/date-strings/parsed.js | 14 +++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/specs/date-strings/date-strings.spec.js create mode 100644 test/specs/date-strings/date-strings.yaml create mode 100644 test/specs/date-strings/parsed.js diff --git a/lib/parsers/yaml.js b/lib/parsers/yaml.js index 2c7dc4ea..94c4eca0 100644 --- a/lib/parsers/yaml.js +++ b/lib/parsers/yaml.js @@ -2,6 +2,7 @@ const { ParserError } = require("../util/errors"); const yaml = require("js-yaml"); +const { JSON_SCHEMA } = require("js-yaml"); module.exports = { /** @@ -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); diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js new file mode 100644 index 00000000..7de4acce --- /dev/null +++ b/test/specs/date-strings/date-strings.spec.js @@ -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, + )); +}); diff --git a/test/specs/date-strings/date-strings.yaml b/test/specs/date-strings/date-strings.yaml new file mode 100644 index 00000000..9137a651 --- /dev/null +++ b/test/specs/date-strings/date-strings.yaml @@ -0,0 +1,6 @@ +title: Date Strings +type: object +properties: + name: + description: 2015-04-22T10:03:19.323-07:00 + type: string diff --git a/test/specs/date-strings/parsed.js b/test/specs/date-strings/parsed.js new file mode 100644 index 00000000..c9228a93 --- /dev/null +++ b/test/specs/date-strings/parsed.js @@ -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" + } + } + } +};