Comparing version 2.0.0 to 2.1.0
@@ -9,3 +9,8 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a valid array length", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected an array length for %n, received %v" | ||
: "%v is not an array length"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (is(value, arguments[1])) return value; | ||
return resolveException(value, "%v is not an array like value", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected an array like for %n, received %v" | ||
: "%v is not an array like"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not an array object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected an array for %n, received %v" : "%v is not an array"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -5,2 +5,16 @@ # Changelog | ||
## [2.1.0](https://github.com/medikoo/type/compare/v2.0.0...v2.1.0) (2020-08-21) | ||
### Features | ||
- `ensure` util for cumulated input validation ([814c5a8](https://github.com/medikoo/type/commit/814c5a801ecac23d06d8a5f4bcafc4763a04408c)) | ||
- Provide an alternative error message with `options.name` ([c7751c0](https://github.com/medikoo/type/commit/c7751c084ee4f3d3ed10500db0edde2ff00e03a1)) | ||
- Support `%n` (meaningful name) token in error message resolver ([b0f374e](https://github.com/medikoo/type/commit/b0f374e54345c714fe37a90887ecfe60577ce133)) | ||
- Support `min` validation for natural numbers ([e703512](https://github.com/medikoo/type/commit/e70351248818d3e113110106ad174b42c5fd9b25)) | ||
- Support custom Error constructors ([c6ecb90](https://github.com/medikoo/type/commit/c6ecb90e21c1c778210934204cbe393fb89ef2f6)) | ||
### Bug Fixes | ||
- Fix typo in error message ([2735533](https://github.com/medikoo/type/commit/2735533de28d33dfa13222743698169c92d08c09)) | ||
## [2.0.0](https://github.com/medikoo/type/compare/v1.2.0...v2.0.0) (2019-10-10) | ||
@@ -7,0 +21,0 @@ |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a date object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected a date for %n, received %v" : "%v is not a date"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not an error object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected an error for %n, received %v" : "%v is not an error"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,8 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a finite number", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a finite number for %n, received %v" | ||
: "%v is not a finite number"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a function", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a function for %n, received %v" | ||
: "%v is not a function"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,8 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not an integer", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected an integer for %n, received %v" | ||
: "%v is not an integer"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,7 +9,11 @@ "use strict"; | ||
var invalidItemsLimit = 3, defaultErrorMessage = "%v is not expected iterable value"; | ||
var invalidItemsLimit = 3; | ||
module.exports = function (value/*, options*/) { | ||
var options = arguments[1]; | ||
if (!is(value, options)) return resolveException(value, defaultErrorMessage, options); | ||
var mainErrorMessage = | ||
options && options.name | ||
? "Expected an iterable for %n, received %v" | ||
: "%v is not expected iterable"; | ||
if (!is(value, options)) return resolveException(value, mainErrorMessage, options); | ||
if (!options) return value; | ||
@@ -35,3 +39,3 @@ | ||
var errorMessage = | ||
resolveErrorMessage(defaultErrorMessage, value, options) + | ||
resolveErrorMessage(mainErrorMessage, value, options) + | ||
".\n Following items are invalid:"; | ||
@@ -38,0 +42,0 @@ for (var i = 0; i < invalidItems.length; ++i) { |
"use strict"; | ||
var isObject = require("../object/is") | ||
, stringCoerce = require("../string/coerce") | ||
var stringCoerce = require("../string/coerce") | ||
, toShortString = require("./to-short-string"); | ||
module.exports = function (errorMessage, value, inputOptions) { | ||
var customErrorMessage; | ||
if (isObject(inputOptions) && inputOptions.errorMessage) { | ||
customErrorMessage = stringCoerce(inputOptions.errorMessage); | ||
if (inputOptions && inputOptions.errorMessage) { | ||
errorMessage = stringCoerce(inputOptions.errorMessage); | ||
} | ||
return (customErrorMessage || errorMessage).replace("%v", toShortString(value)); | ||
var valueInsertIndex = errorMessage.indexOf("%v"); | ||
var valueToken = valueInsertIndex > -1 ? toShortString(value) : null; | ||
if (inputOptions && inputOptions.name) { | ||
var nameInsertIndex = errorMessage.indexOf("%n"); | ||
if (nameInsertIndex > -1) { | ||
if (valueInsertIndex > -1) { | ||
var firstToken, secondToken, firstInsertIndex, secondInsertIndex; | ||
if (nameInsertIndex > valueInsertIndex) { | ||
firstToken = valueToken; | ||
firstInsertIndex = valueInsertIndex; | ||
secondToken = inputOptions.name; | ||
secondInsertIndex = nameInsertIndex; | ||
} else { | ||
firstToken = inputOptions.name; | ||
firstInsertIndex = nameInsertIndex; | ||
secondToken = valueToken; | ||
secondInsertIndex = valueInsertIndex; | ||
} | ||
return ( | ||
errorMessage.slice(0, firstInsertIndex) + | ||
firstToken + | ||
errorMessage.slice(firstInsertIndex + 2, secondInsertIndex) + | ||
secondToken + | ||
errorMessage.slice(secondInsertIndex + 2) | ||
); | ||
} | ||
return ( | ||
errorMessage.slice(0, nameInsertIndex) + | ||
inputOptions.name + | ||
errorMessage.slice(nameInsertIndex + 2) | ||
); | ||
} | ||
} | ||
if (valueInsertIndex > -1) { | ||
return ( | ||
errorMessage.slice(0, valueInsertIndex) + | ||
valueToken + | ||
errorMessage.slice(valueInsertIndex + 2) | ||
); | ||
} | ||
return errorMessage; | ||
}; |
"use strict"; | ||
var isValue = require("../value/is") | ||
, isObject = require("../object/is") | ||
, resolveErrorMessage = require("./resolve-error-message"); | ||
module.exports = function (value, defaultMessage, inputOptions) { | ||
if (isObject(inputOptions) && !isValue(value)) { | ||
if (inputOptions && !isValue(value)) { | ||
if ("default" in inputOptions) return inputOptions["default"]; | ||
if (inputOptions.isOptional) return null; | ||
} | ||
throw new TypeError(resolveErrorMessage(defaultMessage, value, inputOptions)); | ||
var ErrorConstructor = (inputOptions && inputOptions.Error) || TypeError; | ||
throw new ErrorConstructor(resolveErrorMessage(defaultMessage, value, inputOptions)); | ||
}; |
"use strict"; | ||
var resolveException = require("../lib/resolve-exception") | ||
, ensureMin = require("../lib/ensure/min") | ||
, coerce = require("./coerce"); | ||
module.exports = function (value/*, options*/) { | ||
var coerced = coerce(value); | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a natural number", arguments[1]); | ||
var coerced = coerce(value), options = arguments[1]; | ||
if (coerced !== null) { | ||
if (options) { | ||
if (options.min) ensureMin(value, coerced, options); | ||
} | ||
return coerced; | ||
} | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a natural number for %n, received %v" | ||
: "%v is not a natural number"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,6 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a number", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected a number for %n, received %v" : "%v is not a number"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not an object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected an object for %n, received %v" : "%v is not an object"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
{ | ||
"name": "type", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Runtime validation and processing of JavaScript types", | ||
@@ -13,10 +13,10 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (https://www.medikoo.com/)", | ||
"chai": "^4.2.0", | ||
"eslint": "^6.4.0", | ||
"eslint-config-medikoo": "^2.5.1", | ||
"eslint": "^7.7.0", | ||
"eslint-config-medikoo": "^3.1.0", | ||
"git-list-updated": "^1.2.1", | ||
"husky": "^3.0.5", | ||
"lint-staged": "^9.2.5", | ||
"mocha": "^6.2.0", | ||
"nyc": "^14.1.1", | ||
"prettier-elastic": "^1.18.2" | ||
"husky": "^4.2.5", | ||
"lint-staged": "^10.2.11", | ||
"mocha": "^6.2.3", | ||
"nyc": "^15.1.0", | ||
"prettier-elastic": "^1.19.1" | ||
}, | ||
@@ -23,0 +23,0 @@ "husky": { |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a plain function", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a plain function for %n, received %v" | ||
: "%v is not a plain function"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,9 +9,11 @@ "use strict"; | ||
var objHasOwnProperty = Object.prototype.hasOwnProperty | ||
, invalidItemsLimit = 3 | ||
, defaultErrorMessage = "%v is not a valid plain object"; | ||
var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3; | ||
module.exports = function (value/*, options*/) { | ||
var options = arguments[1]; | ||
if (!is(value)) return resolveException(value, defaultErrorMessage, options); | ||
var mainErrorMessage = | ||
options && options.name | ||
? "Expected a plain object for %n, received %v" | ||
: "%v is not a plain object"; | ||
if (!is(value)) return resolveException(value, mainErrorMessage, options); | ||
if (!options) return value; | ||
@@ -30,3 +32,3 @@ | ||
errorMessage = | ||
resolveErrorMessage(defaultErrorMessage, value, options) + | ||
resolveErrorMessage(mainErrorMessage, value, options) + | ||
".\n Following keys are unexpected: " + | ||
@@ -57,4 +59,4 @@ invalidKeys.join(", "); | ||
errorMessage = | ||
resolveErrorMessage(defaultErrorMessage, value, options) + | ||
".\n Valuees for following keys are invalid: " + | ||
resolveErrorMessage(mainErrorMessage, value, options) + | ||
".\n Values for following keys are invalid: " + | ||
invalidKeys.join(", "); | ||
@@ -61,0 +63,0 @@ throw new TypeError(errorMessage); |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a promise", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected a promise for %n, received %v" : "%v is not a promise"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
823
README.md
@@ -14,2 +14,8 @@ [![*nix build status][nix-build-image]][nix-build-url] | ||
## Use case | ||
Validate arguments input in public API endpoints. | ||
_For validation of more sophisticated input structures (as deeply nested configuration objects) it's recommended to consider more powerful schema based utlities (as [AJV](https://ajv.js.org/) or [@hapi/joi](https://hapi.dev/family/joi/))_ | ||
### Example usage | ||
@@ -44,3 +50,3 @@ | ||
Serves following kind of utilities: | ||
Aside of general [`ensure`](docs/ensure.md) validation util, following kind of utilities for recognized JavaScript types are provided: | ||
@@ -63,750 +69,81 @@ ##### `*/coerce` | ||
- `default` - A value to be returned if `null` or `undefined` is passed as an input value. | ||
- `errorMessage` - Custom error message (`%v` can be used as a placeholder for input value) | ||
- `errorMessage` - Custom error message. Following placeholders can be used: | ||
- `%v` - To be replaced with short string representation of invalid value | ||
- `%n` - To be replaced with meaninfgul name (to be passed with `name` option) of validated value. Not effective if `name` option is not present | ||
- `name` - Meaningful name for validated value, to be used in error message, assuming it contains `%n` placeholder | ||
--- | ||
### Index | ||
### Value | ||
#### General utils: | ||
_Value_, any value that's neither `null` nor `undefined` . | ||
- [`ensure`](docs/ensure.md) | ||
#### `value/is` | ||
#### Type specific utils: | ||
Confirms whether passed argument is a _value_ | ||
- **Value** | ||
- [`value/is`](docs/value.md#valueis) | ||
- [`value/ensure`](docs/value.md#valueensure) | ||
- **Object** | ||
- [`object/is`](docs/object.md#objectis) | ||
- [`object/ensure`](docs/object.md#objectensure) | ||
- **Plain Object** | ||
- [`plain-object/is`](docs/plain-object.md#plain-objectis) | ||
- [`plain-object/ensure`](docs/plain-object.md#plain-objectensure) | ||
- **String** | ||
- [`string/coerce`](docs/string.md#stringcoerce) | ||
- [`string/ensure`](docs/string.md#stringensure) | ||
- **Number** | ||
- [`number/coerce`](docs/number.md#numbercoerce) | ||
- [`number/ensure`](docs/number.md#numberensure) | ||
- **Finite Number** | ||
- [`finite/coerce`](docs/finite.md#finitecoerce) | ||
- [`finite/ensure`](docs/finite.md#finiteensure) | ||
- **Integer Number** | ||
- [`integer/coerce`](docs/integer.md#integercoerce) | ||
- [`integer/ensure`](docs/integer.md#integerensure) | ||
- **Safe Integer Number** | ||
- [`safe-integer/coerce`](docs/safe-integer.md#safe-integercoerce) | ||
- [`safe-integer/ensure`](docs/.md#safe-integerensure) | ||
- **Natural Number** | ||
- [`natural-number/coerce`](docs/natural-number.md#natural-numbercoerce) | ||
- [`natural-number/ensure`](docs/natural-number.md#natural-numberensure) | ||
- **Array Length** | ||
- [`array-length/coerce`](docs/array-length.md#array-lengthcoerce) | ||
- [`array-length/ensure`](docs/array-length.md#array-lengthensure) | ||
- **Time Value** | ||
- [`time-value/coerce`](docs/time-value.md#time-valuecoerce) | ||
- [`time-value/ensure`](docs/time-value.md#time-valueensure) | ||
- **Array Like** | ||
- [`array-like/is`](docs/array-like.md#array-likeis) | ||
- [`array-like/ensure`](docs/array-like.md#array-likeensure) | ||
- **Array** | ||
- [`array/is`](docs/array.md#arrayis) | ||
- [`array/ensure`](docs/array.md#arrayensure) | ||
- **Iterable** | ||
- [`iterable/is`](docs/iterable.md#iterableis) | ||
- [`iterable/ensure`](docs/iterable.md#iterableensure) | ||
- **Date** | ||
- [`date/is`](docs/date.md#dateis) | ||
- [`date/ensure`](docs/date.md#dateensure) | ||
- **Function** | ||
- [`function/is`](docs/function.md#functionis) | ||
- [`function/ensure`](docs/function.md#functionensure) | ||
- **Plain Function** | ||
- [`plain-function/is`](docs/plain-function.md#plain-functionis) | ||
- [`plain-function/ensure`](docs/plain-function.md#plain-functionensure) | ||
- **Reg Exp** | ||
- [`reg-exp/is`](docs/reg-exp.md#reg-expis) | ||
- [`reg-exp/ensure`](docs/.md#reg-expensure) | ||
- **Thenable** | ||
- [`thenable/is`](docs/thenable.md#thenableis) | ||
- [`thenable/ensure`](docs/thenable.md#thenableensure) | ||
- **Promise** | ||
- [`promise/is`](docs/promise.md#promiseis) | ||
- [`promise/ensure`](docs/promise.md#promiseensure) | ||
- **Error** | ||
- [`error/is`](docs/error.md#erroris) | ||
- [`error/ensure`](docs/error.md#errorensure) | ||
- **Prototype** | ||
- [`prototype/is`](docs/prototype.md#prototypeis) | ||
```javascript | ||
const isValue = require("type/value/is"); | ||
isValue({}); // true | ||
isValue(null); // false | ||
``` | ||
#### `value/ensure` | ||
Ensures if given argument is a _value_. If it's a value it is returned back, if not `TypeError` is thrown | ||
```javascript | ||
const ensureValue = require("type/value/ensure"); | ||
const obj = {}; | ||
ensureValue(obj); // obj | ||
ensureValue(null); // Thrown TypeError: Cannot use null | ||
``` | ||
--- | ||
### Object | ||
_Object_, any non-primitive value | ||
#### `object/is` | ||
Confirms if passed value is an object | ||
```javascript | ||
const isObject = require("type/object/is"); | ||
isObject({}); // true | ||
isObject(true); // false | ||
isObject(null); // false | ||
``` | ||
#### `object/ensure` | ||
If given argument is an object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureObject = require("type/object/ensure"); | ||
const obj = {}; | ||
ensureObject(obj); // obj | ||
ensureString(null); // Thrown TypeError: null is not an object | ||
``` | ||
--- | ||
### String | ||
_string_ primitive | ||
#### `string/coerce` | ||
Restricted string coercion. Returns string presentation for every value that follows below constraints | ||
- is implicitly coercible to string | ||
- is neither`null` nor `undefined` | ||
- its `toString` method is not `Object.prototype.toString` | ||
For all other values `null` is returned | ||
```javascript | ||
const coerceToString = require("type/string/coerce"); | ||
coerceToString(12); // "12" | ||
coerceToString(undefined); // null | ||
``` | ||
#### `string/ensure` | ||
If given argument is a string coercible value (via [`string/coerce`](#stringcoerce)) returns result string. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureString = require("type/string/ensure"); | ||
ensureString(12); // "12" | ||
ensureString(null); // Thrown TypeError: null is not a string | ||
``` | ||
--- | ||
### Number | ||
_number_ primitive | ||
#### `number/coerce` | ||
Restricted number coercion. Returns number presentation for every value that follows below constraints | ||
- is implicitly coercible to number | ||
- is neither `null` nor `undefined` | ||
- is not `NaN` and doesn't coerce to `NaN` | ||
For all other values `null` is returned | ||
```javascript | ||
const coerceToNumber = require("type/number/coerce"); | ||
coerceToNumber("12"); // 12 | ||
coerceToNumber({}); // null | ||
coerceToNumber(null); // null | ||
``` | ||
#### `number/ensure` | ||
If given argument is a number coercible value (via [`number/coerce`](#numbercoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureNumber = require("type/number/ensure"); | ||
ensureNumber(12); // "12" | ||
ensureNumber(null); // Thrown TypeError: null is not a number | ||
``` | ||
--- | ||
#### Finite Number | ||
Finite _number_ primitive | ||
##### `finite/coerce` | ||
Follows [`number/coerce`](#numbercoerce) additionally rejecting `Infinity` and `-Infinity` values (`null` is returned if given values coerces to them) | ||
```javascript | ||
const coerceToFinite = require("type/finite/coerce"); | ||
coerceToFinite("12"); // 12 | ||
coerceToFinite(Infinity); // null | ||
coerceToFinite(null); // null | ||
``` | ||
##### `finite/ensure` | ||
If given argument is a finite number coercible value (via [`finite/coerce`](#finitecoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureFinite = require("type/finite/ensure"); | ||
ensureFinite(12); // "12" | ||
ensureFinite(null); // Thrown TypeError: null is not a finite number | ||
``` | ||
--- | ||
#### Integer Number | ||
Integer _number_ primitive | ||
##### `integer/coerce` | ||
Follows [`finite/coerce`](#finitecoerce) additionally stripping decimal part from the number | ||
```javascript | ||
const coerceToInteger = require("type/integer/coerce"); | ||
coerceToInteger("12.95"); // 12 | ||
coerceToInteger(Infinity); // null | ||
coerceToInteger(null); // null | ||
``` | ||
##### `integer/ensure` | ||
If given argument is an integer coercible value (via [`integer/coerce`](#integercoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureInteger = require("type/integer/ensure"); | ||
ensureInteger(12.93); // "12" | ||
ensureInteger(null); // Thrown TypeError: null is not an integer | ||
``` | ||
--- | ||
#### Safe Integer Number | ||
Safe integer _number_ primitive | ||
##### `safe-integer/coerce` | ||
Follows [`integer/coerce`](#integercoerce) but returns `null` in place of values which are beyond `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER` range. | ||
```javascript | ||
const coerceToSafeInteger = require("type/safe-integer/coerce"); | ||
coerceToInteger("12.95"); // 12 | ||
coerceToInteger(9007199254740992); // null | ||
coerceToInteger(null); // null | ||
``` | ||
##### `safe-integer/ensure` | ||
If given argument is a safe integer coercible value (via [`safe-integer/coerce`](#safe-integercoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureSafeInteger = require("type/safe-integer/ensure"); | ||
ensureSafeInteger(12.93); // "12" | ||
ensureSafeInteger(9007199254740992); // Thrown TypeError: null is not a safe integer | ||
``` | ||
--- | ||
#### Natural Number | ||
Natural _number_ primitive | ||
##### `natural-number/coerce` | ||
Follows [`integer/coerce`](#integercoerce) but returns `null` for values below `0` | ||
```javascript | ||
const coerceToNaturalNumber = require("type/natural-number/coerce"); | ||
coerceToNaturalNumber("12.95"); // 12 | ||
coerceToNaturalNumber(-120); // null | ||
coerceToNaturalNumber(null); // null | ||
``` | ||
##### `natural-number/ensure` | ||
If given argument is a natural number coercible value (via [`natural-number/coerce`](#natural-numbercoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureNaturalNumber = require("type/natural-number/ensure"); | ||
ensureNaturalNumber(12.93); // "12" | ||
ensureNaturalNumber(-230); // Thrown TypeError: null is not a natural number | ||
``` | ||
--- | ||
### Plain Object | ||
A _plain object_ | ||
- Inherits directly from `Object.prototype` or `null` | ||
- Is not a constructor's `prototype` property | ||
#### `plain-object/is` | ||
Confirms if given object is a _plain object_ | ||
```javascript | ||
const isPlainObject = require("type/plain-object/is"); | ||
isPlainObject({}); // true | ||
isPlainObject(Object.create(null)); // true | ||
isPlainObject([]); // false | ||
``` | ||
#### `plain-object/ensure` | ||
If given argument is a plain object it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensurePlainObject = require("type/plain-object/ensure"); | ||
ensurePlainObject({}); // {} | ||
ensurePlainObject("foo"); // Thrown TypeError: foo is not a plain object | ||
``` | ||
##### Confirming on keys | ||
Keys can be validated by passing `allowedKeys` option. Note that in this case: | ||
- Error message lists up to three invalid keys | ||
```javascript | ||
const allowedKeys = ["foo"]; | ||
ensurePlainObject({}, { allowedKeys }); // {} | ||
ensurePlainObject({ foo: "bar" }, { allowedKeys }); // { foo: 'bar' } | ||
/* | ||
Below invocation with crash with: | ||
TypeError: [object Object] is not a valid plain object. | ||
Following keys are unexpected: lorem, ipsum | ||
*/ | ||
ensurePlainObject({ foo: "bar", lorem: 1, ipsum: 2 }, { allowedKeys }); | ||
``` | ||
##### Confirming on property values | ||
Property values can be validated by passing `ensurePropertyValue` option. Note that in this case: | ||
- A newly created instance of plain object with coerced values is returned | ||
- Error message lists up to three keys that contain invalid values | ||
```javascript | ||
const ensureString = require("type/string/ensure"); | ||
ensurePlainObject({ foo: 12 }, { ensurePropertyValue: ensureString }); // { foo: '12' } | ||
/* | ||
Below invocation with crash with: | ||
TypeError: [object Object] is not a valid plain object. | ||
Valuees for following keys are invalid: lorem, ipsum | ||
*/ | ||
ensurePlainObject({ foo: 23, lorem: {}, ipsum: {} }, { ensurePropertyValue: ensureString }); | ||
``` | ||
--- | ||
### Array | ||
_Array_ instance | ||
#### `array/is` | ||
Confirms if given object is a native array | ||
```javascript | ||
const isArray = require("type/array/is"); | ||
isArray([]); // true | ||
isArray({}); // false | ||
isArray("foo"); // false | ||
``` | ||
#### `array/ensure` | ||
If given argument is an array, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureArray = require("type/array/ensure"); | ||
ensureArray(["foo"]); // ["foo"] | ||
ensureArray("foo"); // Thrown TypeError: foo is not an array | ||
``` | ||
--- | ||
#### Array Like | ||
_Array-like_ value (any value with `length` property) | ||
#### `array-like/is` | ||
Restricted _array-like_ confirmation. Returns true for every value that meets following contraints | ||
- is an _object_ (or with `allowString` option, a _string_) | ||
- is not a _function_ | ||
- Exposes `length` that meets [`array-length`](#array-lengthcoerce) constraints | ||
```javascript | ||
const isArrayLike = require("type/array-like/is"); | ||
isArrayLike([]); // true | ||
isArrayLike({}); // false | ||
isArrayLike({ length: 0 }); // true | ||
isArrayLike("foo"); // false | ||
isArrayLike("foo", { allowString: true }); // true | ||
``` | ||
#### `array-like/ensure` | ||
If given argument is an _array-like_, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureArrayLike = require("type/array-like/ensure"); | ||
ensureArrayLike({ length: 0 }); // { length: 0 } | ||
ensureArrayLike("foo", { allowString: true }); // "foo" | ||
ensureArrayLike({}); // Thrown TypeError: null is not an iterable | ||
``` | ||
--- | ||
#### Array length | ||
_number_ primitive that conforms as valid _array length_ | ||
##### `array-length/coerce` | ||
Follows [`safe-integer/coerce`](#safe-integercoerce) but returns `null` in place of values which are below `0` | ||
```javascript | ||
const coerceToArrayLength = require("type/safe-integer/coerce"); | ||
coerceToArrayLength("12.95"); // 12 | ||
coerceToArrayLength(9007199254740992); // null | ||
coerceToArrayLength(null); // null | ||
``` | ||
##### `array-length/ensure` | ||
If given argument is an _array length_ coercible value (via [`array-length/coerce`](#array-lengthcoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureArrayLength = require("type/array-length/ensure"); | ||
ensureArrayLength(12.93); // "12" | ||
ensureArrayLength(9007199254740992); // Thrown TypeError: null is not a valid array length | ||
``` | ||
--- | ||
### Iterable | ||
Value which implements _iterable_ protocol | ||
#### `iterable/is` | ||
Confirms if given object is an _iterable_ and is not a _string_ (unless `allowString` option is passed) | ||
```javascript | ||
const isIterable = require("type/iterable/is"); | ||
isIterable([]); // true | ||
isIterable({}); // false | ||
isIterable("foo"); // false | ||
isIterable("foo", { allowString: true }); // true | ||
``` | ||
Supports also `denyEmpty` option | ||
```javascript | ||
isIterable([], { denyEmpty: true }); // false | ||
isIterable(["foo"], { denyEmpty: true }); // true | ||
``` | ||
#### `iterable/ensure` | ||
If given argument is an _iterable_, it is returned back. Otherwise `TypeError` is thrown. | ||
By default _string_ primitives are rejected unless `allowString` option is passed. | ||
```javascript | ||
const ensureIterable = require("type/iterable/ensure"); | ||
ensureIterable([]); // [] | ||
ensureIterable("foo", { allowString: true }); // "foo" | ||
ensureIterable({}); // Thrown TypeError: null is not expected iterable | ||
``` | ||
##### Denying empty iterables | ||
Pass `denyEmpty` option to require non empty iterables | ||
```javascript | ||
ensureIterable([], { denyEmpty: true }); // Thrown TypeError: [] is not expected iterable | ||
``` | ||
##### Confirming on items | ||
Items can be validated by passing `ensureItem` option. Note that in this case: | ||
- A newly created instance of array with coerced values is returned | ||
- Error message lists up to three invalid items | ||
```javascript | ||
const ensureString = require("type/string/ensure"); | ||
ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString }); // ["foo", "12"] | ||
/* | ||
Below invocation with crash with: | ||
TypeError: [object Set] is not expected iterable value. | ||
Following items are invalid: | ||
- [object Object] | ||
*/ | ||
ensureIterable(new Set(["foo", {}]), { ensureItem: ensureString }); | ||
``` | ||
--- | ||
### Date | ||
_Date_ instance | ||
#### `date/is` | ||
Confirms if given object is a native date, and is not an _Invalid Date_ | ||
```javascript | ||
const isDate = require("type/date/is"); | ||
isDate(new Date()); // true | ||
isDate(new Date("Invalid date")); // false | ||
isDate(Date.now()); // false | ||
isDate("foo"); // false | ||
``` | ||
#### `date/ensure` | ||
If given argument is a date object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureDate = require("type/date/ensure"); | ||
const date = new Date(); | ||
ensureDate(date); // date | ||
ensureDate(123123); // Thrown TypeError: 123123 is not a date object | ||
``` | ||
--- | ||
### Time value | ||
_number_ primitive which is a valid _time value_ (as used internally in _Date_ instances) | ||
#### `time-value/coerce` | ||
Follows [`integer/coerce`](#integercoerce) but returns `null` in place of values which go beyond 100 000 0000 days from unix epoch | ||
```javascript | ||
const coerceToTimeValue = require("type/time-value/coerce"); | ||
coerceToTimeValue(12312312); // true | ||
coerceToTimeValue(Number.MAX_SAFE_INTEGER); // false | ||
coerceToTimeValue("foo"); // false | ||
``` | ||
##### `time-value/ensure` | ||
If given argument is a _time value_ coercible value (via [`time-value/coerce`](#time-valuecoerce)) returns result number. | ||
Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureTimeValue = require("type/time-value/ensure"); | ||
ensureTimeValue(12.93); // "12" | ||
ensureTimeValue(Number.MAX_SAFE_INTEGER); // Thrown TypeError: null is not a natural number | ||
``` | ||
--- | ||
### Function | ||
_Function_ instance | ||
#### `function/is` | ||
Confirms if given object is a native function | ||
```javascript | ||
const isFunction = require("type/function/is"); | ||
isFunction(function () {}); // true | ||
isFunction(() => {}); // true | ||
isFunction(class {}); // true | ||
isFunction("foo"); // false | ||
``` | ||
#### `function/ensure` | ||
If given argument is a function object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureFunction = require("type/function/ensure"); | ||
const fn = function () {}; | ||
ensureFunction(fn); // fn | ||
ensureFunction(/foo/); // Thrown TypeError: /foo/ is not a function | ||
``` | ||
--- | ||
#### Plain Function | ||
A _Function_ instance that is not a _Class_ | ||
##### `plain-function/is` | ||
Confirms if given object is a _plain function_ | ||
```javascript | ||
const isPlainFunction = require("type/plain-function/is"); | ||
isPlainFunction(function () {}); // true | ||
isPlainFunction(() => {}); // true | ||
isPlainFunction(class {}); // false | ||
isPlainFunction("foo"); // false | ||
``` | ||
##### `plain-function/ensure` | ||
If given argument is a _plain function_ object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensurePlainFunction = require("type/function/ensure"); | ||
const fn = function () {}; | ||
ensurePlainFunction(fn); // fn | ||
ensurePlainFunction(class {}); // Thrown TypeError: class is not a plain function | ||
``` | ||
--- | ||
### RegExp | ||
_RegExp_ instance | ||
#### `reg-exp/is` | ||
Confirms if given object is a native regular expression object | ||
```javascript | ||
const isRegExp = require("type/reg-exp/is"); | ||
isRegExp(/foo/); | ||
isRegExp({}); // false | ||
isRegExp("foo"); // false | ||
``` | ||
#### `reg-exp/ensure` | ||
If given argument is a regular expression object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureRegExp = require("type/reg-exp/ensure"); | ||
ensureRegExp(/foo/); // /foo/ | ||
ensureRegExp("foo"); // Thrown TypeError: null is not a regular expression object | ||
``` | ||
--- | ||
### Promise | ||
_Promise_ instance | ||
#### `promise/is` | ||
Confirms if given object is a native _promise_ | ||
```javascript | ||
const isPromise = require("type/promise/is"); | ||
isPromise(Promise.resolve()); // true | ||
isPromise({ then: () => {} }); // false | ||
isPromise({}); // false | ||
``` | ||
##### `promise/ensure` | ||
If given argument is a promise, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensurePromise = require("type/promise/ensure"); | ||
const promise = Promise.resolve(); | ||
ensurePromise(promise); // promise | ||
eensurePromise({}); // Thrown TypeError: [object Object] is not a promise | ||
``` | ||
--- | ||
#### Thenable | ||
_Thenable_ object (an object with `then` method) | ||
##### `thenable/is` | ||
Confirms if given object is a _thenable_ | ||
```javascript | ||
const isThenable = require("type/thenable/is"); | ||
isThenable(Promise.resolve()); // true | ||
isThenable({ then: () => {} }); // true | ||
isThenable({}); // false | ||
``` | ||
##### `thenable/ensure` | ||
If given argument is a _thenable_ object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureThenable = require("type/thenable/ensure"); | ||
const promise = Promise.resolve(); | ||
ensureThenable(promise); // promise | ||
ensureThenable({}); // Thrown TypeError: [object Object] is not a thenable object | ||
``` | ||
--- | ||
### Error | ||
_Error_ instance | ||
#### `error/is` | ||
Confirms if given object is a native error object | ||
```javascript | ||
const isError = require("type/error/is"); | ||
isError(new Error()); // true | ||
isError({ message: "Fake error" }); // false | ||
``` | ||
#### `error/ensure` | ||
If given argument is an error object, it is returned back. Otherwise `TypeError` is thrown. | ||
```javascript | ||
const ensureError = require("type/error/ensure"); | ||
const someError = new Error("Some error"); | ||
ensureError(someError); // someError | ||
ensureError({ message: "Fake error" }); // Thrown TypeError: [object Object] is not an error object | ||
``` | ||
--- | ||
### Prototype | ||
Some constructor's `prototype` property | ||
#### `prototype/is` | ||
Confirms if given object serves as a _prototype_ property | ||
```javascript | ||
const isPrototype = require("type/prototype/is"); | ||
isPrototype({}); // false | ||
isPrototype(Object.prototype); // true | ||
isPrototype(Array.prototype); // true | ||
``` | ||
### Tests | ||
@@ -813,0 +150,0 @@ |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a regular expression object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a regular expression for %n, received %v" | ||
: "%v is not a regular expression"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,8 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a safe integer", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a safe integer for %n, received %v" | ||
: "%v is not a safe integer"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,6 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a string", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected a string for %n, received %v" : "%v is not a string"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -16,5 +16,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "-20 is not a valid array length"); | ||
assert.equal(error.message, "-20 is not an array length"); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureArrayLength(-20, { name: "foo" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an array length for foo, received -20"); | ||
} | ||
}); | ||
}); |
@@ -21,5 +21,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not an array like value")); | ||
assert(error.message.includes("is not an array like")); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureArrayLike("foo", { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an array like for name, received foo"); | ||
} | ||
}); | ||
}); |
@@ -17,5 +17,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not an array object")); | ||
assert.equal(error.message, "null is not an array"); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureArray(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an array for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,5 +17,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not a date object")); | ||
assert(error.message.includes("is not a date")); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureDate(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a date for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,5 +17,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not an error object")); | ||
assert(error.message.includes("is not an error")); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureError(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an error for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,2 +17,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureFinite(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a finite number for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -20,2 +20,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureFunction(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a function for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,2 +17,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureInteger(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an integer for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -23,5 +23,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not expected iterable value")); | ||
assert(error.message.includes("is not expected iterable")); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureIterable("foo", { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an iterable for name, received foo"); | ||
} | ||
}); | ||
describe("Should support 'ensureItem' option", function () { | ||
@@ -39,3 +48,3 @@ it("Should resolve coerced array", function () { | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not expected iterable value")); | ||
assert(error.message.includes("is not expected iterable")); | ||
} | ||
@@ -42,0 +51,0 @@ }); |
@@ -9,4 +9,5 @@ "use strict"; | ||
assert.equal(resolveErrorMessage("%v is invalid", 12), "12 is invalid"); | ||
assert.equal(resolveErrorMessage("Value is invalid", 12), "Value is invalid"); | ||
}); | ||
it("Should support custome error message via inputOptions.errorMessage", function () { | ||
it("Should support custom error message via inputOptions.errorMessage", function () { | ||
assert.equal( | ||
@@ -17,2 +18,10 @@ resolveErrorMessage("%v is invalid", null, { errorMessage: "%v is not supported age" }), | ||
}); | ||
it("Should support %n (name) token", function () { | ||
assert.equal(resolveErrorMessage("%v is invalid", 12, { name: "foo" }), "12 is invalid"); | ||
assert.equal(resolveErrorMessage("%n is invalid", 12, { name: "foo" }), "foo is invalid"); | ||
assert.equal( | ||
resolveErrorMessage("%v for %n is invalid", 12, { name: "foo" }), | ||
"12 for foo is invalid" | ||
); | ||
}); | ||
}); |
@@ -31,2 +31,11 @@ "use strict"; | ||
}); | ||
it("Should support custom Error constructor", function () { | ||
try { | ||
handleException(12, "Invalid value", { Error: RangeError }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert(error instanceof RangeError); | ||
assert.equal(error.message, "Invalid value"); | ||
} | ||
}); | ||
}); |
@@ -19,2 +19,20 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureNaturalNumber(-20, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a natural number for name, received -20"); | ||
} | ||
}); | ||
it("Should support min validation", function () { | ||
try { | ||
ensureNaturalNumber(2, { min: 3 }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "2 is not greater or equal 3"); | ||
} | ||
}); | ||
}); |
@@ -17,2 +17,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureNumber(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a number for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -20,2 +20,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureObject(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected an object for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -20,2 +20,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensurePlainFunction(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a plain function for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -18,5 +18,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "null is not a valid plain object"); | ||
assert.equal(error.message, "null is not a plain object"); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensurePlainObject(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a plain object for name, received null"); | ||
} | ||
}); | ||
it("Should support allowedKeys option", function () { | ||
@@ -30,3 +39,3 @@ var value = { foo: "bar", marko: "elo" }; | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message.indexOf("is not a valid plain object") !== -1, true); | ||
assert.equal(error.message.indexOf("is not a plain object") !== -1, true); | ||
} | ||
@@ -45,5 +54,5 @@ }); | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message.indexOf("is not a valid plain object") !== -1, true); | ||
assert.equal(error.message.indexOf("is not a plain object") !== -1, true); | ||
} | ||
}); | ||
}); |
@@ -20,2 +20,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensurePromise(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a promise for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,5 +17,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert(error.message.includes("is not a regular expression object")); | ||
assert(error.message.includes("is not a regular expression")); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureRegExp(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a regular expression for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -19,2 +19,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureSafeInteger(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a safe integer for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,2 +17,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureString(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a string for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -17,5 +17,14 @@ "use strict"; | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "[object Object] is not a thenable object"); | ||
assert.equal(error.message, "[object Object] is not a thenable"); | ||
} | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureThenable({}, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a thenable for name, received [object Object]"); | ||
} | ||
}); | ||
}); |
@@ -17,2 +17,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureTimeValue("foo", { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a time value for name, received foo"); | ||
} | ||
}); | ||
}); |
@@ -20,2 +20,11 @@ "use strict"; | ||
}); | ||
it("Should provide alternative error message when name option is passed", function () { | ||
try { | ||
ensureValue(null, { name: "name" }); | ||
throw new Error("Unexpected"); | ||
} catch (error) { | ||
assert.equal(error.name, "TypeError"); | ||
assert.equal(error.message, "Expected a value for name, received null"); | ||
} | ||
}); | ||
}); |
@@ -8,3 +8,8 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "%v is not a thenable object", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a thenable for %n, received %v" | ||
: "%v is not a thenable"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -9,3 +9,8 @@ "use strict"; | ||
if (coerced !== null) return coerced; | ||
return resolveException(value, "%v is not a time value", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name | ||
? "Expected a time value for %n, received %v" | ||
: "%v is not a time value"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
@@ -8,3 +8,6 @@ "use strict"; | ||
if (is(value)) return value; | ||
return resolveException(value, "Cannot use %v", arguments[1]); | ||
var options = arguments[1]; | ||
var errorMessage = | ||
options && options.name ? "Expected a value for %n, received %v" : "Cannot use %v"; | ||
return resolveException(value, errorMessage, options); | ||
}; |
Sorry, the diff of this file is not supported yet
129917
129
2490
158