@apidevtools/json-schema-ref-parser
Advanced tools
Comparing version
@@ -41,3 +41,2 @@ "use strict"; | ||
const pointer_js_1 = __importDefault(require("./pointer.js")); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const url = __importStar(require("./util/url.js")); | ||
@@ -287,5 +286,6 @@ const errors_1 = require("./util/errors"); | ||
if (!options.dereference.circular) { | ||
throw ono_1.ono.reference(`Circular $ref pointer found at ${keyPath}`); | ||
const error = new ReferenceError(`Circular $ref pointer found at ${keyPath}`); | ||
throw error; | ||
} | ||
return true; | ||
} |
@@ -59,3 +59,2 @@ "use strict"; | ||
Object.defineProperty(exports, "JSONParserErrorGroup", { enumerable: true, get: function () { return errors_js_1.JSONParserErrorGroup; } }); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const maybe_js_1 = __importDefault(require("./util/maybe.js")); | ||
@@ -91,3 +90,3 @@ const options_js_1 = require("./options.js"); | ||
if (!args.path && !args.schema) { | ||
const err = (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${args.path || args.schema}`); | ||
const err = new Error(`Expected a file path, URL, or object. Got ${args.path || args.schema}`); | ||
return (0, maybe_js_1.default)(args.callback, Promise.reject(err)); | ||
@@ -141,3 +140,3 @@ } | ||
else { | ||
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`); | ||
throw new SyntaxError(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`); | ||
} | ||
@@ -144,0 +143,0 @@ } |
@@ -108,298 +108,5 @@ import type { HTTPResolverOptions, JSONSchema, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js"; | ||
export declare const getJsonSchemaRefParserDefaultOptions: () => $RefParserOptions<JSONSchema>; | ||
export declare const getNewOptions: <S extends object = JSONSchema, O extends ParserOptions<S> = { | ||
parse?: { | ||
[x: string]: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
allowEmpty?: boolean | undefined; | ||
allowBOM?: boolean | undefined; | ||
encoding?: BufferEncoding | undefined; | ||
canParse?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
parse?: string | number | {} | undefined; | ||
} | undefined; | ||
json?: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
allowEmpty?: boolean | undefined; | ||
allowBOM?: boolean | undefined; | ||
encoding?: BufferEncoding | undefined; | ||
canParse?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
parse?: string | number | {} | undefined; | ||
} | undefined; | ||
yaml?: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
allowEmpty?: boolean | undefined; | ||
allowBOM?: boolean | undefined; | ||
encoding?: BufferEncoding | undefined; | ||
canParse?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
parse?: string | number | {} | undefined; | ||
} | undefined; | ||
binary?: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
allowEmpty?: boolean | undefined; | ||
allowBOM?: boolean | undefined; | ||
encoding?: BufferEncoding | undefined; | ||
canParse?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
parse?: string | number | {} | undefined; | ||
} | undefined; | ||
text?: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
allowEmpty?: boolean | undefined; | ||
allowBOM?: boolean | undefined; | ||
encoding?: BufferEncoding | undefined; | ||
canParse?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
parse?: string | number | {} | undefined; | ||
} | undefined; | ||
} | undefined; | ||
resolve?: { | ||
[x: string]: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
canRead?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
read?: string | object | {} | undefined; | ||
} | { | ||
headers?: ([(string | undefined)?, (string | undefined)?] | undefined)[] | { | ||
[x: string]: string | undefined; | ||
} | { | ||
append?: {} | undefined; | ||
delete?: {} | undefined; | ||
get?: {} | undefined; | ||
getSetCookie?: {} | undefined; | ||
has?: {} | undefined; | ||
set?: {} | undefined; | ||
forEach?: {} | undefined; | ||
} | null | undefined; | ||
timeout?: number | undefined; | ||
redirects?: number | undefined; | ||
withCredentials?: boolean | undefined; | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
canRead?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
read?: string | object | {} | undefined; | ||
} | undefined; | ||
external?: boolean | undefined; | ||
file?: boolean | { | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
canRead?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
read?: string | object | {} | undefined; | ||
} | undefined; | ||
http?: boolean | { | ||
headers?: ([(string | undefined)?, (string | undefined)?] | undefined)[] | { | ||
[x: string]: string | undefined; | ||
} | { | ||
append?: {} | undefined; | ||
delete?: {} | undefined; | ||
get?: {} | undefined; | ||
getSetCookie?: {} | undefined; | ||
has?: {} | undefined; | ||
set?: {} | undefined; | ||
forEach?: {} | undefined; | ||
} | null | undefined; | ||
timeout?: number | undefined; | ||
redirects?: number | undefined; | ||
withCredentials?: boolean | undefined; | ||
name?: string | undefined; | ||
order?: number | undefined; | ||
canRead?: string | boolean | { | ||
exec?: {} | undefined; | ||
test?: {} | undefined; | ||
readonly source?: string | undefined; | ||
readonly global?: boolean | undefined; | ||
readonly ignoreCase?: boolean | undefined; | ||
readonly multiline?: boolean | undefined; | ||
lastIndex?: number | undefined; | ||
compile?: {} | undefined; | ||
readonly flags?: string | undefined; | ||
readonly sticky?: boolean | undefined; | ||
readonly unicode?: boolean | undefined; | ||
readonly dotAll?: boolean | undefined; | ||
readonly hasIndices?: boolean | undefined; | ||
readonly unicodeSets?: boolean | undefined; | ||
[Symbol.match]?: {} | undefined; | ||
[Symbol.replace]?: {} | undefined; | ||
[Symbol.search]?: {} | undefined; | ||
[Symbol.split]?: {} | undefined; | ||
[Symbol.matchAll]?: {} | undefined; | ||
} | (string | undefined)[] | {} | undefined; | ||
read?: string | object | {} | undefined; | ||
} | undefined; | ||
} | undefined; | ||
continueOnError?: boolean | undefined; | ||
dereference?: { | ||
circular?: boolean | "ignore" | undefined; | ||
excludedPathMatcher?: {} | undefined; | ||
onCircular?: {} | undefined; | ||
onDereference?: {} | undefined; | ||
preservedProperties?: (string | undefined)[] | undefined; | ||
externalReferenceResolution?: "relative" | "root" | undefined; | ||
} | undefined; | ||
mutateInputSchema?: boolean | undefined; | ||
timeoutMs?: number | undefined; | ||
}>(options: O | undefined) => O & $RefParserOptions<S>; | ||
export declare const getNewOptions: <S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(options: O | undefined) => O & $RefParserOptions<S>; | ||
export type Options<S extends object = JSONSchema> = $RefParserOptions<S>; | ||
export type ParserOptions<S extends object = JSONSchema> = DeepPartial<$RefParserOptions<S>>; | ||
export default $RefParserOptions; |
@@ -36,3 +36,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const url = __importStar(require("./util/url.js")); | ||
@@ -107,3 +106,3 @@ const plugins = __importStar(require("./util/plugins.js")); | ||
// Throw a generic, friendly error. | ||
throw ono_1.ono.syntax(`Unable to resolve $ref pointer "${file.url}"`); | ||
throw new SyntaxError(`Unable to resolve $ref pointer "${file.url}"`); | ||
} | ||
@@ -144,3 +143,3 @@ // Throw the original error, if it's one of our own (user-friendly) errors. | ||
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) { | ||
throw ono_1.ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`); | ||
throw new SyntaxError(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`); | ||
} | ||
@@ -160,3 +159,3 @@ else { | ||
else if (!err || !("error" in err)) { | ||
throw ono_1.ono.syntax(`Unable to parse ${file.url}`); | ||
throw new SyntaxError(`Unable to parse ${file.url}`); | ||
} | ||
@@ -163,0 +162,0 @@ else if (err.error instanceof errors_js_1.ParserError) { |
@@ -39,3 +39,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const ref_js_1 = __importDefault(require("./ref.js")); | ||
@@ -124,3 +123,3 @@ const url = __importStar(require("./util/url.js")); | ||
if (!$ref) { | ||
throw (0, ono_1.ono)(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
throw new Error(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
} | ||
@@ -168,3 +167,3 @@ $ref.set(absPath, value); | ||
if (!$ref) { | ||
throw (0, ono_1.ono)(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
throw new Error(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
} | ||
@@ -171,0 +170,0 @@ return $ref.resolve(absPath, options, path, pathFromRoot); |
@@ -40,3 +40,2 @@ "use strict"; | ||
const fs_1 = __importDefault(require("fs")); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const url = __importStar(require("../util/url.js")); | ||
@@ -66,3 +65,5 @@ const errors_js_1 = require("../util/errors.js"); | ||
catch (err) { | ||
throw new errors_js_1.ResolverError(ono_1.ono.uri(err, `Malformed URI: ${file.url}`), file.url); | ||
const e = err; | ||
e.message = `Malformed URI: ${file.url}: ${e.message}`; | ||
throw new errors_js_1.ResolverError(e, file.url); | ||
} | ||
@@ -73,5 +74,7 @@ try { | ||
catch (err) { | ||
throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error opening file "${path}"`), path); | ||
const e = err; | ||
e.message = `Error opening file ${path}: ${e.message}`; | ||
throw new errors_js_1.ResolverError(e, path); | ||
} | ||
}, | ||
}; |
@@ -36,3 +36,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const url = __importStar(require("../util/url.js")); | ||
@@ -101,10 +100,16 @@ const errors_js_1 = require("../util/errors.js"); | ||
if (res.status >= 400) { | ||
throw (0, ono_1.ono)({ status: res.status }, `HTTP ERROR ${res.status}`); | ||
const error = new Error(`HTTP ERROR ${res.status}`); | ||
error.status = res.status; | ||
throw error; | ||
} | ||
else if (res.status >= 300) { | ||
if (!Number.isNaN(httpOptions.redirects) && redirects.length > httpOptions.redirects) { | ||
throw new errors_js_1.ResolverError((0, ono_1.ono)({ status: res.status }, `Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`)); | ||
const error = new Error(`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`); | ||
error.status = res.status; | ||
throw new errors_js_1.ResolverError(error); | ||
} | ||
else if (!("location" in res.headers) || !res.headers.location) { | ||
throw (0, ono_1.ono)({ status: res.status }, `HTTP ${res.status} redirect with no location header`); | ||
const error = new Error(`HTTP ${res.status} redirect with no location header`); | ||
error.status = res.status; | ||
throw error; | ||
} | ||
@@ -125,3 +130,5 @@ else { | ||
catch (err) { | ||
throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error downloading ${u.href}`), u.href); | ||
const e = err; | ||
e.message = `Error downloading ${u.href}: ${e.message}`; | ||
throw new errors_js_1.ResolverError(e, u.href); | ||
} | ||
@@ -128,0 +135,0 @@ } |
@@ -5,2 +5,12 @@ import type $RefParser from "../index.js"; | ||
export type JSONParserErrorType = "EUNKNOWN" | "EPARSER" | "EUNMATCHEDPARSER" | "ETIMEOUT" | "ERESOLVER" | "EUNMATCHEDRESOLVER" | "EMISSINGPOINTER" | "EINVALIDPOINTER"; | ||
/** | ||
* Custom JSON serializer for Error objects. | ||
* Returns all built-in error properties, as well as extended properties. | ||
*/ | ||
export declare function toJSON<T extends Error>(this: T): Error & T; | ||
/** | ||
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`. | ||
* Does NOT return members of the base Object prototype, or the specified omitted keys. | ||
*/ | ||
export declare function getDeepKeys(obj: object, omit?: Array<string | symbol>): Set<string | symbol>; | ||
export declare class JSONParserError extends Error { | ||
@@ -13,2 +23,3 @@ readonly name: string; | ||
constructor(message: string, source?: string); | ||
toJSON: () => Error; | ||
get footprint(): string; | ||
@@ -19,2 +30,3 @@ } | ||
constructor(parser: $RefParser<S, O>); | ||
toJSON: () => Error; | ||
static getParserErrors<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(parser: $RefParser<S, O>): JSONParserError[]; | ||
@@ -21,0 +33,0 @@ get errors(): Array<JSONParserError | InvalidPointerError | ResolverError | ParserError | MissingPointerError | UnmatchedParserError | UnmatchedResolverError>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InvalidPointerError = exports.TimeoutError = exports.MissingPointerError = exports.UnmatchedResolverError = exports.ResolverError = exports.UnmatchedParserError = exports.ParserError = exports.JSONParserErrorGroup = exports.JSONParserError = void 0; | ||
exports.toJSON = toJSON; | ||
exports.getDeepKeys = getDeepKeys; | ||
exports.isHandledError = isHandledError; | ||
exports.normalizeError = normalizeError; | ||
const ono_1 = require("@jsdevtools/ono"); | ||
const url_js_1 = require("./url.js"); | ||
const nonJsonTypes = ["function", "symbol", "undefined"]; | ||
const protectedProps = ["constructor", "prototype", "__proto__"]; | ||
const objectPrototype = Object.getPrototypeOf({}); | ||
/** | ||
* Custom JSON serializer for Error objects. | ||
* Returns all built-in error properties, as well as extended properties. | ||
*/ | ||
function toJSON() { | ||
// HACK: We have to cast the objects to `any` so we can use symbol indexers. | ||
// see https://github.com/Microsoft/TypeScript/issues/1863 | ||
const pojo = {}; | ||
const error = this; | ||
for (const key of getDeepKeys(error)) { | ||
if (typeof key === "string") { | ||
const value = error[key]; | ||
const type = typeof value; | ||
if (!nonJsonTypes.includes(type)) { | ||
pojo[key] = value; | ||
} | ||
} | ||
} | ||
return pojo; | ||
} | ||
/** | ||
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`. | ||
* Does NOT return members of the base Object prototype, or the specified omitted keys. | ||
*/ | ||
function getDeepKeys(obj, omit = []) { | ||
let keys = []; | ||
// Crawl the prototype chain, finding all the string and symbol keys | ||
while (obj && obj !== objectPrototype) { | ||
keys = keys.concat(Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj)); | ||
obj = Object.getPrototypeOf(obj); | ||
} | ||
// De-duplicate the list of keys | ||
const uniqueKeys = new Set(keys); | ||
// Remove any omitted keys | ||
for (const key of omit.concat(protectedProps)) { | ||
uniqueKeys.delete(key); | ||
} | ||
return uniqueKeys; | ||
} | ||
class JSONParserError extends Error { | ||
constructor(message, source) { | ||
super(); | ||
this.toJSON = toJSON.bind(this); | ||
this.code = "EUNKNOWN"; | ||
@@ -16,3 +60,2 @@ this.name = "JSONParserError"; | ||
this.path = null; | ||
ono_1.Ono.extend(this); | ||
} | ||
@@ -27,6 +70,6 @@ get footprint() { | ||
super(); | ||
this.toJSON = toJSON.bind(this); | ||
this.files = parser; | ||
this.name = "JSONParserErrorGroup"; | ||
this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${(0, url_js_1.toFileSystemPath)(parser.$refs._root$Ref.path)}'`; | ||
ono_1.Ono.extend(this); | ||
} | ||
@@ -33,0 +76,0 @@ static getParserErrors(parser) { |
import $Ref from "./ref.js"; | ||
import Pointer from "./pointer.js"; | ||
import { ono } from "@jsdevtools/ono"; | ||
import * as url from "./util/url.js"; | ||
@@ -358,5 +357,6 @@ import type $Refs from "./refs.js"; | ||
if (!options.dereference.circular) { | ||
throw ono.reference(`Circular $ref pointer found at ${keyPath}`); | ||
const error = new ReferenceError(`Circular $ref pointer found at ${keyPath}`); | ||
throw error; | ||
} | ||
return true; | ||
} |
@@ -19,3 +19,2 @@ import $Refs from "./refs.js"; | ||
} from "./util/errors.js"; | ||
import { ono } from "@jsdevtools/ono"; | ||
import maybe from "./util/maybe.js"; | ||
@@ -81,3 +80,3 @@ import type { ParserOptions } from "./options.js"; | ||
if (!args.path && !args.schema) { | ||
const err = ono(`Expected a file path, URL, or object. Got ${args.path || args.schema}`); | ||
const err = new Error(`Expected a file path, URL, or object. Got ${args.path || args.schema}`); | ||
return maybe(args.callback, Promise.reject(err)); | ||
@@ -134,3 +133,3 @@ } | ||
} else { | ||
throw ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`); | ||
throw new SyntaxError(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`); | ||
} | ||
@@ -137,0 +136,0 @@ } catch (err) { |
@@ -1,2 +0,1 @@ | ||
import { ono } from "@jsdevtools/ono"; | ||
import * as url from "./util/url.js"; | ||
@@ -95,3 +94,3 @@ import * as plugins from "./util/plugins.js"; | ||
// Throw a generic, friendly error. | ||
throw ono.syntax(`Unable to resolve $ref pointer "${file.url}"`); | ||
throw new SyntaxError(`Unable to resolve $ref pointer "${file.url}"`); | ||
} | ||
@@ -137,3 +136,3 @@ // Throw the original error, if it's one of our own (user-friendly) errors. | ||
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) { | ||
throw ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`); | ||
throw new SyntaxError(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`); | ||
} else { | ||
@@ -149,3 +148,3 @@ return parser; | ||
} else if (!err || !("error" in err)) { | ||
throw ono.syntax(`Unable to parse ${file.url}`); | ||
throw new SyntaxError(`Unable to parse ${file.url}`); | ||
} else if (err.error instanceof ParserError) { | ||
@@ -152,0 +151,0 @@ throw err.error; |
@@ -1,2 +0,1 @@ | ||
import { ono } from "@jsdevtools/ono"; | ||
import $Ref from "./ref.js"; | ||
@@ -103,3 +102,3 @@ import * as url from "./util/url.js"; | ||
if (!$ref) { | ||
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
throw new Error(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
} | ||
@@ -154,3 +153,3 @@ | ||
if (!$ref) { | ||
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
throw new Error(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`); | ||
} | ||
@@ -157,0 +156,0 @@ |
import fs from "fs"; | ||
import { ono } from "@jsdevtools/ono"; | ||
import * as url from "../util/url.js"; | ||
@@ -31,3 +30,5 @@ import { ResolverError } from "../util/errors.js"; | ||
} catch (err: any) { | ||
throw new ResolverError(ono.uri(err, `Malformed URI: ${file.url}`), file.url); | ||
const e = err as Error; | ||
e.message = `Malformed URI: ${file.url}: ${e.message}`; | ||
throw new ResolverError(e, file.url); | ||
} | ||
@@ -37,5 +38,7 @@ try { | ||
} catch (err: any) { | ||
throw new ResolverError(ono(err, `Error opening file "${path}"`), path); | ||
const e = err as Error; | ||
e.message = `Error opening file ${path}: ${e.message}`; | ||
throw new ResolverError(e, path); | ||
} | ||
}, | ||
} as ResolverOptions<JSONSchema>; |
@@ -1,2 +0,1 @@ | ||
import { ono } from "@jsdevtools/ono"; | ||
import * as url from "../util/url.js"; | ||
@@ -81,13 +80,16 @@ import { ResolverError } from "../util/errors.js"; | ||
if (res.status >= 400) { | ||
throw ono({ status: res.status }, `HTTP ERROR ${res.status}`); | ||
const error = new Error(`HTTP ERROR ${res.status}`) as Error & { status?: number }; | ||
error.status = res.status; | ||
throw error; | ||
} else if (res.status >= 300) { | ||
if (!Number.isNaN(httpOptions.redirects) && redirects.length > httpOptions.redirects!) { | ||
throw new ResolverError( | ||
ono( | ||
{ status: res.status }, | ||
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`, | ||
), | ||
); | ||
const error = new Error( | ||
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`, | ||
) as Error & { status?: number }; | ||
error.status = res.status; | ||
throw new ResolverError(error); | ||
} else if (!("location" in res.headers) || !res.headers.location) { | ||
throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`); | ||
const error = new Error(`HTTP ${res.status} redirect with no location header`) as Error & { status?: number }; | ||
error.status = res.status; | ||
throw error; | ||
} else { | ||
@@ -105,3 +107,5 @@ const redirectTo = url.resolve(u.href, res.headers.location as string); | ||
} catch (err: any) { | ||
throw new ResolverError(ono(err, `Error downloading ${u.href}`), u.href); | ||
const e = err as Error; | ||
e.message = `Error downloading ${u.href}: ${e.message}`; | ||
throw new ResolverError(e, u.href); | ||
} | ||
@@ -108,0 +112,0 @@ } |
@@ -1,2 +0,1 @@ | ||
import { Ono } from "@jsdevtools/ono"; | ||
import { getHash, stripHash, toFileSystemPath } from "./url.js"; | ||
@@ -17,3 +16,53 @@ import type $RefParser from "../index.js"; | ||
| "EINVALIDPOINTER"; | ||
const nonJsonTypes = ["function", "symbol", "undefined"]; | ||
const protectedProps = ["constructor", "prototype", "__proto__"]; | ||
const objectPrototype = Object.getPrototypeOf({}); | ||
/** | ||
* Custom JSON serializer for Error objects. | ||
* Returns all built-in error properties, as well as extended properties. | ||
*/ | ||
export function toJSON<T extends Error>(this: T): Error & T { | ||
// HACK: We have to cast the objects to `any` so we can use symbol indexers. | ||
// see https://github.com/Microsoft/TypeScript/issues/1863 | ||
const pojo: any = {}; | ||
const error = this as any; | ||
for (const key of getDeepKeys(error)) { | ||
if (typeof key === "string") { | ||
const value = error[key]; | ||
const type = typeof value; | ||
if (!nonJsonTypes.includes(type)) { | ||
pojo[key] = value; | ||
} | ||
} | ||
} | ||
return pojo as Error & T; | ||
} | ||
/** | ||
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`. | ||
* Does NOT return members of the base Object prototype, or the specified omitted keys. | ||
*/ | ||
export function getDeepKeys(obj: object, omit: Array<string | symbol> = []): Set<string | symbol> { | ||
let keys: Array<string | symbol> = []; | ||
// Crawl the prototype chain, finding all the string and symbol keys | ||
while (obj && obj !== objectPrototype) { | ||
keys = keys.concat(Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj)); | ||
obj = Object.getPrototypeOf(obj) as object; | ||
} | ||
// De-duplicate the list of keys | ||
const uniqueKeys = new Set(keys); | ||
// Remove any omitted keys | ||
for (const key of omit.concat(protectedProps)) { | ||
uniqueKeys.delete(key); | ||
} | ||
return uniqueKeys; | ||
} | ||
export class JSONParserError extends Error { | ||
@@ -33,6 +82,6 @@ public readonly name: string; | ||
this.path = null; | ||
Ono.extend(this); | ||
} | ||
toJSON = toJSON.bind(this); | ||
get footprint() { | ||
@@ -57,5 +106,4 @@ return `${this.path}+${this.source}+${this.code}+${this.message}`; | ||
} occurred while reading '${toFileSystemPath(parser.$refs._root$Ref!.path)}'`; | ||
Ono.extend(this); | ||
} | ||
toJSON = toJSON.bind(this); | ||
@@ -62,0 +110,0 @@ static getParserErrors<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>( |
{ | ||
"name": "@apidevtools/json-schema-ref-parser", | ||
"version": "12.0.2", | ||
"version": "13.0.0", | ||
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", | ||
@@ -70,28 +70,27 @@ "scripts": { | ||
"devDependencies": { | ||
"@eslint/compat": "^1.2.5", | ||
"@eslint/js": "^9.18.0", | ||
"@eslint/compat": "^1.2.9", | ||
"@eslint/js": "^9.28.0", | ||
"@types/eslint": "^9.6.1", | ||
"@types/js-yaml": "^4.0.9", | ||
"@types/node": "^22", | ||
"@typescript-eslint/eslint-plugin": "^8.21.0", | ||
"@typescript-eslint/parser": "^8.21.0", | ||
"@vitest/coverage-v8": "^3.0.4", | ||
"@typescript-eslint/eslint-plugin": "^8.33.1", | ||
"@typescript-eslint/parser": "^8.33.1", | ||
"@vitest/coverage-v8": "^3.2.1", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^9.18.0", | ||
"eslint-config-prettier": "^10.0.1", | ||
"eslint": "^9.28.0", | ||
"eslint-config-prettier": "^10.1.5", | ||
"eslint-config-standard": "^17.1.0", | ||
"eslint-plugin-import": "^2.31.0", | ||
"eslint-plugin-prettier": "^5.2.3", | ||
"eslint-plugin-prettier": "^5.4.1", | ||
"eslint-plugin-promise": "^7.2.1", | ||
"eslint-plugin-unused-imports": "^4.1.4", | ||
"globals": "^15.14.0", | ||
"jsdom": "^26.0.0", | ||
"prettier": "^3.4.2", | ||
"globals": "^16.2.0", | ||
"jsdom": "^26.1.0", | ||
"prettier": "^3.5.3", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.7.3", | ||
"typescript-eslint": "^8.21.0", | ||
"vitest": "^3.0.4" | ||
"typescript": "^5.8.3", | ||
"typescript-eslint": "^8.33.1", | ||
"vitest": "^3.2.1" | ||
}, | ||
"dependencies": { | ||
"@jsdevtools/ono": "^7.1.3", | ||
"@types/json-schema": "^7.0.15", | ||
@@ -111,3 +110,3 @@ "js-yaml": "^4.1.0" | ||
}, | ||
"packageManager": "yarn@4.2.2" | ||
"packageManager": "yarn@4.9.1" | ||
} |
2
-33.33%329237
-2.95%7848
-2.32%- Removed
- Removed