Skip to content

Commit

Permalink
#40: bugfix for edge case where keyword is errorMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-s96 committed Sep 18, 2023
1 parent 1d987e4 commit d0845f8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/schema/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function validatePayload(payloads = {}, schema) {
Object.entries(field).forEach(([f]) => {
Object.entries(errorCache[resource]?.[f] || {}).forEach(([, files]) => {
const fileNames = Object.keys(files);
const { message, value } = files[fileNames[0]];
const { message } = files[fileNames[0]];
if (errorCache[resource]?.[f]) {
if (!field[f]?.errors) {
field[f].errors = [];
Expand All @@ -123,7 +123,6 @@ function validatePayload(payloads = {}, schema) {
errors: [
{
message,
value,
occurrences: fileNames.map(fname => ({
count: files[fname].occurrences,
fileName: fname
Expand All @@ -135,7 +134,7 @@ function validatePayload(payloads = {}, schema) {
});
Object.entries(warningsCache[resource]?.[f] || {}).forEach(([, files]) => {
const fileNames = Object.keys(files);
const { message, value } = files[fileNames[0]];
const { message } = files[fileNames[0]];
if (warningsCache[resource]?.[f]) {
if (!field[f]?.warnings) {
field[f].warnings = [];
Expand All @@ -146,7 +145,6 @@ function validatePayload(payloads = {}, schema) {
warnings: [
{
message,
value,
occurrences: fileNames.map(fname => ({
count: files[fname].occurrences,
fileName: fname
Expand Down Expand Up @@ -224,13 +222,17 @@ function generateErrorReport({
failedItemName = params?.additionalProperty;
}
}
let resolvedKeyword = keyword;
if (keyword === 'errorMessage') {
resolvedKeyword = params?.errors?.[0]?.keyword || keyword;
}
// find corresponding value in the payload
let failedItemValue = nestedPayloadProperties.reduce((acc, curr) => {
if (!acc && json[curr]) return json[curr];
return acc[curr];
}, null);

if (failedItemValue?.['constructor'] === Object && keyword === 'additionalProperties') {
if (failedItemValue?.['constructor'] === Object && resolvedKeyword === 'additionalProperties') {
failedItemValue = 'additionalProperties';
}

Expand All @@ -243,7 +245,7 @@ function generateErrorReport({
}

// needs to be generic to accomodate failures that need to be changed to warnings when -a is passed.
if (keyword === 'maxLength' && additionalPropertiesAllowed) {
if (resolvedKeyword === 'maxLength' && additionalPropertiesAllowed) {
if (!warningsCache[resourceName]) {
warningsCache[resourceName] = {};
}
Expand Down

0 comments on commit d0845f8

Please sign in to comment.