@modulify/validator 0.2.1 → 0.3.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/CHANGELOG.md +29 -1
- package/README.md +12 -2
- package/dist/assert.cjs +99 -63
- package/dist/assert.d.cts +37 -0
- package/dist/assert.d.mts +37 -0
- package/dist/assert.d.ts +24 -3
- package/dist/assert.mjs +94 -64
- package/dist/assertions.cjs +283 -178
- package/dist/assertions.d.cts +56 -0
- package/dist/assertions.d.mts +56 -0
- package/dist/assertions.d.ts +43 -45
- package/dist/assertions.mjs +274 -201
- package/dist/checkers.cjs +23 -0
- package/dist/checkers.d.cts +8 -0
- package/dist/checkers.d.mts +8 -0
- package/dist/checkers.d.ts +1 -1
- package/dist/checkers.mjs +16 -0
- package/dist/combinators.cjs +305 -304
- package/dist/combinators.d.cts +16 -0
- package/dist/combinators.d.mts +16 -0
- package/dist/combinators.d.ts +15 -16
- package/dist/combinators.mjs +305 -314
- package/dist/constraints.cjs +23 -0
- package/dist/constraints.d.cts +4 -0
- package/dist/constraints.d.mts +4 -0
- package/dist/constraints.d.ts +2 -2
- package/dist/constraints.mjs +21 -0
- package/dist/extractors.cjs +6 -0
- package/dist/extractors.d.cts +2 -0
- package/dist/extractors.d.mts +2 -0
- package/dist/extractors.mjs +5 -0
- package/dist/index.cjs +140 -219
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.mjs +93 -222
- package/dist/json-schema.cjs +364 -489
- package/dist/json-schema.d.cts +14 -0
- package/dist/json-schema.d.mts +14 -0
- package/dist/json-schema.d.ts +3 -3
- package/dist/json-schema.mjs +365 -492
- package/dist/metadata.cjs +98 -7
- package/dist/metadata.d.cts +8 -0
- package/dist/metadata.d.mts +8 -0
- package/dist/metadata.d.ts +2 -2
- package/dist/metadata.mjs +93 -7
- package/dist/predicates.cjs +98 -41
- package/dist/predicates.d.cts +77 -0
- package/dist/predicates.d.mts +77 -0
- package/dist/predicates.d.ts +25 -4
- package/dist/predicates.mjs +92 -66
- package/dist/types/index.d.cts +984 -0
- package/dist/types/index.d.mts +984 -0
- package/dist/types/index.d.ts +984 -0
- package/dist/types/json-schema.d.cts +75 -0
- package/dist/types/json-schema.d.mts +75 -0
- package/dist/types/json-schema.d.ts +75 -0
- package/dist/violations.cjs +81 -0
- package/dist/violations.d.cts +29 -0
- package/dist/violations.d.mts +29 -0
- package/dist/violations.d.ts +1 -1
- package/dist/violations.mjs +80 -0
- package/docs/RELEASING.md +66 -0
- package/docs/en/00-index.md +2 -0
- package/docs/en/01-shape-api.md +35 -10
- package/docs/en/02-metadata-and-introspection.md +2 -1
- package/docs/en/03-violations.md +1 -1
- package/docs/en/04-json-schema-export.md +5 -0
- package/docs/en/05-public-api.md +35 -3
- package/docs/en/06-common-recipes.md +2 -1
- package/docs/en/07-ai-reference.md +5 -2
- package/docs/en/08-violation-code-types.md +28 -2
- package/docs/en/09-migration.md +75 -0
- package/docs/ru/00-index.md +2 -0
- package/docs/ru/01-shape-api.md +35 -10
- package/docs/ru/02-metadata-and-introspection.md +2 -1
- package/docs/ru/03-violations.md +1 -1
- package/docs/ru/04-json-schema-export.md +5 -0
- package/docs/ru/05-public-api.md +35 -3
- package/docs/ru/06-common-recipes.md +2 -1
- package/docs/ru/07-ai-reference.md +5 -2
- package/docs/ru/08-violation-code-types.md +28 -2
- package/docs/ru/09-migration.md +76 -0
- package/docs/ru/README.md +10 -2
- package/package.json +63 -37
- package/types/index.d.ts +275 -115
- package/dist/metadata.cjs.js +0 -130
- package/dist/metadata.es.js +0 -131
package/dist/predicates.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Intersect, Predicate } from '
|
|
1
|
+
import { Intersect, Predicate } from './types/index.js';
|
|
2
2
|
/** Checks if a value has a property */
|
|
3
3
|
export declare function hasProperty<K extends PropertyKey = PropertyKey>(key: K): (value: unknown) => value is { [k in K]: unknown; };
|
|
4
4
|
/** Checks if a value is an array */
|
|
@@ -11,12 +11,16 @@ export declare function isBigInt(value: unknown): value is bigint;
|
|
|
11
11
|
export declare function isBlob(value: unknown): value is Blob;
|
|
12
12
|
/** Checks if value is Date */
|
|
13
13
|
export declare function isDate(value: unknown): value is Date;
|
|
14
|
+
/** Checks if value is a valid Date */
|
|
15
|
+
export declare function isValidDate(value: unknown): value is Date;
|
|
14
16
|
/** Checks if a value is an email */
|
|
15
17
|
export declare function isEmail(value: unknown): value is string;
|
|
16
18
|
/** Creates a predicate that checks if a value is equal to specified */
|
|
17
19
|
export declare function isExact<T = unknown>(exact: T): (value: unknown) => value is T;
|
|
18
20
|
/** Checks if a value is a File */
|
|
19
21
|
export declare function isFile(value: unknown): value is File;
|
|
22
|
+
/** Checks if a value is an Error */
|
|
23
|
+
export declare function isError(value: unknown): value is Error;
|
|
20
24
|
/** Checks if a value is a function */
|
|
21
25
|
export declare function isFunction<T extends (...args: never[]) => unknown = (...args: never[]) => unknown>(value: unknown): value is T;
|
|
22
26
|
/** Checks if a value is a Map */
|
|
@@ -27,10 +31,20 @@ export declare function isNaN(value: unknown): value is number;
|
|
|
27
31
|
export declare function isNull(value: unknown): value is null;
|
|
28
32
|
/** Checks if a value is a number */
|
|
29
33
|
export declare function isNumber(value: unknown): value is number;
|
|
34
|
+
/** Checks if a value is a finite number */
|
|
35
|
+
export declare function isFiniteNumber(value: unknown): value is number;
|
|
36
|
+
/** Checks if a value is an integer */
|
|
37
|
+
export declare function isInteger(value: unknown): value is number;
|
|
30
38
|
/** Checks if a value is an object, excluding null value */
|
|
31
39
|
export declare function isObject(value: unknown): value is object;
|
|
40
|
+
/** Checks if a value is promise-like */
|
|
41
|
+
export declare function isPromiseLike<T = unknown>(value: unknown): value is PromiseLike<T>;
|
|
42
|
+
/** Checks if a value is a regular expression */
|
|
43
|
+
export declare function isRegExp(value: unknown): value is RegExp;
|
|
32
44
|
/** Check if a value is a record like Record<PropertyKey, unknown> */
|
|
33
45
|
export declare function isRecord(value: unknown): value is Record<PropertyKey, unknown>;
|
|
46
|
+
/** Checks if a value is a safe integer */
|
|
47
|
+
export declare function isSafeInteger(value: unknown): value is number;
|
|
34
48
|
/** Checks if a value is a Set */
|
|
35
49
|
export declare function isSet<T = unknown>(value: unknown): value is Set<T>;
|
|
36
50
|
/** Checks if a value is a string */
|
|
@@ -49,8 +63,15 @@ export declare function Not<T>(predicate: Predicate<T>): Predicate;
|
|
|
49
63
|
export type Shape<T extends object> = {
|
|
50
64
|
[K in keyof T]: [Predicate<T[K]>, boolean] | Predicate<T[K]>;
|
|
51
65
|
};
|
|
52
|
-
type
|
|
53
|
-
|
|
54
|
-
|
|
66
|
+
type ShapePropertyType<Config> = Config extends Predicate<infer U> ? U : Config extends [Predicate<infer U>, boolean] ? U : never;
|
|
67
|
+
type OptionalShapeKeys<T> = {
|
|
68
|
+
[K in keyof T]: T[K] extends [Predicate, infer Required] ? false extends Required ? K : never : never;
|
|
69
|
+
}[keyof T];
|
|
70
|
+
type ExtractType<T> = {
|
|
71
|
+
[K in Exclude<keyof T, OptionalShapeKeys<T>>]: ShapePropertyType<T[K]>;
|
|
72
|
+
} & {
|
|
73
|
+
[K in OptionalShapeKeys<T>]?: ShapePropertyType<T[K]>;
|
|
74
|
+
};
|
|
75
|
+
/** Checks present fields with their predicates; optional fields may be absent. */
|
|
55
76
|
export declare const isShape: <S extends Shape<any>>(shape: S) => (value: unknown) => value is ExtractType<S>;
|
|
56
77
|
export {};
|
package/dist/predicates.mjs
CHANGED
|
@@ -1,116 +1,142 @@
|
|
|
1
|
+
//#region src/predicates.ts
|
|
2
|
+
/** Checks if a value has a property */
|
|
1
3
|
function hasProperty(key) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
return (value) => {
|
|
5
|
+
return isObject(value) && !isNull(value) && Object.prototype.hasOwnProperty.call(value, key);
|
|
6
|
+
};
|
|
5
7
|
}
|
|
8
|
+
/** Checks if a value is an array */
|
|
6
9
|
function isArray(value) {
|
|
7
|
-
|
|
10
|
+
return Array.isArray(value);
|
|
8
11
|
}
|
|
12
|
+
/** Checks if a value is a boolean */
|
|
9
13
|
function isBoolean(value) {
|
|
10
|
-
|
|
14
|
+
return typeof value === "boolean";
|
|
11
15
|
}
|
|
16
|
+
/** Checks if a value is a bigint */
|
|
12
17
|
function isBigInt(value) {
|
|
13
|
-
|
|
18
|
+
return typeof value === "bigint";
|
|
14
19
|
}
|
|
20
|
+
/** Checks if a value is a Blob */
|
|
15
21
|
function isBlob(value) {
|
|
16
|
-
|
|
22
|
+
return typeof Blob !== "undefined" && value instanceof Blob;
|
|
17
23
|
}
|
|
24
|
+
/** Checks if value is Date */
|
|
18
25
|
function isDate(value) {
|
|
19
|
-
|
|
26
|
+
return value instanceof Date;
|
|
20
27
|
}
|
|
28
|
+
/** Checks if value is a valid Date */
|
|
29
|
+
function isValidDate(value) {
|
|
30
|
+
return isDate(value) && !Number.isNaN(value.getTime());
|
|
31
|
+
}
|
|
32
|
+
/** Checks if a value is an email */
|
|
21
33
|
function isEmail(value) {
|
|
22
|
-
|
|
23
|
-
return isString(value) && pattern.test(value);
|
|
34
|
+
return isString(value) && /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(value);
|
|
24
35
|
}
|
|
36
|
+
/** Creates a predicate that checks if a value is equal to specified */
|
|
25
37
|
function isExact(exact) {
|
|
26
|
-
|
|
38
|
+
return (value) => value === exact;
|
|
27
39
|
}
|
|
40
|
+
/** Checks if a value is a File */
|
|
28
41
|
function isFile(value) {
|
|
29
|
-
|
|
42
|
+
return typeof File !== "undefined" && value instanceof File;
|
|
43
|
+
}
|
|
44
|
+
/** Checks if a value is an Error */
|
|
45
|
+
function isError(value) {
|
|
46
|
+
return value instanceof Error;
|
|
30
47
|
}
|
|
48
|
+
/** Checks if a value is a function */
|
|
31
49
|
function isFunction(value) {
|
|
32
|
-
|
|
50
|
+
return typeof value === "function";
|
|
33
51
|
}
|
|
52
|
+
/** Checks if a value is a Map */
|
|
34
53
|
function isMap(value) {
|
|
35
|
-
|
|
54
|
+
return value instanceof Map;
|
|
36
55
|
}
|
|
56
|
+
/** Checks if a value is NaN */
|
|
37
57
|
function isNaN(value) {
|
|
38
|
-
|
|
58
|
+
return typeof value === "number" && Number.isNaN(value);
|
|
39
59
|
}
|
|
60
|
+
/** Checks if a value is null */
|
|
40
61
|
function isNull(value) {
|
|
41
|
-
|
|
62
|
+
return value === null;
|
|
42
63
|
}
|
|
64
|
+
/** Checks if a value is a number */
|
|
43
65
|
function isNumber(value) {
|
|
44
|
-
|
|
66
|
+
return typeof value === "number" && !isNaN(value);
|
|
67
|
+
}
|
|
68
|
+
/** Checks if a value is a finite number */
|
|
69
|
+
function isFiniteNumber(value) {
|
|
70
|
+
return isNumber(value) && Number.isFinite(value);
|
|
71
|
+
}
|
|
72
|
+
/** Checks if a value is an integer */
|
|
73
|
+
function isInteger(value) {
|
|
74
|
+
return isNumber(value) && Number.isInteger(value);
|
|
45
75
|
}
|
|
76
|
+
/** Checks if a value is an object, excluding null value */
|
|
46
77
|
function isObject(value) {
|
|
47
|
-
|
|
78
|
+
return typeof value === "object" && value !== null;
|
|
48
79
|
}
|
|
80
|
+
/** Checks if a value is promise-like */
|
|
81
|
+
function isPromiseLike(value) {
|
|
82
|
+
return (isObject(value) || isFunction(value)) && "then" in value && isFunction(value.then);
|
|
83
|
+
}
|
|
84
|
+
/** Checks if a value is a regular expression */
|
|
85
|
+
function isRegExp(value) {
|
|
86
|
+
return value instanceof RegExp;
|
|
87
|
+
}
|
|
88
|
+
/** Check if a value is a record like Record<PropertyKey, unknown> */
|
|
49
89
|
function isRecord(value) {
|
|
50
|
-
|
|
90
|
+
return isObject(value) && !isNull(value) && constructorOf(value) === Object && Object.keys(prototypeOf(value)).length === 0;
|
|
51
91
|
}
|
|
92
|
+
/** Checks if a value is a safe integer */
|
|
93
|
+
function isSafeInteger(value) {
|
|
94
|
+
return isNumber(value) && Number.isSafeInteger(value);
|
|
95
|
+
}
|
|
96
|
+
/** Checks if a value is a Set */
|
|
52
97
|
function isSet(value) {
|
|
53
|
-
|
|
98
|
+
return value instanceof Set;
|
|
54
99
|
}
|
|
100
|
+
/** Checks if a value is a string */
|
|
55
101
|
function isString(value) {
|
|
56
|
-
|
|
102
|
+
return typeof value === "string";
|
|
57
103
|
}
|
|
104
|
+
/** Checks if a value is a symbol */
|
|
58
105
|
function isSymbol(value) {
|
|
59
|
-
|
|
106
|
+
return typeof value === "symbol";
|
|
60
107
|
}
|
|
108
|
+
/** Checks if a value is undefined */
|
|
61
109
|
function isUndefined(value) {
|
|
62
|
-
|
|
110
|
+
return typeof value === "undefined";
|
|
63
111
|
}
|
|
64
112
|
function constructorOf(value) {
|
|
65
|
-
|
|
113
|
+
return prototypeOf(value).constructor;
|
|
66
114
|
}
|
|
67
115
|
function prototypeOf(value) {
|
|
68
|
-
|
|
116
|
+
return Object.getPrototypeOf(value);
|
|
69
117
|
}
|
|
70
118
|
function And(...predicates) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
119
|
+
return (value) => {
|
|
120
|
+
return predicates.every((predicate) => predicate(value));
|
|
121
|
+
};
|
|
74
122
|
}
|
|
75
123
|
function Or(...predicates) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
124
|
+
return (value) => {
|
|
125
|
+
return predicates.some((predicate) => predicate(value));
|
|
126
|
+
};
|
|
79
127
|
}
|
|
80
128
|
function Not(predicate) {
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
And,
|
|
93
|
-
Not,
|
|
94
|
-
Or,
|
|
95
|
-
hasProperty,
|
|
96
|
-
isArray,
|
|
97
|
-
isBigInt,
|
|
98
|
-
isBlob,
|
|
99
|
-
isBoolean,
|
|
100
|
-
isDate,
|
|
101
|
-
isEmail,
|
|
102
|
-
isExact,
|
|
103
|
-
isFile,
|
|
104
|
-
isFunction,
|
|
105
|
-
isMap,
|
|
106
|
-
isNaN,
|
|
107
|
-
isNull,
|
|
108
|
-
isNumber,
|
|
109
|
-
isObject,
|
|
110
|
-
isRecord,
|
|
111
|
-
isSet,
|
|
112
|
-
isShape,
|
|
113
|
-
isString,
|
|
114
|
-
isSymbol,
|
|
115
|
-
isUndefined
|
|
129
|
+
return (value) => !predicate(value);
|
|
130
|
+
}
|
|
131
|
+
/** Checks present fields with their predicates; optional fields may be absent. */
|
|
132
|
+
var isShape = (shape) => {
|
|
133
|
+
const properties = Object.keys(shape);
|
|
134
|
+
return (value) => isObject(value) && properties.every((p) => {
|
|
135
|
+
const config = shape[p];
|
|
136
|
+
const [predicate, required] = isArray(config) ? config : [config, true];
|
|
137
|
+
if (!(p in value)) return !required;
|
|
138
|
+
return predicate(value[p]);
|
|
139
|
+
});
|
|
116
140
|
};
|
|
141
|
+
//#endregion
|
|
142
|
+
export { And, Not, Or, hasProperty, isArray, isBigInt, isBlob, isBoolean, isDate, isEmail, isError, isExact, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isObject, isPromiseLike, isRecord, isRegExp, isSafeInteger, isSet, isShape, isString, isSymbol, isUndefined, isValidDate };
|