@frankmeza/affirm 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +130 -0
- package/LICENSE +21 -0
- package/README.md +116 -2
- package/dist/affirm.d.ts +79 -0
- package/dist/checks/arrays.d.ts +10 -0
- package/dist/checks/composition.d.ts +10 -0
- package/dist/checks/equality.d.ts +7 -0
- package/dist/checks/nullish.d.ts +9 -0
- package/dist/checks/numbers.d.ts +11 -0
- package/dist/checks/objects.d.ts +9 -0
- package/dist/checks/runtime-type.d.ts +14 -0
- package/dist/checks/size.d.ts +8 -0
- package/dist/checks/strings.d.ts +6 -0
- package/dist/core/check-def.d.ts +93 -0
- package/dist/core/describe.d.ts +11 -0
- package/dist/core/errors.d.ts +18 -0
- package/dist/core/evaluate.d.ts +8 -0
- package/dist/core/registry.d.ts +18 -0
- package/dist/core/report.d.ts +22 -0
- package/dist/core/util/deep-equal.d.ts +12 -0
- package/dist/core/util/kind.d.ts +22 -0
- package/dist/core/util/levenshtein.d.ts +3 -0
- package/dist/core/util/same-value-zero.d.ts +5 -0
- package/dist/core/util/standard-schema.d.ts +62 -0
- package/dist/core/validate-params.d.ts +8 -0
- package/dist/eslint/check-keys.d.ts +7 -0
- package/dist/eslint/const-params.d.ts +2 -0
- package/dist/eslint/guard-naming.d.ts +2 -0
- package/dist/eslint/index.d.ts +29 -0
- package/dist/eslint/no-negated-affirm.d.ts +2 -0
- package/dist/eslint/shared.d.ts +65 -0
- package/dist/eslint.js +734 -0
- package/dist/eslint.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +967 -0
- package/dist/index.js.map +1 -0
- package/dist/types/affirmed.d.ts +55 -0
- package/dist/types/arrays.d.ts +64 -0
- package/dist/types/composition.d.ts +53 -0
- package/dist/types/contradictions.d.ts +90 -0
- package/dist/types/equality.d.ts +65 -0
- package/dist/types/narrowing.d.ts +66 -0
- package/dist/types/nullish.d.ts +80 -0
- package/dist/types/numbers.d.ts +94 -0
- package/dist/types/objects.d.ts +99 -0
- package/dist/types/params.d.ts +14 -0
- package/dist/types/runtime-type.d.ts +129 -0
- package/dist/types/size.d.ts +76 -0
- package/dist/types/strings.d.ts +49 -0
- package/docs/client-usage.md +850 -0
- package/package.json +58 -4
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { BoolInfo, Exact, ParamOf, Refine } from './narrowing';
|
|
2
|
+
export interface NullishParams {
|
|
3
|
+
/**
|
|
4
|
+
* Value is `null` (`true`) or is not `null` (`false`).
|
|
5
|
+
*
|
|
6
|
+
* Passes when `(value === null) === param`. `undefined` is not `null`.
|
|
7
|
+
* Narrows to `null` / `{} | undefined`, exact in both polarities.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* if (affirm(user.deletedAt, { isNull: true })) { … } // never deleted
|
|
11
|
+
*/
|
|
12
|
+
isNull?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Value is `undefined` (`true`) or is not `undefined` (`false`).
|
|
15
|
+
*
|
|
16
|
+
* Passes when `(value === undefined) === param`. Narrows to `undefined` / `{} | null`, exact.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* affirm(options.timeout, { isUndefined: true })
|
|
20
|
+
*/
|
|
21
|
+
isUndefined?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Value is `null` or `undefined` (`true`), or neither (`false`).
|
|
24
|
+
*
|
|
25
|
+
* Passes when `(value == null) === param`. Narrows to `null | undefined` / `{}`, exact.
|
|
26
|
+
* For "null or empty", use `anyOf: [{ isNullish: true }, { isEmpty: true }]`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* if (affirm(input, { isNullish: true })) return defaultValue
|
|
30
|
+
*/
|
|
31
|
+
isNullish?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Value is not `undefined` (`true`), or is `undefined` (`false`). `null` counts as defined.
|
|
34
|
+
*
|
|
35
|
+
* Passes when `(value !== undefined) === param`. Narrows to `{} | null` / `undefined`, exact.
|
|
36
|
+
* To exclude both `null` and `undefined`, use `isPresent`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* affirm(patch.nickname, { isDefined: true }) // null allowed: it clears the nickname
|
|
40
|
+
*/
|
|
41
|
+
isDefined?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Value is neither `null` nor `undefined` (`true`), or is one of them (`false`).
|
|
44
|
+
*
|
|
45
|
+
* Passes when `(value != null) === param`. Narrows to `{}` / `null | undefined`, exact.
|
|
46
|
+
* `0`, `''` and `false` are present.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* if (affirm(user, { isPresent: true })) { user.id } // user: User (no longer User | null)
|
|
50
|
+
*/
|
|
51
|
+
isPresent?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Value is truthy (`true`) or falsy (`false`).
|
|
54
|
+
*
|
|
55
|
+
* Passes when `Boolean(value) === param`. `true` narrows to `{}` (refined: the `else` branch
|
|
56
|
+
* keeps everything, since `0` and `''` are present but falsy). `false` does not narrow.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* affirm(token, { isTruthy: true })
|
|
60
|
+
*/
|
|
61
|
+
isTruthy?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Value is falsy (`true`) or truthy (`false`): `false`, `0`, `-0`, `0n`, `''`, `null`, `undefined`, `NaN`.
|
|
64
|
+
*
|
|
65
|
+
* Passes when `!value === param`. `false` narrows to `{}` (refined); `true` does not narrow.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* affirm(errorMessage, { isFalsy: true })
|
|
69
|
+
*/
|
|
70
|
+
isFalsy?: boolean;
|
|
71
|
+
}
|
|
72
|
+
export type NullishInfos<P> = [
|
|
73
|
+
BoolInfo<ParamOf<P, 'isNull'>, Exact<null>, Exact<{} | undefined>>,
|
|
74
|
+
BoolInfo<ParamOf<P, 'isUndefined'>, Exact<undefined>, Exact<{} | null>>,
|
|
75
|
+
BoolInfo<ParamOf<P, 'isNullish'>, Exact<null | undefined>, Exact<{}>>,
|
|
76
|
+
BoolInfo<ParamOf<P, 'isDefined'>, Exact<{} | null>, Exact<undefined>>,
|
|
77
|
+
BoolInfo<ParamOf<P, 'isPresent'>, Exact<{}>, Exact<null | undefined>>,
|
|
78
|
+
BoolInfo<ParamOf<P, 'isTruthy'>, Refine<{}>, Refine>,
|
|
79
|
+
BoolInfo<ParamOf<P, 'isFalsy'>, Refine, Refine<{}>>
|
|
80
|
+
];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { BoolInfo, ParamInfo, ParamOf, Refine } from './narrowing';
|
|
2
|
+
export interface NumberParams {
|
|
3
|
+
/**
|
|
4
|
+
* Value is a number greater than n.
|
|
5
|
+
*
|
|
6
|
+
* Passes when the value is a number (not `NaN`) and `value > n`. No coercion: `'10'` fails.
|
|
7
|
+
* Narrows to `number` (refined).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* affirm(age, { isGreaterThan: 17 })
|
|
11
|
+
*/
|
|
12
|
+
isGreaterThan?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Value is a number greater than or equal to n.
|
|
15
|
+
*
|
|
16
|
+
* Passes when the value is a number (not `NaN`) and `value >= n`. Narrows to `number` (refined).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* affirm(quantity, { isInteger: true, isAtLeast: 1 })
|
|
20
|
+
*/
|
|
21
|
+
isAtLeast?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Value is a number less than n.
|
|
24
|
+
*
|
|
25
|
+
* Passes when the value is a number (not `NaN`) and `value < n`. Narrows to `number` (refined).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* affirm(discount, { isLessThan: 1 })
|
|
29
|
+
*/
|
|
30
|
+
isLessThan?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Value is a number less than or equal to n.
|
|
33
|
+
*
|
|
34
|
+
* Passes when the value is a number (not `NaN`) and `value <= n`. Narrows to `number` (refined).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* affirm(retries, { isAtMost: 5 })
|
|
38
|
+
*/
|
|
39
|
+
isAtMost?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Value is a number between min and max, **inclusive at both ends**.
|
|
42
|
+
*
|
|
43
|
+
* Passes when the value is a number (not `NaN`) and `min <= value && value <= max`.
|
|
44
|
+
* `min > max` throws. Narrows to `number` (refined).
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* affirm(rating, { isInteger: true, isBetween: [1, 5] }) // 1 and 5 pass
|
|
48
|
+
*/
|
|
49
|
+
isBetween?: readonly [min: number, max: number];
|
|
50
|
+
/**
|
|
51
|
+
* Value is a positive number (`true`), or a number that is not positive (`false`).
|
|
52
|
+
*
|
|
53
|
+
* Passes when the value is a number (not `NaN`) and `(value > 0) === param`. `0` is not positive.
|
|
54
|
+
* Non-numbers fail both polarities. Narrows to `number` (refined).
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* affirm(order.total, { isPositive: true })
|
|
58
|
+
*/
|
|
59
|
+
isPositive?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Value is a negative number (`true`), or a number that is not negative (`false`).
|
|
62
|
+
*
|
|
63
|
+
* Passes when the value is a number (not `NaN`) and `(value < 0) === param`. `0` is not negative.
|
|
64
|
+
* Non-numbers fail both polarities. Narrows to `number` (refined).
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* affirm(balance, { isNegative: false })
|
|
68
|
+
*/
|
|
69
|
+
isNegative?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Value is a number that is a multiple of n.
|
|
72
|
+
*
|
|
73
|
+
* Passes when the value is a number (not `NaN`) and `value % n === 0`. n must be finite and
|
|
74
|
+
* non-zero. Uses `%`, so fractional divisors are unreliable (`0.3 % 0.1 !== 0`): scale to
|
|
75
|
+
* integers first. Narrows to `number` (refined).
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* affirm(minutes, { isMultipleOf: 15 })
|
|
79
|
+
*/
|
|
80
|
+
isMultipleOf?: number;
|
|
81
|
+
}
|
|
82
|
+
type NumberInfo<V> = ParamInfo<V, Refine<number>>;
|
|
83
|
+
type NumberBool<V> = BoolInfo<V, Refine<number>, Refine<number>>;
|
|
84
|
+
export type NumberInfos<P> = [
|
|
85
|
+
NumberInfo<ParamOf<P, 'isGreaterThan'>>,
|
|
86
|
+
NumberInfo<ParamOf<P, 'isAtLeast'>>,
|
|
87
|
+
NumberInfo<ParamOf<P, 'isLessThan'>>,
|
|
88
|
+
NumberInfo<ParamOf<P, 'isAtMost'>>,
|
|
89
|
+
NumberInfo<ParamOf<P, 'isBetween'>>,
|
|
90
|
+
NumberBool<ParamOf<P, 'isPositive'>>,
|
|
91
|
+
NumberBool<ParamOf<P, 'isNegative'>>,
|
|
92
|
+
NumberInfo<ParamOf<P, 'isMultipleOf'>>
|
|
93
|
+
];
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { NarrowNested } from './affirmed';
|
|
2
|
+
import type { IsLiteral, IsUnion, ParamInfo, ParamOf, Refine } from './narrowing';
|
|
3
|
+
import type { AffirmParams } from './params';
|
|
4
|
+
export interface ObjectParams {
|
|
5
|
+
/**
|
|
6
|
+
* Each listed field of the value passes its own params.
|
|
7
|
+
*
|
|
8
|
+
* Passes when the value is a non-null object (or array) and every listed field passes. A missing
|
|
9
|
+
* or inherited field is checked as `undefined`, so `{ isUndefined: true }` accepts it. Unlisted
|
|
10
|
+
* fields are ignored (add `hasOnlyKeys` to forbid them). Failures report the field path, e.g.
|
|
11
|
+
* `address.zip`. Narrows to `{ [field]: <narrowed field type> }` (refined).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* affirm(order, { shape: { total: { isType: 'number', isPositive: true }, id: { startsWith: 'ord_' } } })
|
|
15
|
+
*/
|
|
16
|
+
shape?: {
|
|
17
|
+
readonly [field: string]: AffirmParams;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Value is an object that has this own key.
|
|
21
|
+
*
|
|
22
|
+
* Passes when the value is a non-null object (or array) and `Object.hasOwn(value, key)`. Inherited
|
|
23
|
+
* keys and prototype getters don't count (use `satisfies`). A key set to `undefined` counts.
|
|
24
|
+
* A literal key narrows to `Record<key, unknown>` (refined).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* if (affirm(payload, { hasKey: 'total' })) { payload.total } // payload.total: unknown
|
|
28
|
+
*/
|
|
29
|
+
hasKey?: PropertyKey;
|
|
30
|
+
/**
|
|
31
|
+
* Value is an object that has every one of these own keys.
|
|
32
|
+
*
|
|
33
|
+
* Passes when the value is a non-null object (or array) and has each key (`Object.hasOwn`).
|
|
34
|
+
* `[]` passes. Literal keys narrow to `Record<keys, unknown>` (refined).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* affirm(row, { hasKeys: ['id', 'createdAt'] })
|
|
38
|
+
*/
|
|
39
|
+
hasKeys?: readonly PropertyKey[];
|
|
40
|
+
/**
|
|
41
|
+
* Value is an object that has at least one of these own keys.
|
|
42
|
+
*
|
|
43
|
+
* Passes when the value is a non-null object (or array) and has any of the keys. `[]` never passes.
|
|
44
|
+
* Narrows to `object` (refined).
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* affirm(contact, { hasAnyKey: ['email', 'phone'] })
|
|
48
|
+
*/
|
|
49
|
+
hasAnyKey?: readonly PropertyKey[];
|
|
50
|
+
/**
|
|
51
|
+
* Value is an object with no own enumerable keys outside this list.
|
|
52
|
+
*
|
|
53
|
+
* Passes when the value is a non-null object (or array) and every own enumerable key (string or
|
|
54
|
+
* symbol) is listed. Does not require the listed keys to be present. `1` and `'1'` are the same
|
|
55
|
+
* key. Narrows to `object` (refined).
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* affirm(query, { hasOnlyKeys: ['page', 'limit', 'sort'] })
|
|
59
|
+
*/
|
|
60
|
+
hasOnlyKeys?: readonly PropertyKey[];
|
|
61
|
+
/**
|
|
62
|
+
* Value has every segment of this dot path as an own key, at each level.
|
|
63
|
+
*
|
|
64
|
+
* Passes when each segment exists on an object or array along the way (`Object.hasOwn`). Numeric
|
|
65
|
+
* segments index arrays. The last segment may hold `undefined`. Empty segments (`'a..b'`) throw.
|
|
66
|
+
* A literal path narrows to the nested type, e.g. `{ items: { 0: { id: unknown } } }` (refined).
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* affirm(event, { hasPath: 'data.object.customer' })
|
|
70
|
+
*/
|
|
71
|
+
hasPath?: string;
|
|
72
|
+
}
|
|
73
|
+
/** `'a.b.0'` → `{ a: { b: { 0: unknown } } }` */
|
|
74
|
+
export type PathType<S extends string> = S extends `${infer Head}.${infer Rest}` ? {
|
|
75
|
+
[K in Head]: PathType<Rest>;
|
|
76
|
+
} : {
|
|
77
|
+
[K in S]: unknown;
|
|
78
|
+
};
|
|
79
|
+
/** A single literal key narrows to `Record<K, unknown>`; a wide or union key only to `object`. */
|
|
80
|
+
type HasKeyInfo<V> = ParamInfo<V, V extends PropertyKey ? IsLiteral<V> extends true ? IsUnion<V> extends true ? Refine<object> : Refine<Record<V, unknown>> : Refine<object> : Refine>;
|
|
81
|
+
/** All keys are required, so a union of literal keys is fine here. */
|
|
82
|
+
type HasKeysInfo<V> = ParamInfo<V, V extends readonly PropertyKey[] ? IsLiteral<V[number]> extends true ? Refine<Record<V[number], unknown>> : Refine<object> : Refine>;
|
|
83
|
+
type HasPathInfo<V> = ParamInfo<V, V extends string ? (IsLiteral<V> extends true ? (IsUnion<V> extends true ? Refine<object> : Refine<PathType<V>>) : Refine<object>) : Refine>;
|
|
84
|
+
/**
|
|
85
|
+
* `{ total: { isType: 'number' } }` → `{ total: number }`. Field types use `Narrow` (no brand):
|
|
86
|
+
* the outer call is always refined, so the brand on fields would only add noise.
|
|
87
|
+
*/
|
|
88
|
+
type ShapeInfo<V> = ParamInfo<V, V extends object ? Refine<{
|
|
89
|
+
-readonly [K in keyof V]: NarrowNested<V[K]>;
|
|
90
|
+
}> : Refine>;
|
|
91
|
+
export type ObjectInfos<P> = [
|
|
92
|
+
ShapeInfo<ParamOf<P, 'shape'>>,
|
|
93
|
+
HasKeyInfo<ParamOf<P, 'hasKey'>>,
|
|
94
|
+
HasKeysInfo<ParamOf<P, 'hasKeys'>>,
|
|
95
|
+
ParamInfo<ParamOf<P, 'hasAnyKey'>, Refine<object>>,
|
|
96
|
+
ParamInfo<ParamOf<P, 'hasOnlyKeys'>, Refine<object>>,
|
|
97
|
+
HasPathInfo<ParamOf<P, 'hasPath'>>
|
|
98
|
+
];
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ArrayParams } from './arrays';
|
|
2
|
+
import type { CompositionParams } from './composition';
|
|
3
|
+
import type { EqualityParams } from './equality';
|
|
4
|
+
import type { NullishParams } from './nullish';
|
|
5
|
+
import type { NumberParams } from './numbers';
|
|
6
|
+
import type { ObjectParams } from './objects';
|
|
7
|
+
import type { RuntimeTypeParams } from './runtime-type';
|
|
8
|
+
import type { SizeParams } from './size';
|
|
9
|
+
import type { StringParams } from './strings';
|
|
10
|
+
/**
|
|
11
|
+
* Every check `affirm` understands. Keys combine with AND; an omitted or `undefined` key is skipped.
|
|
12
|
+
*/
|
|
13
|
+
export interface AffirmParams extends EqualityParams, NullishParams, RuntimeTypeParams, SizeParams, NumberParams, StringParams, ObjectParams, ArrayParams, CompositionParams {
|
|
14
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '../core/util/standard-schema';
|
|
2
|
+
import type { BoolInfo, Exact, IsUnion, ParamInfo, ParamOf, Refine } from './narrowing';
|
|
3
|
+
export type TypeTag = 'string' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'function' | 'object';
|
|
4
|
+
/** A user type guard, e.g. `(v: unknown): v is User => …`. */
|
|
5
|
+
export type TypeGuard<T = unknown> = (value: unknown) => value is T;
|
|
6
|
+
/** What `isFunction: true` and `isType: 'function'` narrow to. */
|
|
7
|
+
export type AnyFunction = (...args: never[]) => unknown;
|
|
8
|
+
export interface RuntimeTypeParams {
|
|
9
|
+
/**
|
|
10
|
+
* Value has this runtime type: a `typeof` tag, a type guard, or a synchronous Standard Schema.
|
|
11
|
+
*
|
|
12
|
+
* Passes when the value matches the param:
|
|
13
|
+
* - Tag: passes when `typeof value === tag`, except `'object'` also requires non-null (arrays
|
|
14
|
+
* pass). `'function'` also matches classes (calling one without `new` throws). Exact, except `'object'`.
|
|
15
|
+
* - Guard `(v: unknown): v is T`: passes when it returns exactly `true`. Narrows to `T`, exact.
|
|
16
|
+
* - Standard Schema (Zod, Valibot, ArkType, …): passes when `validate` reports no issues. Narrows
|
|
17
|
+
* to the schema's *input* type, refined. Nothing is parsed or transformed. Async schemas throw.
|
|
18
|
+
*
|
|
19
|
+
* Not `'null'` / `'undefined'` (use `isNull` / `isUndefined`); not a class (use `isInstanceOf`).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* if (affirm(id, { isType: 'string' })) { id.trim() }
|
|
23
|
+
* if (affirm(body, { isType: OrderSchema })) { body.items } // a Zod schema
|
|
24
|
+
*/
|
|
25
|
+
isType?: TypeTag | TypeGuard | StandardSchemaV1;
|
|
26
|
+
/**
|
|
27
|
+
* Value is an array (`true`) or is not (`false`).
|
|
28
|
+
*
|
|
29
|
+
* Passes when `Array.isArray(value) === param`. `true` narrows to `readonly unknown[]` (or the
|
|
30
|
+
* array members of the input type), exact. `false` does not narrow.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* if (affirm(tags, { isArray: true })) { tags.length }
|
|
34
|
+
*/
|
|
35
|
+
isArray?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Value is a plain object (`true`) or is not (`false`).
|
|
38
|
+
*
|
|
39
|
+
* Passes when the prototype is `Object.prototype` or `null` (so `Object.create(null)` passes).
|
|
40
|
+
* Arrays, Dates, Maps and class instances fail. `true` narrows to `Record<string, unknown>` (refined).
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* affirm(JSON.parse(text), { isPlainObject: true })
|
|
44
|
+
*/
|
|
45
|
+
isPlainObject?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Value is a function (`true`) or is not (`false`).
|
|
48
|
+
*
|
|
49
|
+
* Passes when `(typeof value === 'function') === param`. Classes are functions and pass; calling
|
|
50
|
+
* one without `new` throws. `true` narrows to `(...args: never[]) => unknown`, exact.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* if (affirm(options.onError, { isFunction: true })) { … }
|
|
54
|
+
*/
|
|
55
|
+
isFunction?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Value is an instance of this class.
|
|
58
|
+
*
|
|
59
|
+
* Passes when `value instanceof param`. Narrows to the instance type (refined).
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* if (affirm(error, { isInstanceOf: HttpError })) { error.status }
|
|
63
|
+
*/
|
|
64
|
+
isInstanceOf?: abstract new (...args: any) => any;
|
|
65
|
+
/**
|
|
66
|
+
* Value is a `Date` with a valid time (`true`), or an invalid `Date` (`false`).
|
|
67
|
+
*
|
|
68
|
+
* Passes when the value is a `Date` and `!isNaN(value.getTime()) === param`. Non-Dates (including
|
|
69
|
+
* date strings) fail both polarities. Narrows to `Date` (refined).
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* affirm(new Date(input), { isValidDate: true })
|
|
73
|
+
*/
|
|
74
|
+
isValidDate?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Value is an integer (`true`) or a non-integer number (`false`).
|
|
77
|
+
*
|
|
78
|
+
* Passes when the value is a number and `Number.isInteger(value) === param`. Non-numbers fail
|
|
79
|
+
* both polarities (no coercion: `'3'` fails). Narrows to `number` (refined).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* affirm(quantity, { isInteger: true, isAtLeast: 1 })
|
|
83
|
+
*/
|
|
84
|
+
isInteger?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Value is a finite number (`true`), or `Infinity` / `-Infinity` / `NaN` (`false`).
|
|
87
|
+
*
|
|
88
|
+
* Passes when the value is a number and `Number.isFinite(value) === param`. Non-numbers fail
|
|
89
|
+
* both polarities. Narrows to `number` (refined).
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* affirm(price, { isFiniteNumber: true })
|
|
93
|
+
*/
|
|
94
|
+
isFiniteNumber?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Value is `NaN` (`true`) or a number other than `NaN` (`false`).
|
|
97
|
+
*
|
|
98
|
+
* Passes when the value is a number and `Number.isNaN(value) === param`. Non-numbers fail both
|
|
99
|
+
* polarities (unlike global `isNaN`, `'abc'` is not NaN here). Narrows to `number` (refined).
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* affirm(Number(input), { isNaN: false })
|
|
103
|
+
*/
|
|
104
|
+
isNaN?: boolean;
|
|
105
|
+
}
|
|
106
|
+
interface TagTypes {
|
|
107
|
+
string: string;
|
|
108
|
+
number: number;
|
|
109
|
+
boolean: boolean;
|
|
110
|
+
bigint: bigint;
|
|
111
|
+
symbol: symbol;
|
|
112
|
+
function: AnyFunction;
|
|
113
|
+
object: object;
|
|
114
|
+
}
|
|
115
|
+
type IsTypeInfo<V> = ParamInfo<V, V extends StandardSchemaV1 ? Refine<StandardSchemaV1.InferInput<V>> : V extends TypeGuard<infer T> ? Exact<T> : [V] extends [TypeTag] ? IsUnion<V> extends true ? Refine<TagTypes[V]> : V extends 'object' ? Refine<object> : Exact<TagTypes[V]> : Refine>;
|
|
116
|
+
type InstanceInfo<V> = ParamInfo<V, V extends abstract new (...args: any) => infer I ? Refine<I> : Refine>;
|
|
117
|
+
type Gated<V, Kind> = BoolInfo<V, Refine<Kind>, Refine<Kind>>;
|
|
118
|
+
export type RuntimeTypeInfos<P> = [
|
|
119
|
+
IsTypeInfo<ParamOf<P, 'isType'>>,
|
|
120
|
+
BoolInfo<ParamOf<P, 'isArray'>, Exact<readonly unknown[]>, Refine>,
|
|
121
|
+
BoolInfo<ParamOf<P, 'isPlainObject'>, Refine<Record<string, unknown>>, Refine>,
|
|
122
|
+
BoolInfo<ParamOf<P, 'isFunction'>, Exact<AnyFunction>, Refine>,
|
|
123
|
+
InstanceInfo<ParamOf<P, 'isInstanceOf'>>,
|
|
124
|
+
Gated<ParamOf<P, 'isValidDate'>, Date>,
|
|
125
|
+
Gated<ParamOf<P, 'isInteger'>, number>,
|
|
126
|
+
Gated<ParamOf<P, 'isFiniteNumber'>, number>,
|
|
127
|
+
Gated<ParamOf<P, 'isNaN'>, number>
|
|
128
|
+
];
|
|
129
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Measurable } from '../core/util/kind';
|
|
2
|
+
import type { BoolInfo, ParamInfo, ParamOf, Refine } from './narrowing';
|
|
3
|
+
export interface SizeParams {
|
|
4
|
+
/**
|
|
5
|
+
* Value is empty (`true`) or not empty (`false`).
|
|
6
|
+
*
|
|
7
|
+
* Passes when the value is a string, array, Map, Set or plain object and its size (length, size,
|
|
8
|
+
* or own enumerable key count) is 0 (`true`) / greater than 0 (`false`). Anything else, including
|
|
9
|
+
* `null`, fails both polarities. Does not narrow.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* affirm(cart.items, { isEmpty: false })
|
|
13
|
+
* affirm(input, { anyOf: [{ isNullish: true }, { isEmpty: true }] }) // "null or empty"
|
|
14
|
+
*/
|
|
15
|
+
isEmpty?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Value is not empty (`true`) or empty (`false`). The opposite of `isEmpty`, same kinds.
|
|
18
|
+
*
|
|
19
|
+
* Passes when the size is greater than 0 (`true`) / is 0 (`false`). Non-measurable values fail
|
|
20
|
+
* both polarities. Does not narrow.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* affirm(order.items, { isNotEmpty: true })
|
|
24
|
+
*/
|
|
25
|
+
isNotEmpty?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Value is a blank string (`true`) or a string with non-whitespace (`false`).
|
|
28
|
+
*
|
|
29
|
+
* Passes when the value is a string and `(value.trim() === '') === param`. Non-strings fail both
|
|
30
|
+
* polarities. Narrows to `string` (refined).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* affirm(form.name, { isBlank: false })
|
|
34
|
+
*/
|
|
35
|
+
isBlank?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Value's length (string, array) or size (Map, Set) is exactly n.
|
|
38
|
+
*
|
|
39
|
+
* Passes when the size `=== n`. Plain objects and other values fail. n must be a non-negative
|
|
40
|
+
* integer. Narrows to `string | readonly unknown[] | ReadonlyMap | ReadonlySet` (refined).
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* affirm(otp, { isType: 'string', hasLength: 6 })
|
|
44
|
+
*/
|
|
45
|
+
hasLength?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Value's length or size is at least n.
|
|
48
|
+
*
|
|
49
|
+
* Passes when the size `>= n` (strings, arrays, Maps, Sets). n must be a non-negative integer.
|
|
50
|
+
* Narrows like `hasLength`.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* affirm(password, { hasMinLength: 12 })
|
|
54
|
+
*/
|
|
55
|
+
hasMinLength?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Value's length or size is at most n.
|
|
58
|
+
*
|
|
59
|
+
* Passes when the size `<= n` (strings, arrays, Maps, Sets). n must be a non-negative integer.
|
|
60
|
+
* Narrows like `hasLength`.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* affirm(bio, { hasMaxLength: 280 })
|
|
64
|
+
*/
|
|
65
|
+
hasMaxLength?: number;
|
|
66
|
+
}
|
|
67
|
+
type LengthInfo<V> = ParamInfo<V, Refine<Measurable>>;
|
|
68
|
+
export type SizeInfos<P> = [
|
|
69
|
+
BoolInfo<ParamOf<P, 'isEmpty'>, Refine, Refine>,
|
|
70
|
+
BoolInfo<ParamOf<P, 'isNotEmpty'>, Refine, Refine>,
|
|
71
|
+
BoolInfo<ParamOf<P, 'isBlank'>, Refine<string>, Refine<string>>,
|
|
72
|
+
LengthInfo<ParamOf<P, 'hasLength'>>,
|
|
73
|
+
LengthInfo<ParamOf<P, 'hasMinLength'>>,
|
|
74
|
+
LengthInfo<ParamOf<P, 'hasMaxLength'>>
|
|
75
|
+
];
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ParamInfo, ParamOf, Refine } from './narrowing';
|
|
2
|
+
export interface StringParams {
|
|
3
|
+
/**
|
|
4
|
+
* Value is a string starting with this prefix.
|
|
5
|
+
*
|
|
6
|
+
* Passes when the value is a string and `value.startsWith(param)`. Narrows to `string` (refined).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* affirm(id, { startsWith: 'ord_' })
|
|
10
|
+
*/
|
|
11
|
+
startsWith?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Value is a string ending with this suffix.
|
|
14
|
+
*
|
|
15
|
+
* Passes when the value is a string and `value.endsWith(param)`. Narrows to `string` (refined).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* affirm(filename, { endsWith: '.csv' })
|
|
19
|
+
*/
|
|
20
|
+
endsWith?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Value is a string containing this substring. Strings only: for array items use `contains`.
|
|
23
|
+
*
|
|
24
|
+
* Passes when the value is a string and `value.includes(param)`. Narrows to `string` (refined).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* affirm(email, { includes: '@' })
|
|
28
|
+
*/
|
|
29
|
+
includes?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Value is a string matching this regular expression.
|
|
32
|
+
*
|
|
33
|
+
* Passes when the value is a string and `param.test(value)`. `lastIndex` is reset first, so `/g`
|
|
34
|
+
* and `/y` regexes give the same answer on every call. A string param throws (use `includes`).
|
|
35
|
+
* Narrows to `string` (refined).
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* affirm(zip, { matches: /^\d{5}$/ })
|
|
39
|
+
*/
|
|
40
|
+
matches?: RegExp;
|
|
41
|
+
}
|
|
42
|
+
type StringInfo<V> = ParamInfo<V, Refine<string>>;
|
|
43
|
+
export type StringInfos<P> = [
|
|
44
|
+
StringInfo<ParamOf<P, 'startsWith'>>,
|
|
45
|
+
StringInfo<ParamOf<P, 'endsWith'>>,
|
|
46
|
+
StringInfo<ParamOf<P, 'includes'>>,
|
|
47
|
+
StringInfo<ParamOf<P, 'matches'>>
|
|
48
|
+
];
|
|
49
|
+
export {};
|