Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force YAML loading to confirm to JSON-compatible types #247

Merged
merged 2 commits into from
May 11, 2022
Merged
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
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"
}
}
}
};