@foobarhq/validators
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -31,2 +31,8 @@ // @flow | ||
test('can accept undefined as a default value', () => { | ||
const validate = optionalValidator(noValidate(), { defaultValue: void 0 }); | ||
expect(validate(void 0)).toEqual(void 0); | ||
}); | ||
test('can accept a null as a defaultValue', () => { | ||
@@ -33,0 +39,0 @@ const validate = optionalValidator(noValidate(), { defaultValue: null }); |
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="2.0.0"></a> | ||
# [2.0.0](https://github.com/foobarhq/validators/compare/v1.1.0...v2.0.0) (2017-06-29) | ||
### Features | ||
* Accept undefined as default values ([772cb3e](https://github.com/foobarhq/validators/commit/772cb3e)) | ||
<a name="1.1.0"></a> | ||
@@ -7,0 +17,0 @@ # 1.1.0 (2017-06-13) |
@@ -15,5 +15,3 @@ 'use strict'; | ||
const defaultOptionalArgs = { | ||
allowNull: false, | ||
defaultValue: void 0, | ||
threatAsVoid: false | ||
allowNull: false | ||
}; | ||
@@ -25,7 +23,7 @@ | ||
// $FlowFixMe | ||
{ allowNull, defaultValue, threatAsVoid } = defaultOptionalArgs) { | ||
options = defaultOptionalArgs) { | ||
return function validateOptional(item, metadata) { | ||
if (item === null) { | ||
if (allowNull) { | ||
if (options.allowNull) { | ||
return null; | ||
@@ -37,12 +35,12 @@ } | ||
if (item === void 0 || threatAsVoid && threatAsVoid === item || Array.isArray(threatAsVoid) && threatAsVoid.includes(item)) { | ||
if (defaultValue === void 0) { | ||
if (item === void 0 || options.threatAsVoid && options.threatAsVoid === item || Array.isArray(options.threatAsVoid) && options.threatAsVoid.includes(item)) { | ||
if (!Object.prototype.hasOwnProperty.call(options, 'defaultValue')) { | ||
throw new _InvalidData2.default('Missing value'); | ||
} | ||
if (typeof defaultValue === 'function') { | ||
return defaultValue(); | ||
if (typeof options.defaultValue === 'function') { | ||
return options.defaultValue(); | ||
} | ||
return defaultValue; | ||
return options.defaultValue; | ||
} | ||
@@ -53,2 +51,2 @@ | ||
} | ||
//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xsKSB7XG4gICAgICBpZiAob3B0aW9ucy5hbGxvd051bGwpIHtcbiAgICAgICAgcmV0dXJuIG51bGw7XG4gICAgICB9XG5cbiAgICAgIHRocm93IG5ldyBJbnZhbGlkRGF0YSgnQ2Fubm90IGJlIG51bGwnKTtcbiAgICB9XG5cbiAgICBpZiAoXG4gICAgICBpdGVtID09PSB2b2lkIDBcbiAgICAgIHx8IChvcHRpb25zLnRocmVhdEFzVm9pZCAmJiBvcHRpb25zLnRocmVhdEFzVm9pZCA9PT0gaXRlbSlcbiAgICAgIHx8IChBcnJheS5pc0FycmF5KG9wdGlvbnMudGhyZWF0QXNWb2lkKSAmJiBvcHRpb25zLnRocmVhdEFzVm9pZC5pbmNsdWRlcyhpdGVtKSlcbiAgICApIHtcbiAgICAgIGlmICghT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKG9wdGlvbnMsICdkZWZhdWx0VmFsdWUnKSkge1xuICAgICAgICB0aHJvdyBuZXcgSW52YWxpZERhdGEoJ01pc3NpbmcgdmFsdWUnKTtcbiAgICAgIH1cblxuICAgICAgaWYgKHR5cGVvZiBvcHRpb25zLmRlZmF1bHRWYWx1ZSA9PT0gJ2Z1bmN0aW9uJykge1xuICAgICAgICByZXR1cm4gb3B0aW9ucy5kZWZhdWx0VmFsdWUoKTtcbiAgICAgIH1cblxuICAgICAgcmV0dXJuIG9wdGlvbnMuZGVmYXVsdFZhbHVlO1xuICAgIH1cblxuICAgIHJldHVybiB2YWxpZGF0b3IoaXRlbSwgbWV0YWRhdGEpO1xuICB9O1xufVxuXG4iXX0= |
{ | ||
"name": "@foobarhq/validators", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "FP composable string parsers and validators.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# @foobarhq/validators | ||
[![npm version](https://badge.fury.io/js/%40foobarhq%2Fvalidators.svg)](https://badge.fury.io/js/%40foobarhq%2Fvalidators) | ||
The main purpose of this library is (but not limited) to parse and validate data received from HTTP requests. | ||
@@ -4,0 +6,0 @@ |
@@ -9,3 +9,3 @@ // @flow | ||
defaultValue?: any, | ||
threatAsVoid?: boolean, | ||
threatAsVoid?: any, | ||
}; | ||
@@ -15,4 +15,2 @@ | ||
allowNull: false, | ||
defaultValue: void 0, | ||
threatAsVoid: false, | ||
}; | ||
@@ -25,3 +23,3 @@ | ||
// $FlowFixMe | ||
{ allowNull, defaultValue, threatAsVoid }: ?OptionalArgs = defaultOptionalArgs | ||
options?: OptionalArgs = defaultOptionalArgs | ||
): Validator { | ||
@@ -31,3 +29,3 @@ | ||
if (item === null) { | ||
if (allowNull) { | ||
if (options.allowNull) { | ||
return null; | ||
@@ -41,14 +39,14 @@ } | ||
item === void 0 | ||
|| (threatAsVoid && threatAsVoid === item) | ||
|| (Array.isArray(threatAsVoid) && threatAsVoid.includes(item)) | ||
|| (options.threatAsVoid && options.threatAsVoid === item) | ||
|| (Array.isArray(options.threatAsVoid) && options.threatAsVoid.includes(item)) | ||
) { | ||
if (defaultValue === void 0) { | ||
if (!Object.prototype.hasOwnProperty.call(options, 'defaultValue')) { | ||
throw new InvalidData('Missing value'); | ||
} | ||
if (typeof defaultValue === 'function') { | ||
return defaultValue(); | ||
if (typeof options.defaultValue === 'function') { | ||
return options.defaultValue(); | ||
} | ||
return defaultValue; | ||
return options.defaultValue; | ||
} | ||
@@ -55,0 +53,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
161287
70
240