|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const chai = require("chai"); |
| 4 | +const chaiSubset = require("chai-subset"); |
| 5 | +chai.use(chaiSubset); |
| 6 | +const { expect } = chai; |
| 7 | +const $RefParser = require("../../.."); |
| 8 | +const helper = require("../../utils/helper"); |
| 9 | +const path = require("../../utils/path"); |
| 10 | +const { InvalidPointerError, ResolverError, MissingPointerError } = require("../../../lib/util/errors"); |
| 11 | + |
| 12 | +describe("Report correct error source and path for", () => { |
| 13 | + it("schema with broken reference", async () => { |
| 14 | + const parser = new $RefParser(); |
| 15 | + try { |
| 16 | + await parser.dereference({ foo: { bar: { $ref: "I do not exist" }}}, { continueOnError: true }); |
| 17 | + helper.shouldNotGetCalled(); |
| 18 | + } |
| 19 | + catch (err) { |
| 20 | + expect(err.errors).to.containSubset([ |
| 21 | + { |
| 22 | + name: ResolverError.name, |
| 23 | + source: source => typeof source === "string", |
| 24 | + path: ["foo", "bar"], |
| 25 | + message: message => typeof message === "string", |
| 26 | + }, |
| 27 | + ]); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + it("schema with a local reference pointing at property with broken external reference", async () => { |
| 32 | + const parser = new $RefParser(); |
| 33 | + try { |
| 34 | + await parser.dereference(path.abs("specs/error-source/broken-external.json"), { continueOnError: true }); |
| 35 | + helper.shouldNotGetCalled(); |
| 36 | + } |
| 37 | + catch (err) { |
| 38 | + expect(err.errors).to.containSubset([ |
| 39 | + { |
| 40 | + name: ResolverError.name, |
| 41 | + source: path.abs("specs/error-source/broken-external.json"), |
| 42 | + path: ["components", "schemas", "testSchema", "properties", "test"], |
| 43 | + message: message => typeof message === "string", |
| 44 | + }, |
| 45 | + ]); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it("schema with a missing local pointer and reference pointing at external file with broken external", async () => { |
| 50 | + const parser = new $RefParser(); |
| 51 | + try { |
| 52 | + await parser.dereference(path.abs("specs/error-source/invalid-external.json"), { continueOnError: true }); |
| 53 | + helper.shouldNotGetCalled(); |
| 54 | + } |
| 55 | + catch (err) { |
| 56 | + expect(err.errors).to.containSubset([ |
| 57 | + { |
| 58 | + name: MissingPointerError.name, |
| 59 | + source: path.abs("specs/error-source/invalid-external.json"), |
| 60 | + path: ["foo", "bar"], |
| 61 | + message: message => typeof message === "string", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: ResolverError.name, |
| 65 | + source: path.abs("specs/error-source/broken-external.json"), |
| 66 | + path: ["components", "schemas", "testSchema", "properties", "test"], |
| 67 | + message: message => typeof message === "string", |
| 68 | + }, |
| 69 | + ]); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it("schema with an invalid pointer", async () => { |
| 74 | + const parser = new $RefParser(); |
| 75 | + try { |
| 76 | + await parser.dereference(path.abs("specs/error-source/invalid-pointer.json"), { continueOnError: true }); |
| 77 | + helper.shouldNotGetCalled(); |
| 78 | + } |
| 79 | + catch (err) { |
| 80 | + expect(err.errors).to.containSubset([ |
| 81 | + { |
| 82 | + name: InvalidPointerError.name, |
| 83 | + source: path.abs("specs/error-source/invalid-pointer.json"), |
| 84 | + path: ["foo", "baz"], |
| 85 | + message: message => typeof message === "string", |
| 86 | + }, |
| 87 | + ]); |
| 88 | + } |
| 89 | + }); |
| 90 | +}); |
0 commit comments