Comparing version 0.22.0 to 0.23.0
@@ -5,4 +5,4 @@ /** | ||
export declare class ArgumentError extends Error { | ||
readonly validationErrors: ReadonlyMap<string, string[]>; | ||
constructor(message: string, context: Function, stack: string, errors?: Map<string, string[]>); | ||
readonly validationErrors: ReadonlyMap<string, Set<string>>; | ||
constructor(message: string, context: Function, errors?: Map<string, Set<string>>); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ArgumentError = void 0; | ||
const generate_stack_1 = require("./utils/generate-stack"); | ||
const wrapStackTrace = (error, stack) => `${error.name}: ${error.message}\n${stack}`; | ||
@@ -9,3 +10,3 @@ /** | ||
class ArgumentError extends Error { | ||
constructor(message, context, stack, errors = new Map()) { | ||
constructor(message, context, errors = new Map()) { | ||
super(message); | ||
@@ -23,3 +24,3 @@ Object.defineProperty(this, "validationErrors", { | ||
else { | ||
this.stack = wrapStackTrace(this, stack); | ||
this.stack = wrapStackTrace(this, generate_stack_1.generateStackTrace()); | ||
} | ||
@@ -26,0 +27,0 @@ this.validationErrors = errors; |
@@ -8,3 +8,3 @@ import { Predicate } from './predicates/predicate'; | ||
*/ | ||
export declare type Main = <T>(value: T, label: string | Function, predicate: BasePredicate<T>, stack: string) => void; | ||
export declare type Main = <T>(value: T, label: string | Function, predicate: BasePredicate<T>) => void; | ||
export interface Ow extends Modifiers, Predicates { | ||
@@ -11,0 +11,0 @@ /** |
@@ -12,5 +12,3 @@ "use strict"; | ||
const test_1 = require("./test"); | ||
const generate_stack_1 = require("./utils/generate-stack"); | ||
const ow = (value, labelOrPredicate, predicate) => { | ||
const stack = generate_stack_1.generateStackTrace(); | ||
if (!base_predicate_1.isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') { | ||
@@ -22,6 +20,6 @@ throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``); | ||
const stackFrames = callsites_1.default(); | ||
test_1.default(value, () => infer_label_1.inferLabel(stackFrames), labelOrPredicate, stack); | ||
test_1.default(value, () => infer_label_1.inferLabel(stackFrames), labelOrPredicate); | ||
return; | ||
} | ||
test_1.default(value, labelOrPredicate, predicate, stack); | ||
test_1.default(value, labelOrPredicate, predicate); | ||
}; | ||
@@ -42,9 +40,8 @@ Object.defineProperties(ow, { | ||
value: (labelOrPredicate, predicate) => (value, label) => { | ||
const stack = generate_stack_1.generateStackTrace(); | ||
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, stack); | ||
test_1.default(value, label !== null && label !== void 0 ? label : (() => infer_label_1.inferLabel(stackFrames)), labelOrPredicate); | ||
return; | ||
} | ||
test_1.default(value, label !== null && label !== void 0 ? label : (labelOrPredicate), predicate, stack); | ||
test_1.default(value, label !== null && label !== void 0 ? label : (labelOrPredicate), predicate); | ||
} | ||
@@ -51,0 +48,0 @@ } |
@@ -11,3 +11,3 @@ import { BasePredicate, testSymbol } from './base-predicate'; | ||
constructor(predicates: BasePredicate[], options?: PredicateOptions); | ||
[testSymbol](value: T, main: Main, label: string | Function, stack: string): asserts value; | ||
[testSymbol](value: T, main: Main, label: string | Function): asserts value; | ||
} |
@@ -25,7 +25,7 @@ "use strict"; | ||
} | ||
[base_predicate_1.testSymbol](value, main, label, stack) { | ||
[base_predicate_1.testSymbol](value, main, label) { | ||
const errors = new Map(); | ||
for (const predicate of this.predicates) { | ||
try { | ||
main(value, label, predicate, stack); | ||
main(value, label, predicate); | ||
return; | ||
@@ -43,10 +43,4 @@ } | ||
const alreadyPresent = errors.get(key); | ||
// If they are present already, create a unique set with both current and new values. | ||
if (alreadyPresent) { | ||
errors.set(key, [...new Set([...alreadyPresent, ...value])]); | ||
} | ||
else { | ||
// Add the errors found as is to the map. | ||
errors.set(key, value); | ||
} | ||
// Add all errors under the same key | ||
errors.set(key, new Set([...alreadyPresent !== null && alreadyPresent !== void 0 ? alreadyPresent : [], ...value])); | ||
} | ||
@@ -59,3 +53,3 @@ } | ||
const message = generate_argument_error_message_1.generateArgumentErrorMessage(errors, true); | ||
throw new argument_error_1.ArgumentError(`Any predicate failed with the following errors:\n${message}`, main, stack, errors); | ||
throw new argument_error_1.ArgumentError(`Any predicate failed with the following errors:\n${message}`, main, errors); | ||
} | ||
@@ -62,0 +56,0 @@ } |
@@ -75,2 +75,13 @@ import { BasePredicate } from './base-predicate'; | ||
ofType<U extends T>(predicate: BasePredicate<U>): ArrayPredicate<U>; | ||
/** | ||
Test if the elements in the array exactly matches the elements placed at the same indices in the predicates array. | ||
@param predicates - Predicates to test the array against. Describes what the tested array should look like. | ||
@example | ||
``` | ||
ow(['1', 2], ow.array.exactShape([ow.string, ow.number])); | ||
``` | ||
*/ | ||
exactShape(predicates: Predicate[]): this; | ||
} |
@@ -7,2 +7,3 @@ "use strict"; | ||
const __1 = require(".."); | ||
const match_shape_1 = require("../utils/match-shape"); | ||
class ArrayPredicate extends predicate_1.Predicate { | ||
@@ -152,3 +153,20 @@ /** | ||
} | ||
/** | ||
Test if the elements in the array exactly matches the elements placed at the same indices in the predicates array. | ||
@param predicates - Predicates to test the array against. Describes what the tested array should look like. | ||
@example | ||
``` | ||
ow(['1', 2], ow.array.exactShape([ow.string, ow.number])); | ||
``` | ||
*/ | ||
exactShape(predicates) { | ||
const shape = predicates; | ||
return this.addValidator({ | ||
message: (_, label, message) => `${message.replace('Expected', 'Expected element')} in ${label}`, | ||
validator: object => match_shape_1.exact(object, shape, undefined, true) | ||
}); | ||
} | ||
} | ||
exports.ArrayPredicate = ArrayPredicate; |
@@ -14,3 +14,3 @@ import { Main } from '..'; | ||
export interface BasePredicate<T = unknown> { | ||
[testSymbol](value: T, main: Main, label: string | Function, stack: string): void; | ||
[testSymbol](value: T, main: Main, label: string | Function): void; | ||
} |
@@ -62,3 +62,3 @@ import { BasePredicate, testSymbol } from './base-predicate'; | ||
*/ | ||
[testSymbol](value: T, main: Main, label: string | Function, stack: string): asserts value is T; | ||
[testSymbol](value: T, main: Main, label: string | Function): asserts value is T; | ||
/** | ||
@@ -65,0 +65,0 @@ @hidden |
@@ -56,3 +56,3 @@ "use strict"; | ||
*/ | ||
[base_predicate_1.testSymbol](value, main, label, stack) { | ||
[base_predicate_1.testSymbol](value, main, label) { | ||
// Create a map of labels -> received errors. | ||
@@ -87,9 +87,7 @@ const errors = new Map(); | ||
// If we don't already have this error logged, add it. | ||
if (!currentErrors.includes(errorMessage)) { | ||
currentErrors.push(errorMessage); | ||
} | ||
currentErrors.add(errorMessage); | ||
} | ||
else { | ||
// Set this label and error in the full map. | ||
errors.set(mapKey, [errorMessage]); | ||
errors.set(mapKey, new Set([errorMessage])); | ||
} | ||
@@ -101,3 +99,3 @@ } | ||
const message = generate_argument_error_message_1.generateArgumentErrorMessage(errors); | ||
throw new argument_error_1.ArgumentError(message, main, stack, errors); | ||
throw new argument_error_1.ArgumentError(message, main, errors); | ||
} | ||
@@ -104,0 +102,0 @@ } |
@@ -11,2 +11,2 @@ import { BasePredicate } from './predicates/base-predicate'; | ||
*/ | ||
export default function test<T>(value: T, label: string | Function, predicate: BasePredicate<T>, stack: string): void; | ||
export default function test<T>(value: T, label: string | Function, predicate: BasePredicate<T>): void; |
@@ -13,5 +13,5 @@ "use strict"; | ||
*/ | ||
function test(value, label, predicate, stack) { | ||
predicate[base_predicate_1.testSymbol](value, test, label, stack); | ||
function test(value, label, predicate) { | ||
predicate[base_predicate_1.testSymbol](value, test, label); | ||
} | ||
exports.default = test; |
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare const generateArgumentErrorMessage: (errors: Map<string, string[]>, isAny?: boolean) => string; | ||
export declare const generateArgumentErrorMessage: (errors: Map<string, Set<string>>, isAny?: boolean) => string; |
@@ -13,9 +13,10 @@ "use strict"; | ||
const message = []; | ||
const errorArray = [...errors.values()]; | ||
const anyErrorWithoutOneItemOnly = errorArray.some(array => array.length !== 1); | ||
const errorArray = [...errors.entries()]; | ||
const anyErrorWithoutOneItemOnly = errorArray.some(([, array]) => array.size !== 1); | ||
// If only one error "key" is present, enumerate all of those errors only. | ||
if (errors.size === 1) { | ||
const returnedErrors = errorArray[0]; | ||
if (!isAny && returnedErrors.length === 1) { | ||
return returnedErrors[0]; | ||
if (errorArray.length === 1) { | ||
const [, returnedErrors] = errorArray[0]; | ||
if (!isAny && returnedErrors.size === 1) { | ||
const [errorMessage] = returnedErrors; | ||
return errorMessage; | ||
} | ||
@@ -29,6 +30,6 @@ for (const entry of returnedErrors) { | ||
if (!anyErrorWithoutOneItemOnly) { | ||
return errorArray.map(([item]) => ` - ${item}`).join('\n'); | ||
return errorArray.map(([, [item]]) => ` - ${item}`).join('\n'); | ||
} | ||
// Else, iterate through all the errors and enumerate them. | ||
for (const [key, value] of errors) { | ||
for (const [key, value] of errorArray) { | ||
message.push(`Errors from the "${key}" predicate:`); | ||
@@ -35,0 +36,0 @@ for (const entry of value) { |
@@ -44,2 +44,2 @@ import { BasePredicate } from '..'; | ||
*/ | ||
export declare function exact(object: Record<string, any>, shape: Shape, parent?: string): boolean | string; | ||
export declare function exact(object: Record<string, any>, shape: Shape, parent?: string, isArray?: boolean): boolean | string; |
@@ -7,3 +7,2 @@ "use strict"; | ||
const base_predicate_1 = require("../predicates/base-predicate"); | ||
const generate_stack_1 = require("./generate-stack"); | ||
/** | ||
@@ -19,3 +18,2 @@ Test if the `object` matches the `shape` partially. | ||
function partial(object, shape, parent) { | ||
const stack = generate_stack_1.generateStackTrace(); | ||
try { | ||
@@ -25,3 +23,3 @@ for (const key of Object.keys(shape)) { | ||
if (base_predicate_1.isPredicate(shape[key])) { | ||
test_1.default(object[key], label, shape[key], stack); | ||
test_1.default(object[key], label, shape[key]); | ||
} | ||
@@ -51,4 +49,3 @@ else if (is_1.default.plainObject(shape[key])) { | ||
*/ | ||
function exact(object, shape, parent) { | ||
const stack = generate_stack_1.generateStackTrace(); | ||
function exact(object, shape, parent, isArray) { | ||
try { | ||
@@ -60,3 +57,3 @@ const objectKeys = new Set(Object.keys(object)); | ||
if (base_predicate_1.isPredicate(shape[key])) { | ||
test_1.default(object[key], label, shape[key], stack); | ||
test_1.default(object[key], label, shape[key]); | ||
} | ||
@@ -76,3 +73,3 @@ else if (is_1.default.plainObject(shape[key])) { | ||
const label = parent ? `${parent}.${firstKey}` : firstKey; | ||
return `Did not expect property \`${label}\` to exist, got \`${object[firstKey]}\``; | ||
return `Did not expect ${isArray ? 'element' : 'property'} \`${label}\` to exist, got \`${object[firstKey]}\``; | ||
} | ||
@@ -79,0 +76,0 @@ return true; |
{ | ||
"name": "ow", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "Function argument validation for humans", | ||
@@ -22,2 +22,3 @@ "license": "MIT", | ||
"prepublishOnly": "npm run build", | ||
"postpublish": "gh-pages --dist docs --no-history --message \"Deploy documentation\"", | ||
"example": "npm run build && node example.js" | ||
@@ -65,2 +66,3 @@ }, | ||
"expect-type": "^0.11.0", | ||
"gh-pages": "^3.1.0", | ||
"nyc": "^15.1.0", | ||
@@ -67,0 +69,0 @@ "ts-node": "^9.1.1", |
@@ -146,3 +146,3 @@ <p align="center"> | ||
- [`undefined`](https://sindresorhus.com/ow/interfaces/ow.html#undefined) | ||
- [`undefined`](https://sindresorhus.com/ow/interfaces/ow.html#undefined-1) | ||
- [`null`](https://sindresorhus.com/ow/interfaces/ow.html#null) | ||
@@ -152,3 +152,3 @@ - [`string`](https://sindresorhus.com/ow/classes/stringpredicate.html) | ||
- [`boolean`](https://sindresorhus.com/ow/classes/booleanpredicate.html) | ||
- [`symbol`](https://sindresorhus.com/ow/interfaces/ow.html#symbol) | ||
- [`symbol`](https://sindresorhus.com/ow/interfaces/ow.html#symbol-1) | ||
@@ -161,6 +161,6 @@ #### Built-in types | ||
- [`object`](https://sindresorhus.com/ow/classes/objectpredicate.html) | ||
- [`regExp`](https://sindresorhus.com/ow/interfaces/ow.html#regexp) | ||
- [`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) | ||
- [`promise`](https://sindresorhus.com/ow/interfaces/ow.html#promise-1) | ||
- [`map`](https://sindresorhus.com/ow/classes/mappredicate.html) | ||
@@ -174,8 +174,8 @@ - [`set`](https://sindresorhus.com/ow/classes/setpredicate.html) | ||
- [`int8Array`](https://sindresorhus.com/ow/interfaces/ow.html#int8Array) | ||
- [`uint8Array`](https://sindresorhus.com/ow/interfaces/ow.html#uint8Array) | ||
- [`uint8ClampedArray`](https://sindresorhus.com/ow/interfaces/ow.html#uint8ClampedArray) | ||
- [`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) | ||
- [`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) | ||
- [`uint32Array`](https://sindresorhus.com/ow/interfaces/ow.html#uin32Array-1) | ||
- [`float32Array`](https://sindresorhus.com/ow/interfaces/ow.html#float32Array) | ||
@@ -188,2 +188,3 @@ - [`float64Array`](https://sindresorhus.com/ow/interfaces/ow.html#float64Array) | ||
- [`dataView`](https://sindresorhus.com/ow/interfaces/ow.html#dataview) | ||
- [`sharedArrayBuffer`](https://sindresorhus.com/ow/interfaces/ow.html#sharedarraybuffer-1) | ||
@@ -195,3 +196,3 @@ #### Miscellaneous | ||
- [`iterable`](https://sindresorhus.com/ow/interfaces/ow.html#iterable) | ||
- [`typedArray`](https://sindresorhus.com/ow/interfaces/ow.html#typedarray) | ||
- [`typedArray`](https://sindresorhus.com/ow/interfaces/ow.html#typedarray-1) | ||
@@ -198,0 +199,0 @@ ### Predicates |
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
135186
3272
299
13