Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when using oneOf validation. Fixes #254 #272

Merged
merged 2 commits into from
Oct 11, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible typo: Conifg.oneOf -> Config.oneOf

* @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