-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…loses #1650.
- Loading branch information
1 parent
f3ffef2
commit 7e86c05
Showing
23 changed files
with
825 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,167 @@ | ||
'use strict'; | ||
|
||
const Hoek = require('@hapi/hoek'); | ||
const Marker = require('@hapi/marker'); | ||
|
||
const Utils = require('./utils'); | ||
|
||
const internals = {}; | ||
|
||
const internals = { | ||
symbol: Marker('ref') // Used to internally identify references (shared with other joi versions) | ||
}; | ||
|
||
|
||
module.exports = exports = internals.Ref = class { | ||
|
||
exports.create = function (key, options) { | ||
constructor(key, options = {}) { | ||
|
||
Hoek.assert(typeof key === 'string', 'Invalid reference key:', key); | ||
Hoek.assert(typeof key === 'string', 'Invalid reference key:', key); | ||
|
||
const settings = options ? Hoek.clone(options) : {}; // options can be reused and modified | ||
this.settings = Hoek.clone(options); | ||
this[internals.symbol] = true; | ||
|
||
const ref = function (parent, value, validationOptions) { | ||
const contextPrefix = this.settings.contextPrefix || '$'; | ||
const separator = this.settings.separator || '.'; | ||
|
||
const target = ref.isContext ? validationOptions.context : (settings.self ? value : parent); | ||
return Hoek.reach(target, ref.key, settings); | ||
}; | ||
this.isContext = key[0] === contextPrefix; | ||
if (this.isContext) { | ||
key = key.slice(1); | ||
this.display = `context:${key}`; | ||
} | ||
else { | ||
if (this.settings.ancestor !== undefined) { | ||
Hoek.assert(key[0] !== separator, 'Cannot combine prefix with ancestor option'); | ||
} | ||
else { | ||
const [ancestor, slice] = internals.ancestor(key, separator); | ||
if (slice) { | ||
key = key.slice(slice); | ||
} | ||
|
||
const contextPrefix = settings.contextPrefix || '$'; | ||
const separator = settings.separator || '.'; | ||
this.settings.ancestor = ancestor; | ||
} | ||
|
||
ref.isContext = key[0] === contextPrefix; | ||
if (!ref.isContext && | ||
key[0] === separator) { | ||
this.display = internals.display(key, separator, this.settings.ancestor); | ||
} | ||
|
||
key = key.slice(1); | ||
settings.self = true; | ||
this.key = key; | ||
this.ancestor = this.settings.ancestor; | ||
this.path = this.key.split(separator); | ||
this.depth = this.path.length; | ||
this.root = this.path[0]; | ||
} | ||
|
||
ref.key = ref.isContext ? key.slice(1) : key; | ||
ref.path = ref.key.split(separator); | ||
ref.depth = ref.path.length; | ||
ref.root = ref.path[0]; | ||
ref.isJoi = true; | ||
resolve(value, state, options = {}) { | ||
|
||
ref.toString = () => { | ||
const ancestor = this.settings.ancestor; | ||
Hoek.assert(!ancestor || ancestor <= state.ancestors.length, 'Invalid reference exceeds the schema root:', this.display); | ||
|
||
return (ref.isContext ? 'context:' : 'ref:') + ref.key; | ||
}; | ||
const target = this.isContext ? options.context : (ancestor ? state.ancestors[ancestor - 1] : value); | ||
return Hoek.reach(target, this.key, this.settings); | ||
} | ||
|
||
toString() { | ||
|
||
ref.describe = () => { | ||
return this.display; | ||
} | ||
|
||
describe() { | ||
|
||
return { | ||
type: ref.isContext ? 'context' : 'ref', | ||
key: ref.key, | ||
path: ref.path | ||
type: this.isContext ? 'context' : 'ref', | ||
key: this.key, | ||
path: this.path | ||
}; | ||
}; | ||
} | ||
|
||
static isRef(ref) { | ||
|
||
return ref; | ||
return ref && !!ref[internals.symbol]; | ||
} | ||
}; | ||
|
||
|
||
exports.isRef = function (ref) { | ||
internals.ancestor = function (key, separator) { | ||
|
||
return typeof ref === 'function' && ref.isJoi; | ||
if (key[0] !== separator) { // 'a.b' -> 1 (parent) | ||
return [1, 0]; | ||
} | ||
|
||
if (key[1] !== separator) { // '.a.b' -> 0 (self) | ||
return [0, 1]; | ||
} | ||
|
||
let i = 2; | ||
while (key[i] === separator) { | ||
++i; | ||
} | ||
|
||
return [i - 1, i]; // '...a.b.' -> 2 (grandparent) | ||
}; | ||
|
||
|
||
exports.push = function (array, ref) { | ||
internals.display = function (key, separator, ancestor) { | ||
|
||
if (!ancestor) { | ||
return `ref:${separator}${key}`; | ||
} | ||
|
||
if (ancestor === 1) { | ||
return `ref:${key}`; | ||
} | ||
|
||
return `ref:${new Array(3).fill('.').join('')}${key}`; | ||
}; | ||
|
||
|
||
internals.Ref.toSibling = 0; | ||
internals.Ref.toParent = 1; | ||
|
||
|
||
internals.Ref.Manager = class { | ||
|
||
constructor() { | ||
|
||
this.refs = []; // 0: [self refs], 1: [parent refs], 2: [grandparent refs], ... | ||
} | ||
|
||
register(source, target = internals.Ref.toParent) { | ||
|
||
if (!source) { | ||
return; | ||
} | ||
|
||
// Any | ||
|
||
if (Utils.isSchema(source)) { | ||
for (const item of source._refs.refs) { | ||
if (item.ancestor - target >= 0) { | ||
this.refs.push({ ancestor: item.ancestor - target, root: item.root }); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
// Reference | ||
|
||
if (internals.Ref.isRef(source) && | ||
!source.isContext && | ||
source.ancestor - target >= 0) { | ||
|
||
this.refs.push({ ancestor: source.ancestor - target, root: source.root }); | ||
} | ||
} | ||
|
||
clone() { | ||
|
||
const copy = new internals.Ref.Manager(); | ||
copy.refs = Hoek.clone(this.refs); | ||
return copy; | ||
} | ||
|
||
if (exports.isRef(ref) && | ||
!ref.isContext) { | ||
roots() { | ||
|
||
array.push(ref.root); | ||
return this.refs.filter((ref) => !ref.ancestor).map((ref) => ref.root); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.