Comparing version 1.1.1 to 2.0.0
@@ -7,10 +7,5 @@ import { generateStackTrace } from './utils/generate-stack.js'; | ||
export class ArgumentError extends Error { | ||
validationErrors; | ||
constructor(message, context, errors = new Map()) { | ||
super(message); | ||
Object.defineProperty(this, "validationErrors", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this.name = 'ArgumentError'; | ||
@@ -17,0 +12,0 @@ if (Error.captureStackTrace) { |
@@ -1,2 +0,2 @@ | ||
import type { Predicate } from '../predicates/predicate.js'; | ||
import { type Predicate } from '../predicates/predicate.js'; | ||
/** | ||
@@ -3,0 +3,0 @@ Operator which inverts the following validation. |
@@ -13,3 +13,3 @@ import randomId from '../utils/random-id.js'; | ||
predicate.addValidator = (validator) => { | ||
const { validator: fn, message, negatedMessage } = validator; | ||
const { validator: function_, message, negatedMessage } = validator; | ||
const placeholder = randomId(); | ||
@@ -19,3 +19,3 @@ validator.message = (value, label) => (negatedMessage | ||
: message(value, placeholder).replace(/ to /, '$¬ ').replace(placeholder, label)); | ||
validator.validator = (value) => !fn(value); | ||
validator.validator = (value) => !function_(value); | ||
predicate[validatorSymbol].push(validator); | ||
@@ -22,0 +22,0 @@ predicate.addValidator = originalAddValidator; |
@@ -8,15 +8,7 @@ import { ArgumentError } from '../argument-error.js'; | ||
export class AnyPredicate { | ||
predicates; | ||
options; | ||
constructor(predicates, options = {}) { | ||
Object.defineProperty(this, "predicates", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: predicates | ||
}); | ||
Object.defineProperty(this, "options", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: options | ||
}); | ||
this.predicates = predicates; | ||
this.options = options; | ||
} | ||
@@ -23,0 +15,0 @@ [testSymbol](value, main, label, idLabel) { |
@@ -1,2 +0,2 @@ | ||
import isEqual from 'lodash.isequal'; | ||
import { deepEqual } from 'fast-equals'; | ||
import { exact } from '../utils/match-shape.js'; | ||
@@ -65,4 +65,4 @@ import ofType from '../utils/of-type.js'; | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to end with \`${searchElement}\`, got \`${value[value.length - 1]}\``, | ||
validator: value => value[value.length - 1] === searchElement, | ||
message: (value, label) => `Expected ${label} to end with \`${searchElement}\`, got \`${value.at(-1)}\``, | ||
validator: value => value.at(-1) === searchElement, | ||
}); | ||
@@ -118,3 +118,3 @@ } | ||
message: (value, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify(expected)}\`, got \`${JSON.stringify(value)}\``, | ||
validator: value => isEqual(value, expected), | ||
validator: value => deepEqual(value, expected), | ||
}); | ||
@@ -133,4 +133,3 @@ } | ||
ofType(predicate) { | ||
// TODO [typescript@>=5] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
// TODO [typescript@>=6] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
return this.addValidator({ | ||
@@ -137,0 +136,0 @@ message: (_, label, error) => `(${label}) ${error}`, |
@@ -50,3 +50,3 @@ import { Predicate } from './predicate.js'; | ||
message: (_, label) => `Expected ${label} message to have keys \`${keys.join('`, `')}\``, | ||
validator: error => keys.every(key => Object.prototype.hasOwnProperty.call(error, key)), | ||
validator: error => keys.every(key => Object.hasOwn(error, key)), | ||
}); | ||
@@ -53,0 +53,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import isEqual from 'lodash.isequal'; | ||
import { deepEqual } from 'fast-equals'; | ||
import hasItems from '../utils/has-items.js'; | ||
@@ -142,5 +142,5 @@ import ofType from '../utils/of-type.js'; | ||
message: (map, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify([...expected])}\`, got \`${JSON.stringify([...map])}\``, | ||
validator: map => isEqual(map, expected), | ||
validator: map => deepEqual(map, expected), | ||
}); | ||
} | ||
} |
@@ -159,3 +159,3 @@ import is from '@sindresorhus/is'; | ||
get uint16() { | ||
return this.integer.inRange(0, 65535); | ||
return this.integer.inRange(0, 65_535); | ||
} | ||
@@ -166,3 +166,3 @@ /** | ||
get uint32() { | ||
return this.integer.inRange(0, 4294967295); | ||
return this.integer.inRange(0, 4_294_967_295); | ||
} | ||
@@ -179,3 +179,3 @@ /** | ||
get int16() { | ||
return this.integer.inRange(-32768, 32767); | ||
return this.integer.inRange(-32_768, 32_767); | ||
} | ||
@@ -186,4 +186,4 @@ /** | ||
get int32() { | ||
return this.integer.inRange(-2147483648, 2147483647); | ||
return this.integer.inRange(-2_147_483_648, 2_147_483_647); | ||
} | ||
} |
import is from '@sindresorhus/is'; | ||
import { hasProperty } from 'dot-prop'; | ||
import isEqual from 'lodash.isequal'; | ||
import { deepEqual } from 'fast-equals'; | ||
import hasItems from '../utils/has-items.js'; | ||
import ofType from '../utils/of-type.js'; | ||
import ofTypeDeep from '../utils/of-type-deep.js'; | ||
import { partial, exact } from '../utils/match-shape.js'; | ||
import { partial, exact, } from '../utils/match-shape.js'; | ||
import { Predicate } from './predicate.js'; | ||
@@ -73,3 +73,3 @@ export class ObjectPredicate extends Predicate { | ||
message: (object, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify(expected)}\`, got \`${JSON.stringify(object)}\``, | ||
validator: object => isEqual(object, expected), | ||
validator: object => deepEqual(object, expected), | ||
}); | ||
@@ -164,4 +164,3 @@ } | ||
exactShape(shape) { | ||
// TODO [typescript@>=5] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
// TODO [typescript@>=6] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
return this.addValidator({ | ||
@@ -168,0 +167,0 @@ // TODO: Improve this when message handling becomes smarter |
@@ -14,23 +14,10 @@ import is from '@sindresorhus/is'; | ||
export class Predicate { | ||
type; | ||
options; | ||
context = { | ||
validators: [], | ||
}; | ||
constructor(type, options = {}) { | ||
Object.defineProperty(this, "type", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: type | ||
}); | ||
Object.defineProperty(this, "options", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: options | ||
}); | ||
Object.defineProperty(this, "context", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: { | ||
validators: [], | ||
} | ||
}); | ||
this.type = type; | ||
this.options = options; | ||
this.context = { | ||
@@ -49,3 +36,3 @@ ...this.context, | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
validator: value => is[typeString](value), | ||
@@ -121,3 +108,2 @@ }); | ||
return this.addValidator({ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
message: (_, label, error) => typeof error === 'string' | ||
@@ -168,3 +154,3 @@ ? `(${label}) ${error}` | ||
const { validators } = this.context; | ||
validators[validators.length - 1].message = (value, label) => { | ||
validators.at(-1).message = (value, label) => { | ||
if (typeof newMessage === 'function') { | ||
@@ -171,0 +157,0 @@ return newMessage(value, label); |
@@ -1,2 +0,2 @@ | ||
import isEqual from 'lodash.isequal'; | ||
import { deepEqual } from 'fast-equals'; | ||
import hasItems from '../utils/has-items.js'; | ||
@@ -106,5 +106,5 @@ import ofType from '../utils/of-type.js'; | ||
message: (set, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify([...expected])}\`, got \`${JSON.stringify([...set])}\``, | ||
validator: set => isEqual(set, expected), | ||
validator: set => deepEqual(set, expected), | ||
}); | ||
} | ||
} |
import is from '@sindresorhus/is'; | ||
import valiDate from 'vali-date'; | ||
import { Predicate } from './predicate.js'; | ||
@@ -126,8 +125,8 @@ export class StringPredicate extends Predicate { | ||
const madeVisible = value | ||
.replace(/ /g, '·') | ||
.replace(/\f/g, '\\f') | ||
.replace(/\n/g, '\\n') | ||
.replace(/\r/g, '\\r') | ||
.replace(/\t/g, '\\t') | ||
.replace(/\v/g, '\\v'); | ||
.replaceAll(' ', '·') | ||
.replaceAll('\f', '\\f') | ||
.replaceAll('\n', '\\n') | ||
.replaceAll('\r', '\\r') | ||
.replaceAll('\t', '\\t') | ||
.replaceAll('\v', '\\v'); | ||
return `Expected ${label} to not be only whitespace, got \`${madeVisible}\``; | ||
@@ -191,3 +190,3 @@ }, | ||
message: (value, label) => `Expected ${label} to be a date, got \`${value}\``, | ||
validator: valiDate, | ||
validator: value => is.validDate(new Date(value)), | ||
}); | ||
@@ -194,0 +193,0 @@ } |
import fs from 'node:fs'; | ||
import isValidIdentifier from './is-valid-identifier.js'; | ||
import isNode from './node/is-node.js'; | ||
import { isNode } from 'environment'; | ||
import isIdentifier from 'is-identifier'; | ||
// Regex to extract the label out of the `ow` function call | ||
@@ -48,3 +48,4 @@ const labelRegex = /^.*?\((?<label>.*?)[,)]/; | ||
} | ||
if (isValidIdentifier(token) || isValidIdentifier(token.split('.').pop() ?? '')) { | ||
// @ts-expect-error - Doesn't seem like something I can work around. | ||
if (isIdentifier(token) || isIdentifier(token.split('.').pop() ?? '')) { // eslint-disable-line @typescript-eslint/no-unsafe-call | ||
return token; | ||
@@ -51,0 +52,0 @@ } |
@@ -22,5 +22,7 @@ import type { BasePredicate } from '../index.js'; | ||
*/ | ||
export type TypeOfShape<S extends BasePredicate | Shape> = S extends BasePredicate<infer X> ? X : S extends Shape ? { | ||
export type TypeOfShape<S extends BasePredicate | Shape> = S extends BasePredicate<infer X> ? X extends object ? X : never : S extends Shape ? { | ||
[K in keyof S]: TypeOfShape<S[K]>; | ||
} : never; | ||
} extends object ? { | ||
[K in keyof S]: TypeOfShape<S[K]>; | ||
} : never : never; | ||
/** | ||
@@ -27,0 +29,0 @@ Test if the `object` matches the `shape` partially. |
@@ -52,3 +52,3 @@ import is from '@sindresorhus/is'; | ||
else if (is.plainObject(shape[key])) { | ||
if (!Object.prototype.hasOwnProperty.call(object, key)) { | ||
if (!Object.hasOwn(object, key)) { | ||
return `Expected \`${label}\` to exist`; | ||
@@ -55,0 +55,0 @@ } |
{ | ||
"name": "ow", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Function argument validation for humans", | ||
@@ -26,6 +26,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=14.16" | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "xo && c8 ava", | ||
"test": "xo && NODE_OPTIONS='--import=tsx/esm' c8 ava", | ||
"docs": "typedoc source/index.ts", | ||
@@ -62,22 +62,21 @@ "build": "del-cli dist && tsc", | ||
"dependencies": { | ||
"@sindresorhus/is": "^5.3.0", | ||
"callsites": "^4.0.0", | ||
"dot-prop": "^7.2.0", | ||
"lodash.isequal": "^4.5.0", | ||
"vali-date": "^1.0.0" | ||
"@sindresorhus/is": "^6.3.0", | ||
"callsites": "^4.1.0", | ||
"dot-prop": "^8.0.2", | ||
"environment": "^1.0.0", | ||
"fast-equals": "^5.0.1", | ||
"is-identifier": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^3.0.1", | ||
"@types/lodash.isequal": "^4.5.6", | ||
"@types/node": "^18.8.0", | ||
"@types/vali-date": "^1.0.0", | ||
"ava": "^4.3.3", | ||
"c8": "^7.12.0", | ||
"del-cli": "^5.0.0", | ||
"expect-type": "^0.14.2", | ||
"gh-pages": "^4.0.0", | ||
"ts-node": "^10.9.1", | ||
"typedoc": "^0.23.15", | ||
"typescript": "^4.8.4", | ||
"xo": "^0.52.3" | ||
"@sindresorhus/tsconfig": "^5.0.0", | ||
"@types/node": "^20.12.8", | ||
"ava": "^6.1.2", | ||
"c8": "^9.1.0", | ||
"del-cli": "^5.1.0", | ||
"expect-type": "^0.19.0", | ||
"gh-pages": "^6.1.1", | ||
"tsx": "^4.9.1", | ||
"typedoc": "^0.25.13", | ||
"typescript": "^5.4.5", | ||
"xo": "^0.58.0" | ||
}, | ||
@@ -99,3 +98,5 @@ "browser": { | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/restrict-template-expressions": "off" | ||
"@typescript-eslint/restrict-template-expressions": "off", | ||
"@typescript-eslint/no-unsafe-return": "off", | ||
"@typescript-eslint/no-unsafe-argument": "off" | ||
} | ||
@@ -111,5 +112,3 @@ }, | ||
}, | ||
"nodeArguments": [ | ||
"--loader=ts-node/esm" | ||
] | ||
"workerThreads": false | ||
}, | ||
@@ -116,0 +115,0 @@ "c8": { |
@@ -13,2 +13,4 @@ <p align="center"> | ||
For schema validation, I recommend [`zod`](https://github.com/colinhacks/zod). | ||
## Highlights | ||
@@ -309,10 +311,2 @@ | ||
## Maintainers | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
**Former:** | ||
- [Sam Verschueren](https://github.com/SamVerschueren) | ||
## Related | ||
@@ -319,0 +313,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
11
126624
6
72
3183
314
+ Addedenvironment@^1.0.0
+ Addedfast-equals@^5.0.1
+ Addedis-identifier@^1.0.0
+ Added@sindresorhus/is@6.3.1(transitive)
+ Addedconvert-hrtime@5.0.0(transitive)
+ Addeddot-prop@8.0.2(transitive)
+ Addedenvironment@1.1.0(transitive)
+ Addedfast-equals@5.0.1(transitive)
+ Addedfunction-timeout@1.0.2(transitive)
+ Addedidentifier-regex@1.0.0(transitive)
+ Addedis-identifier@1.0.1(transitive)
+ Addedreserved-identifiers@1.0.0(transitive)
+ Addedsuper-regex@1.0.0(transitive)
+ Addedtime-span@5.1.0(transitive)
+ Addedtype-fest@3.13.1(transitive)
- Removedlodash.isequal@^4.5.0
- Removedvali-date@^1.0.0
- Removed@sindresorhus/is@5.6.0(transitive)
- Removeddot-prop@7.2.0(transitive)
- Removedlodash.isequal@4.5.0(transitive)
- Removedtype-fest@2.19.0(transitive)
- Removedvali-date@1.0.0(transitive)
Updated@sindresorhus/is@^6.3.0
Updatedcallsites@^4.1.0
Updateddot-prop@^8.0.2