Skip to content

Commit

Permalink
Merge pull request #396 from henryrgithub/master
Browse files Browse the repository at this point in the history
Use web URL API instead of Node URL library
  • Loading branch information
tdegrunt authored Apr 5, 2024
2 parents b53e585 + c366037 commit 0742229
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
22 changes: 18 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -131,13 +129,13 @@ var SchemaContext = exports.SchemaContext = function SchemaContext (schema, opti
};

SchemaContext.prototype.resolve = function resolve (target) {
return uri.resolve(this.base, target);
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;
var base = uri.resolve(this.base, id||'');
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;
Expand Down Expand Up @@ -390,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();
}
6 changes: 3 additions & 3 deletions lib/scan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

var urilib = require('url');
var helpers = require('./helpers');

module.exports.SchemaScanResult = SchemaScanResult;
Expand All @@ -20,12 +19,13 @@ 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 = helpers.resolveUrl(baseuri,schema.$ref);
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 = helpers.resolveUrl(baseuri,id);
var ourBase = id ? resolvedBase : baseuri;
if (ourBase) {
// If there's no fragment, append an empty one
if(ourBase.indexOf('#')<0) ourBase += '#';
Expand Down
8 changes: 3 additions & 5 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var urilib = require('url');

var attribute = require('./attribute');
var helpers = require('./helpers');
var scanSchema = require('./scan').scan;
Expand Down Expand Up @@ -115,7 +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;
var base = urilib.resolve(options.base||anonymousBase, id||'');
let base = helpers.resolveUrl(options.base,id||'');
if(!ctx){
ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas));
if (!ctx.schemas[base]) {
Expand Down Expand Up @@ -262,8 +260,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);
Expand Down

0 comments on commit 0742229

Please sign in to comment.