Fixed dataPath handling for root property when dataPath is empty string

This commit is contained in:
Vishal Shingala
2020-07-06 14:11:57 +05:30
parent 5203d582e3
commit 54d810eb9f
2 changed files with 17 additions and 6 deletions

View File

@@ -2425,7 +2425,7 @@ module.exports = {
if (options.suggestAvailableFixes) {
mismatchObj.suggestedFix = {
key: _.split(dataPath, '.').pop(),
actualValue: _.get(valueToUse, dataPath, null),
actualValue: (dataPath === '' ? valueToUse : _.get(valueToUse, dataPath, null)),
suggestedValue: this.getSuggestedValue(fakedValue, valueToUse, ajvError)
};
}
@@ -2457,7 +2457,14 @@ module.exports = {
if (dataPath[0] === '.') {
dataPath = dataPath.slice(1);
}
_.set(suggestedValue, dataPath, this.getSuggestedValue(fakedValue, valueToUse, ajvError));
// for empty string _.set creates new key with empty string '', so separate handling
if (dataPath === '') {
suggestedValue = this.getSuggestedValue(fakedValue, valueToUse, ajvError);
}
else {
_.set(suggestedValue, dataPath, this.getSuggestedValue(fakedValue, valueToUse, ajvError));
}
});
mismatchObj.suggestedFix = {
@@ -3408,14 +3415,17 @@ module.exports = {
var suggestedValue,
tempSuggestedValue,
dataPath = ajvValidationErrorObj.dataPath || '',
targetActualValue = _.get(actualValue, dataPath, {}),
targetFakedValue = _.get(fakedValue, dataPath, {});
targetActualValue,
targetFakedValue;
// discard the leading '.' if it exists
if (dataPath[0] === '.') {
dataPath = dataPath.slice(1);
}
targetActualValue = (dataPath === '' ? actualValue : _.get(actualValue, dataPath, {}));
targetFakedValue = (dataPath === '' ? fakedValue : _.get(fakedValue, dataPath, {}));
switch (ajvValidationErrorObj.keyword) {
// to do: check for minItems, maxItems
@@ -3450,7 +3460,7 @@ module.exports = {
// Keywords: minLength, maxLength, format, minimum, maximum, type, multipleOf, pattern
default:
suggestedValue = _.get(fakedValue, dataPath, null);
suggestedValue = (dataPath === '' ? fakedValue : _.get(fakedValue, dataPath, null));
break;
}