Skip to content

Commit

Permalink
Add keyPath and recursive parentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Sep 19, 2022
1 parent 375e089 commit 5f4da09
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ Then use this custom key in your JSON
### 19.5. <a name='Customerrorfunctions'></a>Custom error functions
You can also add custom error functions at a granular level with access to `constraints`, `key` , `title`, `description`, `parentNode` and `typeHandler`
You can also add custom error functions at a granular level with access to `constraints`, `key` , `keyPath`, `title`, `description`, `parentNode` and `typeHandler`
````js
const message2 = {
Expand Down
13 changes: 11 additions & 2 deletions src/error-message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class ErrorMessageHandler extends TypeMatcher {
this.value = typeHandler.value; // raw type constraints
this.constraints = typeHandler.constraints; // type constraints (possibly filtered)
this.parentNode = typeHandler.parentNode;
this.keyPath = typeHandler.keyPath;
this.type = typeHandler.type;
this.description = typeHandler.description;
this.title = typeHandler.title;
Expand Down Expand Up @@ -45,8 +46,15 @@ export class ErrorMessageHandler extends TypeMatcher {
}

validationErrorMessage(msgName) {
const { constraints, key, description, title, typeHandler, parentNode } =
this;
const {
constraints,
key,
description,
title,
typeHandler,
keyPath,
parentNode,
} = this;
const errMsg = this.errMessageFor(msgName);
return typeof errMsg === "function"
? errMsg(constraints, {
Expand All @@ -55,6 +63,7 @@ export class ErrorMessageHandler extends TypeMatcher {
description,
typeHandler,
parentNode,
keyPath,
})
: errMsg;
}
Expand Down
19 changes: 19 additions & 0 deletions src/types/mixed/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ class YupMixed extends Base {
this.base = this.getBase();
}

get keyPath() {
this._keyPath = this._keyPath || this.calcKeyPath();
return this._keyPath;
}

calcKeyPath() {
let ctx = this;
let parentNode;
let keys = [];
while ((parentNode = ctx.parentNode)) {
if (!parentNode) {
break;
}
keys.push(parentNode.key);
ctx = ctx.parentNode;
}
return keys.join(".");
}

get builder() {
return this.entryHandler && this.entryHandler.builder;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/object/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class YupObject extends YupMixed {
const parentNode = {
key: this.key,
...this.value,
parentNode: this.parentNode,
};
const yupSchema = this.config.buildYup(schema, config, parentNode);
this.base = yupSchema;
Expand Down

0 comments on commit 5f4da09

Please sign in to comment.