MCPcopy
hub / github.com/autoNumeric/autoNumeric / validate

Method validate

src/AutoNumeric.js:3806–4302  ·  view source on GitHub ↗

* Validate the given option object. * If the options are valid, this function returns nothing, otherwise if the options are invalid, this function throws an error. * * This tests if the options are not conflicting and are well formatted. * This function is lenient since it only t

(userOptions, shouldExtendDefaultOptions = true, originalOptions = null)

Source from the content-addressed store, hash-verified

3804 * @throws Error This throws if the `userOptions` are not valid
3805 */
3806 static validate(userOptions, shouldExtendDefaultOptions = true, originalOptions = null) {
3807 if (AutoNumericHelper.isUndefinedOrNullOrEmpty(userOptions) || !AutoNumericHelper.isObject(userOptions)) {
3808 AutoNumericHelper.throwError(`The userOptions are invalid ; it should be a valid object, [${userOptions}] given.`);
3809 }
3810
3811 const isOriginalOptionAnObject = AutoNumericHelper.isObject(originalOptions);
3812 if (!isOriginalOptionAnObject && !AutoNumericHelper.isNull(originalOptions)) {
3813 AutoNumericHelper.throwError(`The 'originalOptions' parameter is invalid ; it should either be a valid option object or \`null\`, [${userOptions}] given.`);
3814 }
3815
3816 // If the user used old options, we convert them to new ones
3817 if (!AutoNumericHelper.isNull(userOptions)) {
3818 this._convertOldOptionsToNewOnes(userOptions);
3819 }
3820
3821 // The user can choose if the `userOptions` has already been extended with the default options, or not
3822 let options;
3823 if (shouldExtendDefaultOptions) {
3824 options = Object.assign({}, this.getDefaultConfig(), userOptions);
3825 } else {
3826 options = userOptions;
3827 }
3828
3829 // First things first, we test that the `showWarnings` option is valid
3830 if (!AutoNumericHelper.isTrueOrFalseString(options.showWarnings) && !AutoNumericHelper.isBoolean(options.showWarnings)) {
3831 AutoNumericHelper.throwError(`The debug option 'showWarnings' is invalid ; it should be either 'true' or 'false', [${options.showWarnings}] given.`);
3832 }
3833
3834 // Define the regular expressions needed for the following tests
3835 const testPositiveInteger = /^[0-9]+$/;
3836 const testNumericalCharacters = /[0-9]+/;
3837 // const testFloatAndPossibleNegativeSign = /^-?[0-9]+(\.?[0-9]+)$/;
3838 const testFloatOrIntegerAndPossibleNegativeSign = /^-?[0-9]+(\.?[0-9]+)?$/;
3839 const testPositiveFloatOrInteger = /^[0-9]+(\.?[0-9]+)?$/;
3840
3841 // Then tests the options individually
3842 if (!AutoNumericHelper.isTrueOrFalseString(options.allowDecimalPadding) &&
3843 !AutoNumericHelper.isBoolean(options.allowDecimalPadding) &&
3844 options.allowDecimalPadding !== AutoNumeric.options.allowDecimalPadding.floats &&
3845 !(AutoNumericHelper.isNumber(options.allowDecimalPadding) && options.allowDecimalPadding > 0)) {
3846 AutoNumericHelper.throwError(`The decimal padding option 'allowDecimalPadding' is invalid ; it should either be \`false\`, \`true\`, \`'floats'\` or a positive integer superior to 0, [${options.allowDecimalPadding}] given.`);
3847 }
3848
3849 if (AutoNumericHelper.isNumber(options.allowDecimalPadding) && options.allowDecimalPadding > options.decimalPlaces) {
3850 AutoNumericHelper.warning(`Setting 'allowDecimalPadding' to a number [${options.allowDecimalPadding}] superior to the current 'decimalPlaces' settings [${options.decimalPlaces}] is useless, since the padding will not be shown.`, options.showWarnings);
3851 }
3852
3853 if ((options.allowDecimalPadding === AutoNumeric.options.allowDecimalPadding.never ||
3854 options.allowDecimalPadding === 'false') &&
3855 (options.decimalPlaces !== AutoNumeric.options.decimalPlaces.none ||
3856 options.decimalPlacesShownOnBlur !== AutoNumeric.options.decimalPlacesShownOnBlur.none ||
3857 options.decimalPlacesShownOnFocus !== AutoNumeric.options.decimalPlacesShownOnFocus.none)) {
3858 AutoNumericHelper.warning(`Setting 'allowDecimalPadding' to [${options.allowDecimalPadding}] will override the current 'decimalPlaces*' settings [${options.decimalPlaces}, ${options.decimalPlacesShownOnBlur} and ${options.decimalPlacesShownOnFocus}].`, options.showWarnings);
3859 }
3860
3861 if (!AutoNumericHelper.isTrueOrFalseString(options.alwaysAllowDecimalCharacter) &&
3862 !AutoNumericHelper.isBoolean(options.alwaysAllowDecimalCharacter)) {
3863 AutoNumericHelper.throwError(`The option 'alwaysAllowDecimalCharacter' is invalid ; it should either be \`true\` or \`false\`, [${options.alwaysAllowDecimalCharacter}] given.`);

Callers 3

areSettingsValidMethod · 0.95
_setSettingsMethod · 0.80

Calls 15

getDefaultConfigMethod · 0.95
isObjectMethod · 0.80
throwErrorMethod · 0.80
isNullMethod · 0.80
isTrueOrFalseStringMethod · 0.80
isBooleanMethod · 0.80
isNumberMethod · 0.80

Tested by

no test coverage detected