1.0.4 • Published 4 years ago
@tmurphree/validation-error-messages v1.0.4
validation-error-messages
Companion library to @tmurphree/validation-predicates.
Why
Makes it easier for you to make complicated validation messages.
Change log
Installation
npm install --save @tmurphree/validation-error-messages
Usage
const { isObjectLike } = require('@tmurphree/validation-predicates');
const { makeIsObjectLikeMessage } = require('@tmurphree/validation-error-messages');
const template = { password: '***', userName: 'something' };
// the long way
if (!(isObjectLike(input, template))) {
throw new Error('Yikes! input is not valid but I have no idea why! Now I have to run complicated code to find it.');
}
// uses this library and makes good error messages quickly
// throws Error('input is missing at least one property: password.')
if (!(isObjectLike(input, template))) {
throw new Error(makeIsObjectLikeMessage(input, template));
}
Strict mode
Important
The options settings in @tmurphree/validation-predicates and @tmurphree/validation-error-messages MUST be the same for things to act as expected. This includes but is not limited to strict mode (because strict mode is just a quick way to set options).
Strict mode options in this library mirror the strict mode options in @tmurphree/validation-predicates.
Without strict mode:
const { makeIsObjectLikeMessage } = require('@tmurphree/validation-error-messages');
With strict mode:
const { makeIsObjectLikeMessage } = require('@tmurphree/validation-error-messages').strict;
Functions
makeExpectedPropsMessage
/**
* @description Make an error message for isObjectWithExpectedProps.
* @param {object} x The object to test.
* @param {string[]} expectedProperties The required properties.
* @param {string} [variableName='input'] The name you want printed in the message.
* @returns {string|undefined} string|undefined
*/
makeExpectedPropsMessage(input, expectedProperties, [variableName]);
Sample output:
- input is not an object.
- input is missing at least property foo.
undefined
(returns undefined if for some reason all of the expected properties are there)
makeIsObjectLikeMessage
/**
* @description Make an error message for isObjectLike.
* @param {object} x The object to test.
* @param {object} template The object you want x to look like.
* @param {string} [variableName='input'] The name you want printed in the message.
* @param {object} [options]
* @param {boolean} [options.allowExtraProps=false] Return undefined if the object to test has
* properties not in template and other tests pass.
* @param {boolean} [options.checkType=false] If true check property data types.
* @returns {string|undefined} string|undefined
*/
makeIsObjectLikeMessage(x, template, [variableName], [options]);
Sample output:
- input is not an object.
- input is missing at least property foo.
- input has at least one additional property someSuperCoolProperty.
- input.fizz is type string and expected type boolean.
undefined
(returns undefined if for some reason all of the expected properties are there and, if checkType is true, they're of the expected type)