Skip to content

Commit

Permalink
fixMergeErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Jul 8, 2021
1 parent cfd94c8 commit c550cac
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 77 deletions.
5 changes: 5 additions & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
162 changes: 86 additions & 76 deletions src/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
},
],
},
],
}],
});
});

Expand Down
8 changes: 7 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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];
};

Expand All @@ -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));
}

Expand Down

0 comments on commit c550cac

Please sign in to comment.