-
Notifications
You must be signed in to change notification settings - Fork 906
/
Reference.js
43 lines (33 loc) · 1.05 KB
/
Reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { getter } from 'property-expr';
let validateName = d => {
if (typeof d !== 'string')
throw new TypeError('ref\'s must be strings, got: ' + d)
}
export default class Reference {
static isRef(value) {
return !!(value && (value.__isYupRef || value instanceof Reference))
}
constructor(key, mapFn, options = {}) {
validateName(key)
let prefix = options.contextPrefix || '$';
if (typeof key === 'function') {
key = '.';
}
this.key = key.trim();
this.prefix = prefix;
this.isContext = this.key.indexOf(prefix) === 0
this.isSelf = this.key === '.';
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key
this._get = getter(this.path, true)
this.map = mapFn || (value => value);
}
cast(value, { parent, context }) {
return this.getValue(parent, context)
}
getValue(parent, context) {
let isContext = this.isContext
let value = this._get(isContext ? context : (parent || context) || {})
return this.map(value)
}
}
Reference.prototype.__isYupRef = true