@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/index.mjs
CHANGED
|
@@ -1,231 +1,102 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { arrayify, isValidator } from "./constraints.mjs";
|
|
2
|
+
import { custom, describe, meta } from "./metadata.mjs";
|
|
3
|
+
import { assert, isRefinementAssertion, refine, runRefinementAssertion } from "./assert.mjs";
|
|
4
|
+
import { endsWith, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isError, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isValidDate, multipleOf, oneOf, startsWith } from "./assertions.mjs";
|
|
2
5
|
import { discriminatedUnion, each, exact, nullable, nullish, optional, record, shape, tuple, union } from "./combinators.mjs";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
import { ViolationCollection, collection } from "./violations.mjs";
|
|
7
|
+
//#region src/index.ts
|
|
8
|
+
var collectViolations = async (value, constraints, path = []) => {
|
|
9
|
+
const validations = [];
|
|
10
|
+
let establishedAssertionDomain = false;
|
|
11
|
+
for (const c of arrayify(constraints)) {
|
|
12
|
+
if (isValidator(c)) {
|
|
13
|
+
establishedAssertionDomain = false;
|
|
14
|
+
validations.push(...c.run(collectViolations, value, path).map((v) => v instanceof Promise ? v : Promise.resolve(v)));
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (!establishedAssertionDomain && isRefinementAssertion(c)) throw new Error(`Refinement ${String(c.name)} requires a compatible preceding guard`);
|
|
18
|
+
const v = establishedAssertionDomain && isRefinementAssertion(c) ? runRefinementAssertion(c, value) : c(value);
|
|
19
|
+
if (v instanceof Promise) {
|
|
20
|
+
establishedAssertionDomain = false;
|
|
21
|
+
if (c.bail) {
|
|
22
|
+
const awaited = await v;
|
|
23
|
+
if (awaited) {
|
|
24
|
+
validations.push(Promise.resolve([Object.assign(awaited, { path: [...path] })]));
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
} else validations.push(v.then((v) => v ? [Object.assign(v, { path: [...path] })] : []));
|
|
28
|
+
} else if (v) {
|
|
29
|
+
establishedAssertionDomain = false;
|
|
30
|
+
validations.push(Promise.resolve([Object.assign(v, { path: [...path] })]));
|
|
31
|
+
if (c.bail) break;
|
|
32
|
+
} else establishedAssertionDomain = true;
|
|
33
|
+
}
|
|
34
|
+
return settle(value, path, validations);
|
|
14
35
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
at(path) {
|
|
37
|
-
if (!isPathPrefix(path, this.path)) {
|
|
38
|
-
return void 0;
|
|
39
|
-
}
|
|
40
|
-
if (path.length === this.path.length) {
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
const child = this.children.get(path[this.path.length]);
|
|
44
|
-
return child?.at(path);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const buildTree = (violations) => {
|
|
48
|
-
const root = createTreeState(ROOT_PATH);
|
|
49
|
-
violations.forEach((violation) => {
|
|
50
|
-
const path = normalizePath(violation.path);
|
|
51
|
-
root.subtree.push(violation);
|
|
52
|
-
if (path.length === 0) {
|
|
53
|
-
root.self.push(violation);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let current = root;
|
|
57
|
-
const currentPath = [];
|
|
58
|
-
path.forEach((segment, index) => {
|
|
59
|
-
currentPath.push(segment);
|
|
60
|
-
let child = current.children.get(segment);
|
|
61
|
-
if (!child) {
|
|
62
|
-
child = createTreeState(currentPath);
|
|
63
|
-
current.children.set(segment, child);
|
|
64
|
-
}
|
|
65
|
-
child.subtree.push(violation);
|
|
66
|
-
if (index === path.length - 1) {
|
|
67
|
-
child.self.push(violation);
|
|
68
|
-
}
|
|
69
|
-
current = child;
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
return new TreeNode(root);
|
|
73
|
-
};
|
|
74
|
-
class ViolationCollection {
|
|
75
|
-
constructor(violations) {
|
|
76
|
-
this.violations = [...violations];
|
|
77
|
-
this.size = this.violations.length;
|
|
78
|
-
}
|
|
79
|
-
[Symbol.iterator]() {
|
|
80
|
-
return this.violations[Symbol.iterator]();
|
|
81
|
-
}
|
|
82
|
-
forEach(callback) {
|
|
83
|
-
this.violations.forEach((violation, index) => callback(violation, index, this));
|
|
84
|
-
}
|
|
85
|
-
map(callback) {
|
|
86
|
-
return this.violations.map((violation, index) => callback(violation, index, this));
|
|
87
|
-
}
|
|
88
|
-
at(path) {
|
|
89
|
-
return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
|
|
90
|
-
}
|
|
91
|
-
tree() {
|
|
92
|
-
return buildTree(this.violations);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const collection = (violations) => new ViolationCollection(violations);
|
|
96
|
-
const collectViolations = async (value, constraints, path = []) => {
|
|
97
|
-
const validations = [];
|
|
98
|
-
for (const c2 of arrayify(constraints)) {
|
|
99
|
-
if (isValidator(c2)) {
|
|
100
|
-
validations.push(...c2.run(collectViolations, value, path).map((v2) => v2 instanceof Promise ? v2 : Promise.resolve(v2)));
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const v = c2(value);
|
|
104
|
-
if (v instanceof Promise) {
|
|
105
|
-
if (c2.bail) {
|
|
106
|
-
const awaited = await v;
|
|
107
|
-
if (awaited) {
|
|
108
|
-
validations.push(Promise.resolve([Object.assign(awaited, { path: [...path] })]));
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
} else {
|
|
112
|
-
validations.push(v.then((v2) => v2 ? [Object.assign(v2, { path: [...path] })] : []));
|
|
113
|
-
}
|
|
114
|
-
} else if (v) {
|
|
115
|
-
validations.push(Promise.resolve([Object.assign(v, { path: [...path] })]));
|
|
116
|
-
if (c2.bail) {
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return settle(value, path, validations);
|
|
122
|
-
};
|
|
123
|
-
const collectViolationsSync = (value, constraints, path = []) => {
|
|
124
|
-
const violations = [];
|
|
125
|
-
for (const c2 of arrayify(constraints)) {
|
|
126
|
-
if (isValidator(c2)) {
|
|
127
|
-
violations.push(...c2.run(collectViolationsSync, value, path));
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
const v = c2(value);
|
|
131
|
-
if (v instanceof Promise) {
|
|
132
|
-
throw new Error("Found asynchronous validator " + String(c2.name));
|
|
133
|
-
} else if (v) {
|
|
134
|
-
violations.push(Object.assign(v, { path: [...path] }));
|
|
135
|
-
if (c2.bail) break;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return flatten(violations);
|
|
36
|
+
collectViolations.sync = false;
|
|
37
|
+
var collectViolationsSync = (value, constraints, path = []) => {
|
|
38
|
+
const violations = [];
|
|
39
|
+
let establishedAssertionDomain = false;
|
|
40
|
+
for (const c of arrayify(constraints)) {
|
|
41
|
+
if (isValidator(c)) {
|
|
42
|
+
establishedAssertionDomain = false;
|
|
43
|
+
violations.push(...c.run(collectViolationsSync, value, path));
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (!establishedAssertionDomain && isRefinementAssertion(c)) throw new Error(`Refinement ${String(c.name)} requires a compatible preceding guard`);
|
|
47
|
+
const v = establishedAssertionDomain && isRefinementAssertion(c) ? runRefinementAssertion(c, value) : c(value);
|
|
48
|
+
if (v instanceof Promise) throw new Error("Found asynchronous validator " + String(c.name));
|
|
49
|
+
else if (v) {
|
|
50
|
+
establishedAssertionDomain = false;
|
|
51
|
+
violations.push(Object.assign(v, { path: [...path] }));
|
|
52
|
+
if (c.bail) break;
|
|
53
|
+
} else establishedAssertionDomain = true;
|
|
54
|
+
}
|
|
55
|
+
return flatten(violations);
|
|
139
56
|
};
|
|
57
|
+
collectViolationsSync.sync = true;
|
|
140
58
|
function toResult(value, violations) {
|
|
141
|
-
|
|
59
|
+
return violations.length === 0 ? [
|
|
60
|
+
true,
|
|
61
|
+
value,
|
|
62
|
+
[]
|
|
63
|
+
] : [
|
|
64
|
+
false,
|
|
65
|
+
value,
|
|
66
|
+
violations
|
|
67
|
+
];
|
|
142
68
|
}
|
|
143
69
|
function flatten(recursive) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
70
|
+
const flattened = [];
|
|
71
|
+
recursive.forEach((element) => {
|
|
72
|
+
flattened.push(...Array.isArray(element) ? flatten(element) : [element]);
|
|
73
|
+
});
|
|
74
|
+
return flattened;
|
|
149
75
|
}
|
|
150
76
|
async function settle(value, path, validations) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
77
|
+
const violations = [];
|
|
78
|
+
(await Promise.allSettled(validations)).forEach((result) => {
|
|
79
|
+
if (result.status === "fulfilled") violations.push(...result.value);
|
|
80
|
+
else violations.push({
|
|
81
|
+
value,
|
|
82
|
+
path,
|
|
83
|
+
violates: {
|
|
84
|
+
kind: "runtime",
|
|
85
|
+
name: "validate",
|
|
86
|
+
code: "runtime.rejection",
|
|
87
|
+
args: [result.reason]
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
return violations;
|
|
165
92
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
violations
|
|
177
|
-
);
|
|
178
|
-
},
|
|
179
|
-
{
|
|
180
|
-
sync(value, constraints) {
|
|
181
|
-
const violations = collectViolationsSync(value, constraints);
|
|
182
|
-
return toResult(
|
|
183
|
-
value,
|
|
184
|
-
violations
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
);
|
|
189
|
-
export {
|
|
190
|
-
ViolationCollection,
|
|
191
|
-
assert,
|
|
192
|
-
collection,
|
|
193
|
-
c as custom,
|
|
194
|
-
b as describe,
|
|
195
|
-
discriminatedUnion,
|
|
196
|
-
each,
|
|
197
|
-
endsWith,
|
|
198
|
-
exact,
|
|
199
|
-
hasLength,
|
|
200
|
-
hasPattern,
|
|
201
|
-
hasSize,
|
|
202
|
-
hasValue,
|
|
203
|
-
isBigInt,
|
|
204
|
-
isBlob,
|
|
205
|
-
isBoolean,
|
|
206
|
-
isDate,
|
|
207
|
-
isDefined,
|
|
208
|
-
isEmail,
|
|
209
|
-
isFile,
|
|
210
|
-
isFunction,
|
|
211
|
-
isMap,
|
|
212
|
-
isNaN,
|
|
213
|
-
isNull,
|
|
214
|
-
isNumber,
|
|
215
|
-
isSet,
|
|
216
|
-
isString,
|
|
217
|
-
isSymbol,
|
|
218
|
-
matches,
|
|
219
|
-
m as meta,
|
|
220
|
-
multipleOf,
|
|
221
|
-
nullable,
|
|
222
|
-
nullish,
|
|
223
|
-
oneOf,
|
|
224
|
-
optional,
|
|
225
|
-
record,
|
|
226
|
-
shape,
|
|
227
|
-
startsWith,
|
|
228
|
-
tuple,
|
|
229
|
-
union,
|
|
230
|
-
validate
|
|
231
|
-
};
|
|
93
|
+
var matches = { sync(value, constraints) {
|
|
94
|
+
return collectViolationsSync(value, constraints).length === 0;
|
|
95
|
+
} };
|
|
96
|
+
var validate = /* @__PURE__ */ Object.assign(async (value, constraints) => {
|
|
97
|
+
return toResult(value, await collectViolations(value, constraints));
|
|
98
|
+
}, { sync(value, constraints) {
|
|
99
|
+
return toResult(value, collectViolationsSync(value, constraints));
|
|
100
|
+
} });
|
|
101
|
+
//#endregion
|
|
102
|
+
export { ViolationCollection, assert, collection, custom, describe, discriminatedUnion, each, endsWith, exact, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isError, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isValidDate, matches, meta, multipleOf, nullable, nullish, oneOf, optional, record, refine, shape, startsWith, tuple, union, validate };
|