@frankmeza/affirm 0.0.0-stage → 0.0.2
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 +154 -0
- package/LICENSE +21 -0
- package/README.md +152 -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 +842 -0
- package/package.json +75 -4
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AffirmParams } from '../types/params.js';
|
|
2
|
+
import type { CheckDef, CheckGroup } from './check-def.js';
|
|
3
|
+
type AnyGroup = Readonly<Record<string, CheckDef<any>>>;
|
|
4
|
+
type UnionToIntersection<U> = (U extends unknown ? (u: U) => void : never) extends (i: infer I) => void ? I : never;
|
|
5
|
+
/**
|
|
6
|
+
* Merges group objects into one registry. Throws at module load on a duplicate key,
|
|
7
|
+
* which a plain spread would silently overwrite.
|
|
8
|
+
*/
|
|
9
|
+
export declare function defineRegistry<const G extends readonly AnyGroup[]>(...groups: G): UnionToIntersection<G[number]>;
|
|
10
|
+
/** Every check, keyed by params key. Group order here is the tie-breaker within a phase. */
|
|
11
|
+
export declare const checks: CheckGroup<AffirmParams>;
|
|
12
|
+
export type CheckKey = keyof AffirmParams;
|
|
13
|
+
/** All keys, sorted by phase (stable, so registry order breaks ties). Design §4 rule 7. */
|
|
14
|
+
export declare const KEY_ORDER: readonly CheckKey[];
|
|
15
|
+
export declare function isCheckKey(key: string): key is CheckKey;
|
|
16
|
+
/** Canonical rank of a key; only call with keys that passed `isCheckKey`. */
|
|
17
|
+
export declare function rankOf(key: CheckKey): number;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** One failed check. Never contains raw values from the checked data (design §8, D5). */
|
|
2
|
+
export interface AffirmFailure {
|
|
3
|
+
/** The params key that failed, e.g. `'hasMinLength'`. */
|
|
4
|
+
key: string;
|
|
5
|
+
/** `''` at the root; `'user.address.zip'`; `'items[2].id'`. */
|
|
6
|
+
path: string;
|
|
7
|
+
/** What the check wanted, e.g. `'length >= 3'`, `'not null'`. */
|
|
8
|
+
expected: string;
|
|
9
|
+
/** A type description of the value, e.g. `'string(len 2)'`. */
|
|
10
|
+
received: string;
|
|
11
|
+
/** One human sentence combining path, expected and received. */
|
|
12
|
+
reason: string;
|
|
13
|
+
/** For `anyOf`: the failures of each branch. */
|
|
14
|
+
branches?: AffirmFailure[][];
|
|
15
|
+
}
|
|
16
|
+
export interface AffirmReport {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
failures: AffirmFailure[];
|
|
19
|
+
}
|
|
20
|
+
export declare function makeFailure(key: string, path: string, expected: string, value: unknown): AffirmFailure;
|
|
21
|
+
/** `joinPath('items', 2)` → `'items[2]'`; `joinPath('user', 'zip')` → `'user.zip'`. */
|
|
22
|
+
export declare function joinPath(base: string, segment: string | number): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural equality per design §7.
|
|
3
|
+
* - primitives: `Object.is`, except `+0` equals `-0`
|
|
4
|
+
* - arrays: same length, elementwise
|
|
5
|
+
* - plain objects: same own enumerable string keys, valuewise
|
|
6
|
+
* - Date: same time · RegExp: same source + flags
|
|
7
|
+
* - Map: same size, same keys (SameValueZero), values deep-equal
|
|
8
|
+
* - Set: same size, every member present (SameValueZero; no deep matching of members)
|
|
9
|
+
* - other class instances: reference equality only
|
|
10
|
+
* - cycles: a pair already under comparison counts as equal
|
|
11
|
+
*/
|
|
12
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Prototype is `Object.prototype` or `null` (so `Object.create(null)` counts, design Q2). */
|
|
2
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
3
|
+
export declare function isNumber(value: unknown): value is number;
|
|
4
|
+
export declare function isDate(value: unknown): value is Date;
|
|
5
|
+
/**
|
|
6
|
+
* Heuristic for "this is a class/constructor, not a guard function" so `isType: Date`
|
|
7
|
+
* can be rejected with "use isInstanceOf". Catches `class` syntax and capitalised built-ins
|
|
8
|
+
* (`Date`, `Map`, `Error`, …). Arrow functions and lowercase function declarations pass.
|
|
9
|
+
*/
|
|
10
|
+
export declare function looksLikeConstructor(fn: Function): boolean;
|
|
11
|
+
/** A number other than NaN: the kind gate for every number key (design §5 "Numbers"). */
|
|
12
|
+
export declare function isNumberNotNaN(value: unknown): value is number;
|
|
13
|
+
export declare function isString(value: unknown): value is string;
|
|
14
|
+
/** Non-null object or array (functions excluded): the kind gate for object keys. */
|
|
15
|
+
export declare function isObjectLike(value: unknown): value is object;
|
|
16
|
+
export declare function isArray(value: unknown): value is readonly unknown[];
|
|
17
|
+
export type Measurable = string | readonly unknown[] | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>;
|
|
18
|
+
/** "Measurable" = string, array, Map, Set (design §5 "Emptiness & size"). */
|
|
19
|
+
export declare function isMeasurable(value: unknown): value is Measurable;
|
|
20
|
+
export declare function sizeOf(value: Measurable): number;
|
|
21
|
+
/** Own enumerable keys, string and symbol. */
|
|
22
|
+
export declare function ownKeys(value: object): PropertyKey[];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** The Standard Schema interface. */
|
|
2
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
+
/** The Standard Schema properties. */
|
|
4
|
+
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
5
|
+
}
|
|
6
|
+
export declare namespace StandardSchemaV1 {
|
|
7
|
+
/** The Standard Schema properties interface. */
|
|
8
|
+
interface Props<Input = unknown, Output = Input> {
|
|
9
|
+
/** The version number of the standard. */
|
|
10
|
+
readonly version: 1;
|
|
11
|
+
/** The vendor name of the schema library. */
|
|
12
|
+
readonly vendor: string;
|
|
13
|
+
/** Validates unknown input values. */
|
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
+
/** Inferred types associated with the schema. */
|
|
16
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
17
|
+
}
|
|
18
|
+
/** The result interface of the validate function. */
|
|
19
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
20
|
+
/** The result interface if validation succeeds. */
|
|
21
|
+
interface SuccessResult<Output> {
|
|
22
|
+
/** The typed output value. */
|
|
23
|
+
readonly value: Output;
|
|
24
|
+
/** The non-existent issues. */
|
|
25
|
+
readonly issues?: undefined;
|
|
26
|
+
}
|
|
27
|
+
/** The result interface if validation fails. */
|
|
28
|
+
interface FailureResult {
|
|
29
|
+
/** The issues of failed validation. */
|
|
30
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
31
|
+
}
|
|
32
|
+
/** The issue interface of the failure output. */
|
|
33
|
+
interface Issue {
|
|
34
|
+
/** The error message of the issue. */
|
|
35
|
+
readonly message: string;
|
|
36
|
+
/** The path of the issue, if any. */
|
|
37
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** The path segment interface of the issue. */
|
|
40
|
+
interface PathSegment {
|
|
41
|
+
/** The key representing a path segment. */
|
|
42
|
+
readonly key: PropertyKey;
|
|
43
|
+
}
|
|
44
|
+
/** The Standard Schema types interface. */
|
|
45
|
+
interface Types<Input = unknown, Output = Input> {
|
|
46
|
+
/** The input type of the schema. */
|
|
47
|
+
readonly input: Input;
|
|
48
|
+
/** The output type of the schema. */
|
|
49
|
+
readonly output: Output;
|
|
50
|
+
}
|
|
51
|
+
/** Infers the input type of a Standard Schema. */
|
|
52
|
+
type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
|
|
53
|
+
/** Infers the output type of a Standard Schema. */
|
|
54
|
+
type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Detects a Standard Schema via its `'~standard'` property. Schemas may be objects (Zod, Valibot)
|
|
58
|
+
* or callable functions (ArkType), so both are accepted.
|
|
59
|
+
*/
|
|
60
|
+
export declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
|
|
61
|
+
/** Runs a schema synchronously. Throws `TypeError` for async schemas (design §4 rule 6, §10). */
|
|
62
|
+
export declare function validateSync(schema: StandardSchemaV1, value: unknown): StandardSchemaV1.Result<unknown>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AffirmParams } from '../types/params.js';
|
|
2
|
+
/**
|
|
3
|
+
* Throws `TypeError` for programmer errors (design §4 rule 6, D4):
|
|
4
|
+
* params that are not an object, empty params `{}`, unknown keys, and wrong param types.
|
|
5
|
+
* Runs over all keys before any evaluation, so a typo can't hide behind a short-circuit.
|
|
6
|
+
* `path` locates nested params (from `shape`, `everyItem`, …) in messages.
|
|
7
|
+
*/
|
|
8
|
+
export declare function validateParams(params: unknown, path?: string): asserts params is AffirmParams;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const CHECK_KEYS: ReadonlySet<string>;
|
|
2
|
+
/**
|
|
3
|
+
* Check names that are ordinary words in other objects (`{ shape: 'circle' }`, a manifest's `matches`,
|
|
4
|
+
* JSON Schema's `anyOf` / `not` / `contains`). An object made only of these is not taken for params
|
|
5
|
+
* unless its nested params are distinctive.
|
|
6
|
+
*/
|
|
7
|
+
export declare const GENERIC_KEYS: ReadonlySet<string>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Linter } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* ESLint plugin for projects that use `affirm` (flat config, ESLint 9+).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* // eslint.config.js
|
|
7
|
+
* import affirm from '@frankmeza/affirm/eslint'
|
|
8
|
+
* export default [affirm.configs.recommended]
|
|
9
|
+
*
|
|
10
|
+
* // Optional: catch negated guards imported from other files, and/or a local re-export of affirm.
|
|
11
|
+
* export default [
|
|
12
|
+
* affirm.configs.recommended,
|
|
13
|
+
* { settings: { affirm: { guards: '^isValid', sources: ['@frankmeza/affirm', '@/lib/affirm'] } } },
|
|
14
|
+
* ]
|
|
15
|
+
*/
|
|
16
|
+
declare const plugin: {
|
|
17
|
+
meta: {
|
|
18
|
+
name: string;
|
|
19
|
+
};
|
|
20
|
+
rules: {
|
|
21
|
+
'no-negated-affirm': import("eslint").Rule.RuleModule;
|
|
22
|
+
'guard-naming': import("eslint").Rule.RuleModule;
|
|
23
|
+
'const-params': import("eslint").Rule.RuleModule;
|
|
24
|
+
};
|
|
25
|
+
configs: {
|
|
26
|
+
recommended: Linter.Config;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export default plugin;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Rule, Scope, SourceCode } from 'eslint';
|
|
2
|
+
export type Node = Rule.Node;
|
|
3
|
+
export declare const ADVICE: string;
|
|
4
|
+
/** The outermost transparent wrapper around `node`. */
|
|
5
|
+
export declare function outermost(node: Node): Node;
|
|
6
|
+
/**
|
|
7
|
+
* The whole expression carrying a value's result through `&&` / `||` (and transparent wrappers):
|
|
8
|
+
* for `affirm(a) && ok` that is the `&&` expression. Negating it negates the affirm() inside.
|
|
9
|
+
*/
|
|
10
|
+
export declare function carrier(node: Node): Node;
|
|
11
|
+
/** `!x`, `x === false`, `x == false`, `x !== true`, `x != true` (either side): the node that negates `expr`, if any. */
|
|
12
|
+
export declare function negationOf(expr: Node): Node | undefined;
|
|
13
|
+
/** One assignment at most (its initializer), so every read sees the same value. */
|
|
14
|
+
export declare function assignedOnce(variable: Scope.Variable): boolean;
|
|
15
|
+
/** The variable a function is bound to: `function f() {}`, `const f = () => …`, `const f = function () {}`. */
|
|
16
|
+
export declare function functionVariable(fn: Node, sourceCode: SourceCode): Scope.Variable | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The function whose entire body is `value`: `(v) => value`, or a body that is only `return value`.
|
|
19
|
+
* Calling such a function is the same as evaluating `value`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function wrapperOf(value: Node): Node | undefined;
|
|
22
|
+
export declare function findVariable(scope: Scope.Scope | null, name: string): Scope.Variable | undefined;
|
|
23
|
+
/** `affirm(…)`, `check(…)` (aliased import) or `ns.affirm(…)` / `ns['affirm'](…)` (namespace import). */
|
|
24
|
+
export declare function isAffirmCall(node: Node, sourceCode: SourceCode, sources: ReadonlySet<string>): boolean;
|
|
25
|
+
/** Any call that takes affirm params second: `affirm(v, p)`, `affirm.assert(v, p, msg)`, `affirm.explain(v, p)`. */
|
|
26
|
+
export declare function takesAffirmParams(node: Node, sourceCode: SourceCode, sources: ReadonlySet<string>): boolean;
|
|
27
|
+
export declare function truncate(text: string): string;
|
|
28
|
+
/** The offending code, on one line, shortened for the message. */
|
|
29
|
+
export declare function usageText(sourceCode: SourceCode, node: Node): string;
|
|
30
|
+
/**
|
|
31
|
+
* Shared settings for every affirm rule, in the flat config's `settings.affirm`:
|
|
32
|
+
*
|
|
33
|
+
* - `sources`: module specifiers that export `affirm` (default `['@frankmeza/affirm']`). Add your own re-export.
|
|
34
|
+
* - `guards`: a regular expression (as a string) that every function returning `affirm(…)` must match,
|
|
35
|
+
* and that only such functions may match. With it, negated calls to imported guards are caught too.
|
|
36
|
+
* - `guardModules`: import prefixes that are your own code, for path aliases such as `'@/'`.
|
|
37
|
+
* Guards are recognised when defined in the file, imported from a relative path, or imported from
|
|
38
|
+
* one of these prefixes; anything imported from a package (a third-party `isValid…`) is not a guard.
|
|
39
|
+
*/
|
|
40
|
+
export interface AffirmSettings {
|
|
41
|
+
sources?: string[];
|
|
42
|
+
guards?: string;
|
|
43
|
+
guardModules?: string[];
|
|
44
|
+
}
|
|
45
|
+
/** `sources` from the rule's own option (if given), else `settings.affirm.sources`, else `['@frankmeza/affirm']`. */
|
|
46
|
+
export declare function sourcesOf(context: Rule.RuleContext, option?: string[]): ReadonlySet<string>;
|
|
47
|
+
/** `settings.affirm.guards` compiled, or `undefined` when no guard pattern is configured. */
|
|
48
|
+
export declare function guardPatternOf(context: Rule.RuleContext): RegExp | undefined;
|
|
49
|
+
/** `guardModules` as a list of prefixes (default none: only relative imports count as project code). */
|
|
50
|
+
export declare function guardModulesOf(context: Rule.RuleContext): readonly string[];
|
|
51
|
+
/** The module an imported variable comes from, or `undefined` when it isn't an import. */
|
|
52
|
+
export declare function importSourceOf(variable: Scope.Variable | undefined): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Whether a call that matches the guard pattern can be one of the project's guards: defined in this
|
|
55
|
+
* file, imported from a relative path or a `guardModules` prefix, or a method on a local value.
|
|
56
|
+
* Calls into packages (`import validator from 'validator'; validator.isValidX()`) are not.
|
|
57
|
+
*/
|
|
58
|
+
export declare function isProjectCall(call: Node, sourceCode: SourceCode, guardModules: readonly string[]): boolean;
|
|
59
|
+
/** The name a function is bound to, and the node to report on: declarations, variables, object and class members. */
|
|
60
|
+
export declare function functionName(fn: Node): {
|
|
61
|
+
name: string;
|
|
62
|
+
node: Node;
|
|
63
|
+
} | undefined;
|
|
64
|
+
/** The name a call goes through: `isValidOrder(x)` or `guards.isValidOrder(x)`. */
|
|
65
|
+
export declare function calleeName(call: Node): string | undefined;
|