Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Jul 9, 2021
1 parent c550cac commit 928b211
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
Expand Down
22 changes: 14 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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];
};

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

Expand Down

0 comments on commit 928b211

Please sign in to comment.