|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = Pointer; |
| 4 | + |
| 5 | +var $Ref = require('./ref'), |
| 6 | + util = require('./util'), |
| 7 | + url = require('url'), |
| 8 | + ono = require('ono'), |
| 9 | + escapedSlash = /~1/g, |
| 10 | + escapedTilde = /~0/g; |
| 11 | + |
| 12 | +/** |
| 13 | + * This class represents a single JSON pointer and its resolved value. |
| 14 | + * |
| 15 | + * @param {$Ref} $ref |
| 16 | + * @param {string} path |
| 17 | + * @constructor |
| 18 | + */ |
| 19 | +function Pointer($ref, path) { |
| 20 | + /** |
| 21 | + * The {@link $Ref} object that contains this {@link Pointer} object. |
| 22 | + * @type {$Ref} |
| 23 | + */ |
| 24 | + this.$ref = $ref; |
| 25 | + |
| 26 | + /** |
| 27 | + * The file path or URL, containing the JSON pointer in the hash. |
| 28 | + * This path is relative to the path of the main JSON schema file. |
| 29 | + * @type {string} |
| 30 | + */ |
| 31 | + this.path = path; |
| 32 | + |
| 33 | + /** |
| 34 | + * The value of the JSON pointer. |
| 35 | + * Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays). |
| 36 | + * @type {?*} |
| 37 | + */ |
| 38 | + this.value = undefined; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Resolves the value of a nested property within the given object. |
| 43 | + * |
| 44 | + * @param {*} obj - The object that will be crawled |
| 45 | + * @param {$RefParserOptions} [options] |
| 46 | + * |
| 47 | + * @returns {Pointer} |
| 48 | + * Returns a JSON pointer whose {@link Pointer#value} is the resolved value. |
| 49 | + * If resolving this value required resolving other JSON references, then |
| 50 | + * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path |
| 51 | + * of the resolved value. |
| 52 | + */ |
| 53 | +Pointer.prototype.resolve = function(obj, options) { |
| 54 | + var tokens = Pointer.parse(this.path); |
| 55 | + this.value = obj; |
| 56 | + |
| 57 | + // Crawl the value, one token at a time |
| 58 | + for (var i = 0; i < tokens.length; i++) { |
| 59 | + resolveIf$Ref(this, options); |
| 60 | + |
| 61 | + var token = tokens[i]; |
| 62 | + if (this.value[token] === undefined) { |
| 63 | + throw ono.syntax('Error resolving $ref pointer "%s". \nToken "%s" does not exist.', path, token); |
| 64 | + } |
| 65 | + else { |
| 66 | + this.value = this.value[token]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Resolve the final value |
| 71 | + resolveIf$Ref(this, options); |
| 72 | + return this; |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Sets the value of a nested property within the given object. |
| 77 | + * |
| 78 | + * @param {*} obj - The object that will be crawled |
| 79 | + * @param {*} value - the value to assign |
| 80 | + * @param {$RefParserOptions} [options] |
| 81 | + * |
| 82 | + * @returns {*} |
| 83 | + * Returns the modified object, or an entirely new object if the entire object is overwritten. |
| 84 | + */ |
| 85 | +Pointer.prototype.set = function(obj, value, options) { |
| 86 | + var tokens = Pointer.parse(this.path); |
| 87 | + |
| 88 | + if (tokens.length === 0) { |
| 89 | + // There are no tokens, replace the entire object with the new value |
| 90 | + this.value = value; |
| 91 | + return value; |
| 92 | + } |
| 93 | + |
| 94 | + // Crawl the object, one token at a time |
| 95 | + this.value = obj; |
| 96 | + for (var i = 0; i < tokens.length - 1; i++) { |
| 97 | + resolveIf$Ref(this, options); |
| 98 | + |
| 99 | + var token = tokens[i]; |
| 100 | + if (this.value && this.value[token] !== undefined) { |
| 101 | + // The token exists |
| 102 | + this.value = this.value[token]; |
| 103 | + } |
| 104 | + else { |
| 105 | + // The token doesn't exist, so create it |
| 106 | + this.value = setValue(this, token, {}); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Set the value of the final token |
| 111 | + resolveIf$Ref(this, options); |
| 112 | + token = tokens[tokens.length - 1]; |
| 113 | + setValue(this, token, value); |
| 114 | + |
| 115 | + // Return the updated object |
| 116 | + return obj; |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Parses a JSON pointer (or a path containing a JSON pointer in the hash) |
| 121 | + * and returns an array of the pointer's tokens. |
| 122 | + * (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"]) |
| 123 | + * |
| 124 | + * The pointer is parsed according to RFC 6901 |
| 125 | + * {@link https://tools.ietf.org/html/rfc6901#section-3} |
| 126 | + * |
| 127 | + * @param {string} path |
| 128 | + * @returns {string[]} |
| 129 | + */ |
| 130 | +Pointer.parse = function(path) { |
| 131 | + // Get the JSON pointer from the path's hash |
| 132 | + var pointer = util.path.getHash(path).substr(1); |
| 133 | + |
| 134 | + // If there's no pointer, then there are no tokens, |
| 135 | + // so return an empty array |
| 136 | + if (!pointer) { |
| 137 | + return []; |
| 138 | + } |
| 139 | + |
| 140 | + // Split into an array |
| 141 | + pointer = pointer.split('/'); |
| 142 | + |
| 143 | + // Decode each part, according to RFC 6901 |
| 144 | + for (var i = 0; i < pointer.length; i++) { |
| 145 | + pointer[i] = pointer[i].replace(escapedSlash, '/').replace(escapedTilde, '~'); |
| 146 | + } |
| 147 | + |
| 148 | + if (pointer[0] !== '') { |
| 149 | + throw ono.syntax('Invalid $ref pointer "%s". Pointers must begin with "#/"', pointer); |
| 150 | + } |
| 151 | + |
| 152 | + return pointer.slice(1); |
| 153 | +}; |
| 154 | + |
| 155 | +/** |
| 156 | + * If the given pointer's {@link Pointer#value} is a JSON reference, |
| 157 | + * then the reference is resolved and {@link Pointer#value} is replaced with the resolved value. |
| 158 | + * In addition, {@link Pointer#path} and {@link Pointer#$ref} are updated to reflect the |
| 159 | + * resolution path of the new value. |
| 160 | + * |
| 161 | + * @param {Pointer} pointer |
| 162 | + * @param {$RefParserOptions} [options] |
| 163 | + */ |
| 164 | +function resolveIf$Ref(pointer, options) { |
| 165 | + // Is the value a JSON reference? (and allowed?) |
| 166 | + if ($Ref.isAllowed$Ref(pointer.value, options)) { |
| 167 | + var $refPath = url.resolve(pointer.path, pointer.value.$ref); |
| 168 | + |
| 169 | + // Is the value a reference to itself? If so, then there's nothing to do. |
| 170 | + if ($refPath !== pointer.path) { |
| 171 | + // Resolve the reference |
| 172 | + var resolved = pointer.$ref.$refs._resolve($refPath); |
| 173 | + pointer.$ref = resolved.$ref; |
| 174 | + pointer.path = resolved.path; // pointer.path = $refPath ??? |
| 175 | + pointer.value = resolved.value; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Sets the specified token value of the {@link Pointer#value}. |
| 182 | + * |
| 183 | + * The token is evaluated according to RFC 6901. |
| 184 | + * {@link https://tools.ietf.org/html/rfc6901#section-4} |
| 185 | + * |
| 186 | + * @param {Pointer} pointer - The JSON Pointer whose value will be modified |
| 187 | + * @param {string} token - A JSON Pointer token that indicates how to modify `obj` |
| 188 | + * @param {*} value - The value to assign |
| 189 | + * @returns {*} - Returns the assigned value |
| 190 | + */ |
| 191 | +function setValue(pointer, token, value) { |
| 192 | + if (pointer.value && typeof(pointer.value) === 'object') { |
| 193 | + if (token === '-' && Array.isArray(pointer.value)) { |
| 194 | + pointer.value.push(value); |
| 195 | + } |
| 196 | + else { |
| 197 | + pointer.value[token] = value; |
| 198 | + } |
| 199 | + } |
| 200 | + else { |
| 201 | + throw ono.syntax('Error assigning $ref pointer "%s". \nCannot set "%s" of a non-object.', pointer.path, token); |
| 202 | + } |
| 203 | + return value; |
| 204 | +} |
0 commit comments