Skip to content

Commit

Permalink
Support for self references
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Feb 3, 2019
1 parent d02ff5e commit e11cbb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Conditional {
}
}

getValue(parent, context) {
let values = this.refs.map(r => r.getValue(parent, context));
getValue(value, parent, context) {
let values = this.refs.map(r => r.getValue(value, parent, context));

return values;
}
Expand Down
26 changes: 16 additions & 10 deletions src/Reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,36 @@ export default class Reference {
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.isParent = this.key === '';
this.isSelf = this.key === '.';
this.isSibling = !this.isContext && !this.isParent && !this.isSelf;

if (!this.isSelf) {
this.path = this.isContext
? this.key.slice(this.prefix.length)
: this.key;
this._get = getter(this.path, true);
}

this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
this._get = getter(this.path, true);
this.map = mapFn || (value => value);
}
resolve() {
return this;
}

cast(value, { parent, context }) {
return this.getValue(parent, context);
return this.getValue(value, parent, context);
}

getValue(parent, context) {
let isContext = this.isContext;
let value = this._get(isContext ? context : parent || context || {});
getValue(value, parent, context) {
if (!this.isSelf) {
value = this._get(this.isContext ? context : parent || context || {});
}

return this.map(value);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ const proto = (SchemaType.prototype = {
return !this._typeCheck || this._typeCheck(v);
},

resolve({ context, parent }) {
resolve({ value, parent, context }) {
if (this._conditions.length) {
return this._conditions.reduce(
(schema, match) =>
match.resolve(schema, match.getValue(parent, context)),
match.resolve(schema, match.getValue(value, parent, context)),
this,
);
}
Expand Down Expand Up @@ -386,7 +386,7 @@ const proto = (SchemaType.prototype = {
deps = [].concat(keys).map(key => new Ref(key));

deps.forEach(dep => {
if (!dep.isContext) next._deps.push(dep.key);
if (dep.isSibling) next._deps.push(dep.key);
});

next._conditions.push(new Condition(deps, options));
Expand Down
4 changes: 2 additions & 2 deletions src/util/createValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default function createValidation(options) {
...rest
}) {
let parent = options.parent;
let resolve = value =>
Ref.isRef(value) ? value.getValue(parent, options.context) : value;
let resolve = item =>
Ref.isRef(item) ? item.getValue(value, parent, options.context) : item;

let createError = createErrorFactory({
message,
Expand Down
2 changes: 1 addition & 1 deletion src/util/sortFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function sortFields(fields, excludes = []) {

if (!~nodes.indexOf(key)) nodes.push(key);

if (Ref.isRef(value) && !value.isContext) addNode(value.path, key);
if (Ref.isRef(value) && value.isSibling) addNode(value.path, key);
else if (isSchema(value) && value._deps)
value._deps.forEach(path => addNode(path, key));
}
Expand Down

0 comments on commit e11cbb7

Please sign in to comment.