natesilva / jayschema

[Unmaintained] - A comprehensive JSON Schema validator for Node.js
BSD 3-Clause "New" or "Revised" License
217 stars 22 forks source link

$ref with spaces does not work #63

Open EdikDolynskyi opened 7 years ago

EdikDolynskyi commented 7 years ago

Validation doesn't work if the reference path $ref has spaces, e.g. "#/definitions/some name". Here is the next part of the code:

SchemaRegistry._resolveJsonPointer = function(schema, jp) {
   if (jp === '#') {
      return schema;
   }

  if (jp.slice(0, 2) !== '#/') {
    // not a JSON pointer fragment
    // (may be a valid id ref, but that’s not our problem here)
    return null;
  }
  var path = jp.slice(2).split('/');
  var currentSchema = schema;
  while (path.length) {
     var element = SchemaRegistry._decodeJsonPointer(path.shift());
     if (!Object.prototype.hasOwnProperty.call(currentSchema, element)) {
         return null;
     }
     currentSchema = currentSchema[element];
  }

  return currentSchema;
};

If the $ref has spaces, second param jp in the function is "#/definitions/some%20name". As a result, local definition can`t be founded. It would be nice if the author took the time to fix it.