Comparing version 0.27.0 to 0.28.0
@@ -23,3 +23,3 @@ "use strict"; | ||
else { | ||
this.stack = wrapStackTrace(this, generate_stack_1.generateStackTrace()); | ||
this.stack = wrapStackTrace(this, (0, generate_stack_1.generateStackTrace)()); | ||
} | ||
@@ -26,0 +26,0 @@ this.validationErrors = errors; |
@@ -9,2 +9,17 @@ import { Predicate } from './predicates/predicate'; | ||
export declare type Main = <T>(value: T, label: string | Function, predicate: BasePredicate<T>, idLabel?: boolean) => void; | ||
/** | ||
Retrieve the type from the given predicate. | ||
@example | ||
``` | ||
import ow, {Infer} from 'ow'; | ||
const userPredicate = ow.object.exactShape({ | ||
name: ow.string | ||
}); | ||
type User = Infer<typeof userPredicate>; | ||
``` | ||
*/ | ||
export declare type Infer<P> = P extends BasePredicate<infer T> ? T : never; | ||
export interface Ow extends Modifiers, Predicates { | ||
@@ -11,0 +26,0 @@ /** |
@@ -23,12 +23,12 @@ "use strict"; | ||
const ow = (value, labelOrPredicate, predicate) => { | ||
if (!base_predicate_1.isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') { | ||
if (!(0, base_predicate_1.isPredicate)(labelOrPredicate) && typeof labelOrPredicate !== 'string') { | ||
throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``); | ||
} | ||
if (base_predicate_1.isPredicate(labelOrPredicate)) { | ||
if ((0, base_predicate_1.isPredicate)(labelOrPredicate)) { | ||
// If the second argument is a predicate, infer the label | ||
const stackFrames = callsites_1.default(); | ||
test_1.default(value, () => infer_label_1.inferLabel(stackFrames), labelOrPredicate); | ||
const stackFrames = (0, callsites_1.default)(); | ||
(0, test_1.default)(value, () => (0, infer_label_1.inferLabel)(stackFrames), labelOrPredicate); | ||
return; | ||
} | ||
test_1.default(value, labelOrPredicate, predicate); | ||
(0, test_1.default)(value, labelOrPredicate, predicate); | ||
}; | ||
@@ -39,3 +39,3 @@ Object.defineProperties(ow, { | ||
try { | ||
test_1.default(value, '', predicate); | ||
(0, test_1.default)(value, '', predicate); | ||
return true; | ||
@@ -50,8 +50,8 @@ } | ||
value: (labelOrPredicate, predicate) => (value, label) => { | ||
if (base_predicate_1.isPredicate(labelOrPredicate)) { | ||
const stackFrames = callsites_1.default(); | ||
test_1.default(value, label !== null && label !== void 0 ? label : (() => infer_label_1.inferLabel(stackFrames)), labelOrPredicate); | ||
if ((0, base_predicate_1.isPredicate)(labelOrPredicate)) { | ||
const stackFrames = (0, callsites_1.default)(); | ||
(0, test_1.default)(value, label !== null && label !== void 0 ? label : (() => (0, infer_label_1.inferLabel)(stackFrames)), labelOrPredicate); | ||
return; | ||
} | ||
test_1.default(value, label !== null && label !== void 0 ? label : (labelOrPredicate), predicate); | ||
(0, test_1.default)(value, label !== null && label !== void 0 ? label : (labelOrPredicate), predicate); | ||
} | ||
@@ -63,3 +63,3 @@ } | ||
// See https://github.com/microsoft/TypeScript/issues/36931 for more details. | ||
const _ow = predicates_1.default(modifiers_1.default(ow)); | ||
const _ow = (0, predicates_1.default)((0, modifiers_1.default)(ow)); | ||
exports.default = _ow; | ||
@@ -66,0 +66,0 @@ __exportStar(require("./predicates"), exports); |
@@ -7,3 +7,3 @@ "use strict"; | ||
optional: { | ||
get: () => predicates_1.default({}, { optional: true }) | ||
get: () => (0, predicates_1.default)({}, { optional: true }) | ||
} | ||
@@ -10,0 +10,0 @@ }); |
@@ -17,3 +17,3 @@ "use strict"; | ||
const { validator: fn, message, negatedMessage } = validator; | ||
const placeholder = random_id_1.default(); | ||
const placeholder = (0, random_id_1.default)(); | ||
validator.message = (value, label) => (negatedMessage ? | ||
@@ -20,0 +20,0 @@ negatedMessage(value, label) : |
@@ -50,3 +50,3 @@ "use strict"; | ||
// Generate the `error.message` property. | ||
const message = generate_argument_error_message_1.generateArgumentErrorMessage(errors, true); | ||
const message = (0, generate_argument_error_message_1.generateArgumentErrorMessage)(errors, true); | ||
throw new argument_error_1.ArgumentError(`Any predicate failed with the following errors:\n${message}`, main, errors); | ||
@@ -53,0 +53,0 @@ } |
@@ -137,3 +137,3 @@ "use strict"; | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: value => of_type_1.default(value, 'values', predicate) | ||
validator: value => (0, of_type_1.default)(value, 'values', predicate) | ||
}); | ||
@@ -155,3 +155,3 @@ } | ||
message: (_, label, message) => `${message.replace('Expected', 'Expected element')} in ${label}`, | ||
validator: object => match_shape_1.exact(object, shape, undefined, true) | ||
validator: object => (0, match_shape_1.exact)(object, shape, undefined, true) | ||
}); | ||
@@ -158,0 +158,0 @@ } |
@@ -58,3 +58,3 @@ "use strict"; | ||
message: (_, label, missingKeys) => `Expected ${label} to have keys \`${JSON.stringify(missingKeys)}\``, | ||
validator: map => has_items_1.default(map, keys) | ||
validator: map => (0, has_items_1.default)(map, keys) | ||
}); | ||
@@ -81,3 +81,3 @@ } | ||
message: (_, label, missingValues) => `Expected ${label} to have values \`${JSON.stringify(missingValues)}\``, | ||
validator: map => has_items_1.default(new Set(map.values()), values) | ||
validator: map => (0, has_items_1.default)(new Set(map.values()), values) | ||
}); | ||
@@ -107,3 +107,3 @@ } | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: map => of_type_1.default(map.keys(), 'keys', predicate) | ||
validator: map => (0, of_type_1.default)(map.keys(), 'keys', predicate) | ||
}); | ||
@@ -119,3 +119,3 @@ } | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: map => of_type_1.default(map.values(), 'values', predicate) | ||
validator: map => (0, of_type_1.default)(map.values(), 'values', predicate) | ||
}); | ||
@@ -122,0 +122,0 @@ } |
@@ -54,3 +54,3 @@ "use strict"; | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: object => of_type_1.default(Object.values(object), 'values', predicate) | ||
validator: object => (0, of_type_1.default)(Object.values(object), 'values', predicate) | ||
}); | ||
@@ -66,3 +66,3 @@ } | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: object => of_type_deep_1.default(object, predicate) | ||
validator: object => (0, of_type_deep_1.default)(object, predicate) | ||
}); | ||
@@ -107,3 +107,3 @@ } | ||
message: (_, label, missingKeys) => `Expected ${label} to have keys \`${JSON.stringify(missingKeys)}\``, | ||
validator: object => has_items_1.default({ | ||
validator: object => (0, has_items_1.default)({ | ||
has: item => dotProp.has(object, item) | ||
@@ -149,3 +149,3 @@ }, keys) | ||
message: (_, label, message) => `${message.replace('Expected', 'Expected property')} in ${label}`, | ||
validator: object => match_shape_1.partial(object, shape) | ||
validator: object => (0, match_shape_1.partial)(object, shape) | ||
}); | ||
@@ -174,3 +174,3 @@ } | ||
message: (_, label, message) => `${message.replace('Expected', 'Expected property')} in ${label}`, | ||
validator: object => match_shape_1.exact(object, shape) | ||
validator: object => (0, match_shape_1.exact)(object, shape) | ||
}); | ||
@@ -177,0 +177,0 @@ } |
@@ -48,3 +48,3 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${is_1.default(value)}\``; | ||
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${(0, is_1.default)(value)}\``; | ||
}, | ||
@@ -98,3 +98,3 @@ validator: value => is_1.default[typeString](value) | ||
// Generate the `error.message` property. | ||
const message = generate_argument_error_message_1.generateArgumentErrorMessage(errors); | ||
const message = (0, generate_argument_error_message_1.generateArgumentErrorMessage)(errors); | ||
throw new argument_error_1.ArgumentError(message, main, errors); | ||
@@ -113,3 +113,3 @@ } | ||
get not() { | ||
return not_1.not(this); | ||
return (0, not_1.not)(this); | ||
} | ||
@@ -116,0 +116,0 @@ /** |
@@ -58,3 +58,3 @@ "use strict"; | ||
message: (_, label, missingItems) => `Expected ${label} to have items \`${JSON.stringify(missingItems)}\``, | ||
validator: set => has_items_1.default(set, items) | ||
validator: set => (0, has_items_1.default)(set, items) | ||
}); | ||
@@ -81,3 +81,3 @@ } | ||
message: (_, label, error) => `(${label}) ${error}`, | ||
validator: set => of_type_1.default(set, 'values', predicate) | ||
validator: set => (0, of_type_1.default)(set, 'values', predicate) | ||
}); | ||
@@ -84,0 +84,0 @@ } |
@@ -21,3 +21,3 @@ "use strict"; | ||
message: (_, label, missingKeys) => `Expected ${label} to have keys \`${JSON.stringify(missingKeys)}\``, | ||
validator: map => has_items_1.default(map, keys) | ||
validator: map => (0, has_items_1.default)(map, keys) | ||
}); | ||
@@ -24,0 +24,0 @@ } |
@@ -21,3 +21,3 @@ "use strict"; | ||
message: (_, label, missingItems) => `Expected ${label} to have items \`${JSON.stringify(missingItems)}\``, | ||
validator: set => has_items_1.default(set, items) | ||
validator: set => (0, has_items_1.default)(set, items) | ||
}); | ||
@@ -24,0 +24,0 @@ } |
@@ -52,3 +52,3 @@ "use strict"; | ||
const token = match.groups.label; | ||
if (is_valid_identifier_1.default(token) || is_valid_identifier_1.default(token.split('.').pop())) { | ||
if ((0, is_valid_identifier_1.default)(token) || (0, is_valid_identifier_1.default)(token.split('.').pop())) { | ||
return token; | ||
@@ -55,0 +55,0 @@ } |
@@ -20,4 +20,4 @@ "use strict"; | ||
const label = parent ? `${parent}.${key}` : key; | ||
if (base_predicate_1.isPredicate(shape[key])) { | ||
test_1.default(object[key], label, shape[key]); | ||
if ((0, base_predicate_1.isPredicate)(shape[key])) { | ||
(0, test_1.default)(object[key], label, shape[key]); | ||
} | ||
@@ -53,4 +53,4 @@ else if (is_1.default.plainObject(shape[key])) { | ||
const label = parent ? `${parent}.${key}` : key; | ||
if (base_predicate_1.isPredicate(shape[key])) { | ||
test_1.default(object[key], label, shape[key]); | ||
if ((0, base_predicate_1.isPredicate)(shape[key])) { | ||
(0, test_1.default)(object[key], label, shape[key]); | ||
} | ||
@@ -57,0 +57,0 @@ else if (is_1.default.plainObject(shape[key])) { |
@@ -7,3 +7,3 @@ "use strict"; | ||
if (!is_1.default.plainObject(object)) { | ||
test_1.default(object, 'deep values', predicate, false); | ||
(0, test_1.default)(object, 'deep values', predicate, false); | ||
return true; | ||
@@ -10,0 +10,0 @@ } |
@@ -15,3 +15,3 @@ "use strict"; | ||
for (const item of source) { | ||
test_1.default(item, name, predicate, false); | ||
(0, test_1.default)(item, name, predicate, false); | ||
} | ||
@@ -18,0 +18,0 @@ return true; |
{ | ||
"name": "ow", | ||
"version": "0.27.0", | ||
"version": "0.28.0", | ||
"description": "Function argument validation for humans", | ||
@@ -56,3 +56,3 @@ "license": "MIT", | ||
"lodash.isequal": "^4.5.0", | ||
"type-fest": "^1.2.1", | ||
"type-fest": "^2.3.4", | ||
"vali-date": "^1.0.0" | ||
@@ -63,12 +63,12 @@ }, | ||
"@types/lodash.isequal": "^4.5.5", | ||
"@types/node": "^15.12.4", | ||
"@types/node": "^16.9.6", | ||
"@types/vali-date": "^1.0.0", | ||
"ava": "^2.4.0", | ||
"del-cli": "^4.0.0", | ||
"expect-type": "^0.11.0", | ||
"expect-type": "^0.12.0", | ||
"gh-pages": "^3.2.3", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^10.0.0", | ||
"typedoc": "^0.21.1", | ||
"typescript": "^4.3.4", | ||
"typedoc": "^0.22.4", | ||
"typescript": "^4.4.3", | ||
"xo": "^0.38.2" | ||
@@ -75,0 +75,0 @@ }, |
@@ -72,3 +72,3 @@ <p align="center"> | ||
[Complete API documentation](https://sindresorhus.com/ow/interfaces/ow.html) | ||
[Complete API documentation](https://sindresorhus.com/ow/interfaces/Ow.html) | ||
@@ -149,48 +149,48 @@ Ow includes TypeScript type guards, so using it will narrow the type of previously-unknown values. | ||
- [`undefined`](https://sindresorhus.com/ow/interfaces/ow.html#undefined-1) | ||
- [`null`](https://sindresorhus.com/ow/interfaces/ow.html#null) | ||
- [`string`](https://sindresorhus.com/ow/classes/stringpredicate.html) | ||
- [`number`](https://sindresorhus.com/ow/classes/numberpredicate.html) | ||
- [`boolean`](https://sindresorhus.com/ow/classes/booleanpredicate.html) | ||
- [`symbol`](https://sindresorhus.com/ow/interfaces/ow.html#symbol-1) | ||
- [`undefined`](https://sindresorhus.com/ow/interfaces/Ow.html#undefined-1) | ||
- [`null`](https://sindresorhus.com/ow/interfaces/Ow.html#null) | ||
- [`string`](https://sindresorhus.com/ow/classes/StringPredicate.html) | ||
- [`number`](https://sindresorhus.com/ow/classes/NumberPredicate.html) | ||
- [`boolean`](https://sindresorhus.com/ow/classes/BooleanPredicate.html) | ||
- [`symbol`](https://sindresorhus.com/ow/interfaces/Ow.html#symbol-1) | ||
#### Built-in types | ||
- [`array`](https://sindresorhus.com/ow/classes/arraypredicate.html) | ||
- [`function`](https://sindresorhus.com/ow/interfaces/ow.html#function) | ||
- [`buffer`](https://sindresorhus.com/ow/interfaces/ow.html#buffer) | ||
- [`object`](https://sindresorhus.com/ow/classes/objectpredicate.html) | ||
- [`regExp`](https://sindresorhus.com/ow/interfaces/ow.html#regexp-1) | ||
- [`date`](https://sindresorhus.com/ow/classes/datepredicate.html) | ||
- [`error`](https://sindresorhus.com/ow/classes/errorpredicate.html) | ||
- [`promise`](https://sindresorhus.com/ow/interfaces/ow.html#promise-1) | ||
- [`map`](https://sindresorhus.com/ow/classes/mappredicate.html) | ||
- [`set`](https://sindresorhus.com/ow/classes/setpredicate.html) | ||
- [`weakMap`](https://sindresorhus.com/ow/classes/weakmappredicate.html) | ||
- [`weakSet`](https://sindresorhus.com/ow/classes/weaksetpredicate.html) | ||
- [`array`](https://sindresorhus.com/ow/classes/ArrayPredicate.html) | ||
- [`function`](https://sindresorhus.com/ow/interfaces/Ow.html#function) | ||
- [`buffer`](https://sindresorhus.com/ow/interfaces/Ow.html#buffer) | ||
- [`object`](https://sindresorhus.com/ow/classes/ObjectPredicate.html) | ||
- [`regExp`](https://sindresorhus.com/ow/interfaces/Ow.html#regexp-1) | ||
- [`date`](https://sindresorhus.com/ow/classes/DatePredicate.html) | ||
- [`error`](https://sindresorhus.com/ow/classes/ErrorPredicate.html) | ||
- [`promise`](https://sindresorhus.com/ow/interfaces/Ow.html#promise-1) | ||
- [`map`](https://sindresorhus.com/ow/classes/MapPredicate.html) | ||
- [`set`](https://sindresorhus.com/ow/classes/SetPredicate.html) | ||
- [`weakMap`](https://sindresorhus.com/ow/classes/WeakMapPredicate.html) | ||
- [`weakSet`](https://sindresorhus.com/ow/classes/WeakSetPredicate.html) | ||
#### Typed arrays | ||
- [`int8Array`](https://sindresorhus.com/ow/interfaces/ow.html#int8Array) | ||
- [`uint8Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint8Array-1) | ||
- [`uint8ClampedArray`](https://sindresorhus.com/ow/interfaces/ow.html#uint8ClampedArray-1) | ||
- [`int16Array`](https://sindresorhus.com/ow/interfaces/ow.html#int16Array) | ||
- [`uint16Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint16Array-1) | ||
- [`int32Array`](https://sindresorhus.com/ow/interfaces/ow.html#in32Array) | ||
- [`uint32Array`](https://sindresorhus.com/ow/interfaces/ow.html#uin32Array-1) | ||
- [`float32Array`](https://sindresorhus.com/ow/interfaces/ow.html#float32Array) | ||
- [`float64Array`](https://sindresorhus.com/ow/interfaces/ow.html#float64Array) | ||
- [`int8Array`](https://sindresorhus.com/ow/interfaces/Ow.html#int8Array) | ||
- [`uint8Array`](https://sindresorhus.com/ow/interfaces/Ow.html#uint8Array-1) | ||
- [`uint8ClampedArray`](https://sindresorhus.com/ow/interfaces/Ow.html#uint8ClampedArray-1) | ||
- [`int16Array`](https://sindresorhus.com/ow/interfaces/Ow.html#int16Array) | ||
- [`uint16Array`](https://sindresorhus.com/ow/interfaces/Ow.html#uint16Array-1) | ||
- [`int32Array`](https://sindresorhus.com/ow/interfaces/Ow.html#int32Array) | ||
- [`uint32Array`](https://sindresorhus.com/ow/interfaces/Ow.html#uint32Array-1) | ||
- [`float32Array`](https://sindresorhus.com/ow/interfaces/Ow.html#float32Array) | ||
- [`float64Array`](https://sindresorhus.com/ow/interfaces/Ow.html#float64Array) | ||
#### Structured data | ||
- [`arrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#arraybuffer) | ||
- [`dataView`](https://sindresorhus.com/ow/interfaces/ow.html#dataview) | ||
- [`sharedArrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#sharedarraybuffer-1) | ||
- [`arrayBuffer`](https://sindresorhus.com/ow/interfaces/Ow.html#arrayBuffer) | ||
- [`dataView`](https://sindresorhus.com/ow/interfaces/Ow.html#dataView) | ||
- [`sharedArrayBuffer`](https://sindresorhus.com/ow/interfaces/Ow.html#sharedArrayBuffer-1) | ||
#### Miscellaneous | ||
- [`nan`](https://sindresorhus.com/ow/interfaces/ow.html#nan) | ||
- [`nullOrUndefined`](https://sindresorhus.com/ow/interfaces/ow.html#nullorundefined) | ||
- [`iterable`](https://sindresorhus.com/ow/interfaces/ow.html#iterable) | ||
- [`typedArray`](https://sindresorhus.com/ow/interfaces/ow.html#typedarray-1) | ||
- [`nan`](https://sindresorhus.com/ow/interfaces/Ow.html#nan) | ||
- [`nullOrUndefined`](https://sindresorhus.com/ow/interfaces/Ow.html#nullOrUndefined) | ||
- [`iterable`](https://sindresorhus.com/ow/interfaces/Ow.html#iterable) | ||
- [`typedArray`](https://sindresorhus.com/ow/interfaces/Ow.html#typedArray-1) | ||
@@ -294,2 +294,16 @@ ### Predicates | ||
### TypeScript | ||
Ow includes a type utility that lets you to extract a TypeScript type from the given predicate. | ||
```ts | ||
import ow, {Infer} from 'ow'; | ||
const userPredicate = ow.object.exactShape({ | ||
name: ow.string | ||
}); | ||
type User = Infer<typeof userPredicate>; | ||
``` | ||
## Maintainers | ||
@@ -296,0 +310,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
135421
3303
315
+ Addedtype-fest@2.19.0(transitive)
- Removedtype-fest@1.4.0(transitive)
Updatedtype-fest@^2.3.4