Skip to content

Commit

Permalink
Improve error message when using oneOf validation. Fixes metal#254
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Frampton authored and Robert-Frampton committed Oct 6, 2017
1 parent 8fe3762 commit 5012217
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/metal-state/src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFunctionName, isDefAndNotNull } from 'metal';

const ERROR_ARRAY_OF_TYPE = 'Expected an array of single type.';
const ERROR_OBJECT_OF_TYPE = 'Expected object of one type.';
const ERROR_ONE_OF = 'Expected one of given values.';
const ERROR_ONE_OF = 'Expected one of the following values:';
const ERROR_ONE_OF_TYPE = 'Expected one of given types.';
const ERROR_SHAPE_OF = 'Expected object with a specific shape.';

Expand Down Expand Up @@ -84,8 +84,9 @@ const validators = {
return result;
}
return arrayOfValues.indexOf(value) === -1 ?
composeError(ERROR_ONE_OF, name, context) :
true;
composeError(
composeOneOfErrorMessage(arrayOfValues), name, context
) : true;
});
},

Expand Down Expand Up @@ -184,6 +185,15 @@ function composeError(error, name, context) {
);
}

/**
* Composes an error message for Conifg.oneOf validator.
* @param {!Array} arrayOfValues Array of values to check equality against.
* @return {!Error}
*/
function composeOneOfErrorMessage(arrayOfValues) {
return `${ERROR_ONE_OF} ${JSON.stringify(arrayOfValues)}.`;
}

/**
* Returns the type of the given value.
* @param {*} value Any value.
Expand Down

0 comments on commit 5012217

Please sign in to comment.