From c550cac6cc0e05c507c26906c27663eae6fb4c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Przoda=C5=82a?= Date: Thu, 8 Jul 2021 14:32:05 +0200 Subject: [PATCH 1/2] fixMergeErrors --- src/Schema.js | 5 ++ src/Schema.test.js | 162 ++++++++++++++++++++++++--------------------- src/helpers.js | 8 ++- 3 files changed, 98 insertions(+), 77 deletions(-) diff --git a/src/Schema.js b/src/Schema.js index 79e7ec8..a3381ce 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -158,6 +158,11 @@ class Schema { return; } + if (typeof error === 'object' && this.errors[key] && this.errors[key].length) { + this.errors[key][0] = mergeErrors(this.errors[key][0], errorMessage); + return; + } + this.errors[key].push(errorMessage); } diff --git a/src/Schema.test.js b/src/Schema.test.js index af54d35..08c5e15 100644 --- a/src/Schema.test.js +++ b/src/Schema.test.js @@ -1910,7 +1910,7 @@ describe('Schema', () => { }); }); - it('should merge errors if error is set on the same key and index', () => { + xit('should merge errors if error is set on the same key and index', () => { const unitSchema = new Schema({ value: { type: String, @@ -1938,102 +1938,112 @@ describe('Schema', () => { type: unitSchema, }, }); - const modelSchema = new Schema({ + const elementsSchema = new Schema({ elements: { type: [elementSchema], }, }); + const modelSchema = new Schema({ + test: { + type: elementsSchema, + }, + }); const data = { - elements: [ - { - name: 'test', - type: 'pallet', - height: { - value: '10', - unit: 'm', - }, - weight: { - value: '10', - unit: 'm', - }, - length: { - value: '10', - unit: 'm', - }, - }, - { - name: 'test', - type: 'pallet', - height: { - value: '', - unit: 'm', - }, - weight: { - value: '10', - unit: 'm', - }, - length: { - value: '10', - unit: 'm', - }, - }, - { - name: 'test1', - type: 'pallet', - height: { - value: '', - unit: 'm', + test: { + elements: [ + { + name: 'test', + type: 'pallet', + height: { + value: '10', + unit: 'm', + }, + weight: { + value: '10', + unit: 'm', + }, + length: { + value: '10', + unit: 'm', + }, }, - weight: { - value: '', - unit: 'm', + { + name: 'test', + type: 'pallet', + height: { + value: '', + unit: 'm', + }, + weight: { + value: '10', + unit: 'm', + }, + length: { + value: '10', + unit: 'm', + }, }, - length: { - value: '10', - unit: 'm', + { + name: 'test1', + type: 'pallet', + height: { + value: '', + unit: 'm', + }, + weight: { + value: '', + unit: 'm', + }, + length: { + value: '10', + unit: 'm', + }, }, - }, - ], + ], + }, }; modelSchema.addValidator((model, schema) => { - if (!model || !Array.isArray(model.elements)) return; + if (!model || !Array.isArray(model.test.elements)) return; const uniqueNames = new Set(); const errorMsg = 'duplicatedKey'; - model.elements.forEach((element, index) => { + model.test.elements.forEach((element, index) => { if (uniqueNames.has(element.name)) { - schema.setModelError(`elements.${index}.name`, errorMsg); + schema.setModelError(`test.elements.${index}.name`, errorMsg); + schema.setModelError(`test.elements.${index}.name`, errorMsg); } else { uniqueNames.add(element.name); } }); }); - + console.log(modelSchema.validate(data)); expect(modelSchema.validate(data)).toEqual({ - elements: [ - undefined, - { - name: ['duplicatedKey'], - height: [ - { - value: ["Field 'value' is required"], - }, - ], - }, - { - height: [ - { - value: ["Field 'value' is required"], - }, - ], - weight: [ - { - value: ["Field 'value' is required"], - }, - ], - }, - ], + test: [{ + elements: [ + undefined, + { + name: ['duplicatedKey', 'duplicatedKey'], + height: [ + { + value: ["Field 'value' is required"], + }, + ], + }, + { + height: [ + { + value: ["Field 'value' is required"], + }, + ], + weight: [ + { + value: ["Field 'value' is required"], + }, + ], + }, + ], + }], }); }); diff --git a/src/helpers.js b/src/helpers.js index f79d176..d88093b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -116,7 +116,7 @@ const castAsArray = (src) => { const mergeErrorsLists = (a, b) => { const merged = []; const maxLength = Math.max(a.length, b.length); - + console.log(a, b); for (let i = 0; i < maxLength; i += 1) { const value = b[i] || a[i]; if (value && !isObjectWithoutProps(value)) { @@ -130,6 +130,8 @@ const mergeErrorsLists = (a, b) => { export const mergeArraysOfStrings = (a, b) => { const parsedA = Array.isArray(a) ? [...a] : [a]; const parsedB = Array.isArray(b) ? [...b] : [b]; + console.log('parsedA --->', parsedA); + console.log('parsedB --->', parsedB); return [...parsedA, ...parsedB]; }; @@ -154,15 +156,19 @@ const mergeObjectsErrors = (currentErrors, nextErrors) => { }; export const mergeErrors = (currentErrors = {}, nextErrors = {}) => { + console.log('mergeErrors --->', currentErrors, nextErrors); if (isObjectWithoutProps(currentErrors) && isObjectWithoutProps(nextErrors)) { + console.log('isObjectWithoutProps'); return {}; } if (isArrayOfStringsOrString(currentErrors) && isArrayOfStringsOrString(nextErrors)) { + console.log('isArrayOfStringsOrString'); return mergeArraysOfStrings(currentErrors, nextErrors); } if (isArrayable(currentErrors) || isArrayable(nextErrors)) { + console.log('isArrayable'); return mergeErrorsLists(castAsArray(currentErrors), castAsArray(nextErrors)); } From 928b211edda8d0c4d2f19229165d9ec35f1e6b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Przoda=C5=82a?= Date: Fri, 9 Jul 2021 07:33:54 +0200 Subject: [PATCH 2/2] fixes --- src/Schema.js | 2 +- src/Schema.test.js | 5 ++--- src/helpers.js | 22 ++++++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Schema.js b/src/Schema.js index a3381ce..6124f3a 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -158,7 +158,7 @@ class Schema { return; } - if (typeof error === 'object' && this.errors[key] && this.errors[key].length) { + if (typeof errorMessage === 'object' && this.errors[key] && this.errors[key].length) { this.errors[key][0] = mergeErrors(this.errors[key][0], errorMessage); return; } diff --git a/src/Schema.test.js b/src/Schema.test.js index 08c5e15..f046b84 100644 --- a/src/Schema.test.js +++ b/src/Schema.test.js @@ -1910,7 +1910,7 @@ describe('Schema', () => { }); }); - xit('should merge errors if error is set on the same key and index', () => { + it('should merge errors if error is set on the same key and index', () => { const unitSchema = new Schema({ value: { type: String, @@ -2017,13 +2017,12 @@ describe('Schema', () => { } }); }); - console.log(modelSchema.validate(data)); expect(modelSchema.validate(data)).toEqual({ test: [{ elements: [ undefined, { - name: ['duplicatedKey', 'duplicatedKey'], + name: ['duplicatedKey'], height: [ { value: ["Field 'value' is required"], diff --git a/src/helpers.js b/src/helpers.js index d88093b..d2c5404 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -88,6 +88,12 @@ const isObjectWithoutProps = (obj) => { && Object.keys(obj).length === 0; }; +const isObject = obj => ( + typeof obj === 'object' + && !Array.isArray(obj) + && obj !== null +); + const isArrayable = src => Array.isArray(src) || typeof src === 'string' || typeof src === 'undefined'; @@ -116,9 +122,15 @@ const castAsArray = (src) => { const mergeErrorsLists = (a, b) => { const merged = []; const maxLength = Math.max(a.length, b.length); - console.log(a, b); for (let i = 0; i < maxLength; i += 1) { - const value = b[i] || a[i]; + let value; + const currentErrors = a[i]; + const nextErrors = b[i]; + if (isObject(currentErrors) && isObject(nextErrors)) { + value = { ...currentErrors, ...nextErrors }; + } else { + value = b[i] || a[i]; + } if (value && !isObjectWithoutProps(value)) { merged[i] = value; } @@ -130,8 +142,6 @@ const mergeErrorsLists = (a, b) => { export const mergeArraysOfStrings = (a, b) => { const parsedA = Array.isArray(a) ? [...a] : [a]; const parsedB = Array.isArray(b) ? [...b] : [b]; - console.log('parsedA --->', parsedA); - console.log('parsedB --->', parsedB); return [...parsedA, ...parsedB]; }; @@ -156,19 +166,15 @@ const mergeObjectsErrors = (currentErrors, nextErrors) => { }; export const mergeErrors = (currentErrors = {}, nextErrors = {}) => { - console.log('mergeErrors --->', currentErrors, nextErrors); if (isObjectWithoutProps(currentErrors) && isObjectWithoutProps(nextErrors)) { - console.log('isObjectWithoutProps'); return {}; } if (isArrayOfStringsOrString(currentErrors) && isArrayOfStringsOrString(nextErrors)) { - console.log('isArrayOfStringsOrString'); return mergeArraysOfStrings(currentErrors, nextErrors); } if (isArrayable(currentErrors) || isArrayable(nextErrors)) { - console.log('isArrayable'); return mergeErrorsLists(castAsArray(currentErrors), castAsArray(nextErrors)); }