From 8b2d62b9ec0ee1b078c4ec7101a4fef8be49be94 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 11:52:57 -0700 Subject: [PATCH 01/11] updated submodule git:// -> https:// --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 4e25145..e1a7410 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "test/suite"] path = test/suite - url = git://github.com/json-schema-org/JSON-Schema-Test-Suite.git + url = https://github.com/json-schema-org/JSON-Schema-Test-Suite.git From c0d5cba9aab0ed060f7ad892301ca27a12018595 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 12:54:34 -0700 Subject: [PATCH 02/11] removed dependency on Node URL in favor of web URL API --- lib/validator.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index cce1694..b08b1a9 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -115,7 +115,12 @@ Validator.prototype.validate = function validate (instance, schema, options, ctx // This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema // This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI var id = schema.$id || schema.id; - var base = urilib.resolve(options.base||anonymousBase, id||''); + let base = new URL(id||'', new URL(options.base||anonymousBase, 'resolve://')); + if(base.protocol === 'resolve:'){ + const { pathname, search, hash } = base; + base = pathname + search + hash; + } + base = String(base) if(!ctx){ ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas)); if (!ctx.schemas[base]) { @@ -262,8 +267,8 @@ Validator.prototype.resolve = function resolve (schema, switchSchema, ctx) { return {subschema: ctx.schemas[switchSchema], switchSchema: switchSchema}; } // Else try walking the property pointer - var parsed = urilib.parse(switchSchema); - var fragment = parsed && parsed.hash; + let parsed = new URL(switchSchema,'thismessage::/'); + let fragment = parsed.hash; var document = fragment && fragment.length && switchSchema.substr(0, switchSchema.length - fragment.length); if (!document || !ctx.schemas[document]) { throw new SchemaError("no such schema <" + switchSchema + ">", schema); From 89bac2adb022345ecea18a8cb22f89dfd2932205 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 13:24:31 -0700 Subject: [PATCH 03/11] removed node URL lib req --- lib/validator.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index b08b1a9..6318de3 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -1,7 +1,5 @@ 'use strict'; -var urilib = require('url'); - var attribute = require('./attribute'); var helpers = require('./helpers'); var scanSchema = require('./scan').scan; From 1c952ad610ed80d2abaab51a11abeb90db25bc45 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 13:30:07 -0700 Subject: [PATCH 04/11] missing semicolon --- lib/validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validator.js b/lib/validator.js index 6318de3..985fda5 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -118,7 +118,7 @@ Validator.prototype.validate = function validate (instance, schema, options, ctx const { pathname, search, hash } = base; base = pathname + search + hash; } - base = String(base) + base = String(base); if(!ctx){ ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas)); if (!ctx.schemas[base]) { From 001c24f72e53c5a7a267faa8e76c7dcade763679 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 14:33:28 -0700 Subject: [PATCH 05/11] removed dependency on node URL lib --- lib/helpers.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 58a9f36..876599e 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,7 +1,5 @@ 'use strict'; -var uri = require('url'); - var ValidationError = exports.ValidationError = function ValidationError (message, instance, schema, path, name, argument) { if(Array.isArray(path)){ this.path = path; @@ -131,13 +129,24 @@ var SchemaContext = exports.SchemaContext = function SchemaContext (schema, opti }; SchemaContext.prototype.resolve = function resolve (target) { - return uri.resolve(this.base, target); + const resolvedUrl = new URL(target, new URL(this.base, 'resolve://')); + if (resolvedUrl.protocol === 'resolve:') { + const { pathname, search, hash } = resolvedUrl; + return pathname + search + hash; + } + return resolvedUrl.toString(); }; SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){ var path = (propertyName===undefined) ? this.path : this.path.concat([propertyName]); var id = schema.$id || schema.id; - var base = uri.resolve(this.base, id||''); + //var base = uri.resolve(this.base, id||''); + let base = new URL(id||'', new URL(this.base, 'resolve://')); + if (base.protocol === 'resolve:') { + const { pathname, search, hash } = base; + base = pathname + search + hash; + } + base = base.toString(); var ctx = new SchemaContext(schema, this.options, path, base, Object.create(this.schemas)); if(id && !ctx.schemas[base]){ ctx.schemas[base] = schema; From ed152d7f8720f07138c559700e89c14300855ddc Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 14:34:42 -0700 Subject: [PATCH 06/11] removed commented old logic --- lib/helpers.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 876599e..a6c6004 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -140,7 +140,6 @@ SchemaContext.prototype.resolve = function resolve (target) { SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){ var path = (propertyName===undefined) ? this.path : this.path.concat([propertyName]); var id = schema.$id || schema.id; - //var base = uri.resolve(this.base, id||''); let base = new URL(id||'', new URL(this.base, 'resolve://')); if (base.protocol === 'resolve:') { const { pathname, search, hash } = base; From 57cf7f17cca95de16d170a7d6b9c2493bb265561 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 15:47:54 -0700 Subject: [PATCH 07/11] removed node URL lib dependency from scan.js --- lib/scan.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/scan.js b/lib/scan.js index 26f6b33..b79fd21 100644 --- a/lib/scan.js +++ b/lib/scan.js @@ -1,6 +1,5 @@ "use strict"; -var urilib = require('url'); var helpers = require('./helpers'); module.exports.SchemaScanResult = SchemaScanResult; @@ -20,12 +19,25 @@ module.exports.scan = function scan(base, schema){ if(!schema || typeof schema!='object') return; // Mark all referenced schemas so we can tell later which schemas are referred to, but never defined if(schema.$ref){ - var resolvedUri = urilib.resolve(baseuri, schema.$ref); + let resolvedUri = new URL(schema.$ref, new URL(baseuri, 'resolve://')); + if (resolvedUri.protocol === 'resolve:') { + const { pathname, search, hash } = resolvedUri; + resolvedUri = pathname + search + hash; + } + resolvedUri = resolvedUri.toString(); ref[resolvedUri] = ref[resolvedUri] ? ref[resolvedUri]+1 : 0; return; } var id = schema.$id || schema.id; - var ourBase = id ? urilib.resolve(baseuri, id) : baseuri; + + let resolvedBase = new URL(id, new URL(baseuri, 'resolve://')); + if (resolvedBase.protocol === 'resolve:') { + const { pathname, search, hash } = resolvedBase; + resolvedBase = pathname + search + hash; + } + resolvedBase = resolvedBase.toString(); + + var ourBase = id ? resolvedBase : baseuri; if (ourBase) { // If there's no fragment, append an empty one if(ourBase.indexOf('#')<0) ourBase += '#'; From a656ebb0dc1146b664b95d82b9e316998f3293af Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 15:53:48 -0700 Subject: [PATCH 08/11] changed String(URL) to URL.toString() --- lib/validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validator.js b/lib/validator.js index 985fda5..fb26324 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -118,7 +118,7 @@ Validator.prototype.validate = function validate (instance, schema, options, ctx const { pathname, search, hash } = base; base = pathname + search + hash; } - base = String(base); + base = base.toString(); if(!ctx){ ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas)); if (!ctx.schemas[base]) { From 616f86fc0eebd97cb2cc17e81cc64dbc480e6fde Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 20:48:38 -0700 Subject: [PATCH 09/11] replaced instanced resolve logic with helper logic in helper.js --- lib/helpers.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index a6c6004..239eb19 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -129,23 +129,13 @@ var SchemaContext = exports.SchemaContext = function SchemaContext (schema, opti }; SchemaContext.prototype.resolve = function resolve (target) { - const resolvedUrl = new URL(target, new URL(this.base, 'resolve://')); - if (resolvedUrl.protocol === 'resolve:') { - const { pathname, search, hash } = resolvedUrl; - return pathname + search + hash; - } - return resolvedUrl.toString(); + return (() => resolveUrl(this.base,target))(); }; SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){ var path = (propertyName===undefined) ? this.path : this.path.concat([propertyName]); var id = schema.$id || schema.id; - let base = new URL(id||'', new URL(this.base, 'resolve://')); - if (base.protocol === 'resolve:') { - const { pathname, search, hash } = base; - base = pathname + search + hash; - } - base = base.toString(); + let base = (() => resolveUrl(this.base,id||''))(); var ctx = new SchemaContext(schema, this.options, path, base, Object.create(this.schemas)); if(id && !ctx.schemas[base]){ ctx.schemas[base] = schema; @@ -398,3 +388,19 @@ exports.isSchema = function isSchema(val){ return (typeof val === 'object' && val) || (typeof val === 'boolean'); }; +/** + * Resolve target URL from a base and relative URL. + * Similar to Node's URL Lib's legacy resolve function. + * Code from example in deprecation note in said library. + * @param string + * @param string + * @returns {string} + */ +var resolveUrl = exports.resolveUrl = function resolveUrl(from, to) { + const resolvedUrl = new URL(to, new URL(from, 'resolve://')); + if (resolvedUrl.protocol === 'resolve:') { + const { pathname, search, hash } = resolvedUrl; + return pathname + search + hash; + } + return resolvedUrl.toString(); +} From 592f49d913ea627d114df32d2e86afce8d8853f5 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 20:53:22 -0700 Subject: [PATCH 10/11] replaced instanced resolve logic with helper in scan.js --- lib/scan.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/scan.js b/lib/scan.js index b79fd21..c33100f 100644 --- a/lib/scan.js +++ b/lib/scan.js @@ -19,24 +19,12 @@ module.exports.scan = function scan(base, schema){ if(!schema || typeof schema!='object') return; // Mark all referenced schemas so we can tell later which schemas are referred to, but never defined if(schema.$ref){ - let resolvedUri = new URL(schema.$ref, new URL(baseuri, 'resolve://')); - if (resolvedUri.protocol === 'resolve:') { - const { pathname, search, hash } = resolvedUri; - resolvedUri = pathname + search + hash; - } - resolvedUri = resolvedUri.toString(); + let resolvedUri = helpers.resolveUrl(baseuri,schema.$ref); ref[resolvedUri] = ref[resolvedUri] ? ref[resolvedUri]+1 : 0; return; } var id = schema.$id || schema.id; - - let resolvedBase = new URL(id, new URL(baseuri, 'resolve://')); - if (resolvedBase.protocol === 'resolve:') { - const { pathname, search, hash } = resolvedBase; - resolvedBase = pathname + search + hash; - } - resolvedBase = resolvedBase.toString(); - + let resolvedBase = helpers.resolveUrl(baseuri,id); var ourBase = id ? resolvedBase : baseuri; if (ourBase) { // If there's no fragment, append an empty one From c366037b7c7278aefd8e6c4981e779fe74dd8595 Mon Sep 17 00:00:00 2001 From: henryrgithub Date: Thu, 4 Apr 2024 20:56:06 -0700 Subject: [PATCH 11/11] replaced url resolve logic with helper function in validator.js --- lib/validator.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index fb26324..cca5e9c 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -113,12 +113,7 @@ Validator.prototype.validate = function validate (instance, schema, options, ctx // This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema // This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI var id = schema.$id || schema.id; - let base = new URL(id||'', new URL(options.base||anonymousBase, 'resolve://')); - if(base.protocol === 'resolve:'){ - const { pathname, search, hash } = base; - base = pathname + search + hash; - } - base = base.toString(); + let base = helpers.resolveUrl(options.base,id||''); if(!ctx){ ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas)); if (!ctx.schemas[base]) {