@modulify/validator 0.2.1 → 0.3.0
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 +27 -1
- package/README.md +12 -2
- package/dist/assert-BDOtHdLx.cjs +289 -0
- package/dist/assert-CzMg5aO7.mjs +206 -0
- package/dist/assert.cjs +7 -65
- 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 +2 -66
- package/dist/assertions-DPYCHREw.mjs +297 -0
- package/dist/assertions-nfOKqcjs.cjs +476 -0
- package/dist/assertions.cjs +34 -206
- 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 +3 -207
- package/dist/checkers.d.cts +8 -0
- package/dist/checkers.d.mts +8 -0
- package/dist/checkers.d.ts +1 -1
- package/dist/combinators.cjs +303 -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 +304 -315
- package/dist/constraints.d.cts +4 -0
- package/dist/constraints.d.mts +4 -0
- package/dist/constraints.d.ts +2 -2
- package/dist/extractors.d.cts +2 -0
- package/dist/extractors.d.mts +2 -0
- package/dist/index.cjs +210 -213
- 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 +165 -218
- 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 +6 -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 +2 -8
- 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.d.cts +29 -0
- package/dist/violations.d.mts +29 -0
- package/dist/violations.d.ts +1 -1
- 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,178 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as refine, c as custom, d as meta, f as arrayify, i as isRefinementAssertion, l as describe, o as runRefinementAssertion, p as isValidator, t as assert } from "./assert-CzMg5aO7.mjs";
|
|
2
|
+
import { A as startsWith, C as isSafeInteger, D as isValidDate, E as isSymbol, O as multipleOf, S as isRegExp, T as isString, _ as isMap, a as hasValue, b as isNumber, c as isBoolean, d as isEmail, f as isError, g as isInteger, h as isFunction, i as hasSize, k as oneOf, l as isDate, m as isFiniteNumber, n as hasLength, o as isBigInt, p as isFile, r as hasPattern, s as isBlob, t as endsWith, u as isDefined, v as isNaN, w as isSet, x as isPromiseLike, y as isNull } from "./assertions-DPYCHREw.mjs";
|
|
2
3
|
import { discriminatedUnion, each, exact, nullable, nullish, optional, record, shape, tuple, union } from "./combinators.mjs";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (normalizedLeft.length !== right.length) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return normalizedLeft.every((part, index) => Object.is(part, right[index]));
|
|
4
|
+
//#region src/violations.ts
|
|
5
|
+
var ROOT_PATH = Object.freeze([]);
|
|
6
|
+
var normalizePath = (path) => path ?? ROOT_PATH;
|
|
7
|
+
var isSamePath = (left, right = ROOT_PATH) => {
|
|
8
|
+
const normalizedLeft = normalizePath(left);
|
|
9
|
+
if (normalizedLeft.length !== right.length) return false;
|
|
10
|
+
return normalizedLeft.every((part, index) => Object.is(part, right[index]));
|
|
14
11
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
return prefix.every((part, index) => Object.is(part, path[index]));
|
|
12
|
+
var isPathPrefix = (path, prefix) => {
|
|
13
|
+
if (prefix.length > path.length) return false;
|
|
14
|
+
return prefix.every((part, index) => Object.is(part, path[index]));
|
|
20
15
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
var createTreeState = (path) => ({
|
|
17
|
+
path: Object.freeze([...path]),
|
|
18
|
+
self: [],
|
|
19
|
+
subtree: [],
|
|
20
|
+
children: /* @__PURE__ */ new Map()
|
|
26
21
|
});
|
|
27
|
-
class TreeNode {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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);
|
|
22
|
+
var TreeNode = class TreeNode {
|
|
23
|
+
constructor(state) {
|
|
24
|
+
this.path = state.path;
|
|
25
|
+
this.self = new ViolationCollection(state.self);
|
|
26
|
+
this.subtree = new ViolationCollection(state.subtree);
|
|
27
|
+
this.children = new Map([...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)]));
|
|
28
|
+
}
|
|
29
|
+
at(path) {
|
|
30
|
+
if (!isPathPrefix(path, this.path)) return;
|
|
31
|
+
if (path.length === this.path.length) return this;
|
|
32
|
+
return this.children.get(path[this.path.length])?.at(path);
|
|
33
|
+
}
|
|
73
34
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return settle(value, path, validations);
|
|
35
|
+
var buildTree = (violations) => {
|
|
36
|
+
const root = createTreeState(ROOT_PATH);
|
|
37
|
+
violations.forEach((violation) => {
|
|
38
|
+
const path = normalizePath(violation.path);
|
|
39
|
+
root.subtree.push(violation);
|
|
40
|
+
if (path.length === 0) {
|
|
41
|
+
root.self.push(violation);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let current = root;
|
|
45
|
+
const currentPath = [];
|
|
46
|
+
path.forEach((segment, index) => {
|
|
47
|
+
currentPath.push(segment);
|
|
48
|
+
let child = current.children.get(segment);
|
|
49
|
+
if (!child) {
|
|
50
|
+
child = createTreeState(currentPath);
|
|
51
|
+
current.children.set(segment, child);
|
|
52
|
+
}
|
|
53
|
+
child.subtree.push(violation);
|
|
54
|
+
if (index === path.length - 1) child.self.push(violation);
|
|
55
|
+
current = child;
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
return new TreeNode(root);
|
|
59
|
+
};
|
|
60
|
+
var ViolationCollection = class ViolationCollection {
|
|
61
|
+
constructor(violations) {
|
|
62
|
+
this.violations = [...violations];
|
|
63
|
+
this.size = this.violations.length;
|
|
64
|
+
}
|
|
65
|
+
[Symbol.iterator]() {
|
|
66
|
+
return this.violations[Symbol.iterator]();
|
|
67
|
+
}
|
|
68
|
+
forEach(callback) {
|
|
69
|
+
this.violations.forEach((violation, index) => callback(violation, index, this));
|
|
70
|
+
}
|
|
71
|
+
map(callback) {
|
|
72
|
+
return this.violations.map((violation, index) => callback(violation, index, this));
|
|
73
|
+
}
|
|
74
|
+
at(path) {
|
|
75
|
+
return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
|
|
76
|
+
}
|
|
77
|
+
tree() {
|
|
78
|
+
return buildTree(this.violations);
|
|
79
|
+
}
|
|
122
80
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
81
|
+
var collection = (violations) => new ViolationCollection(violations);
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/index.ts
|
|
84
|
+
var collectViolations = async (value, constraints, path = []) => {
|
|
85
|
+
const validations = [];
|
|
86
|
+
let establishedAssertionDomain = false;
|
|
87
|
+
for (const c of arrayify(constraints)) {
|
|
88
|
+
if (isValidator(c)) {
|
|
89
|
+
establishedAssertionDomain = false;
|
|
90
|
+
validations.push(...c.run(collectViolations, value, path).map((v) => v instanceof Promise ? v : Promise.resolve(v)));
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (!establishedAssertionDomain && isRefinementAssertion(c)) throw new Error(`Refinement ${String(c.name)} requires a compatible preceding guard`);
|
|
94
|
+
const v = establishedAssertionDomain && isRefinementAssertion(c) ? runRefinementAssertion(c, value) : c(value);
|
|
95
|
+
if (v instanceof Promise) {
|
|
96
|
+
establishedAssertionDomain = false;
|
|
97
|
+
if (c.bail) {
|
|
98
|
+
const awaited = await v;
|
|
99
|
+
if (awaited) {
|
|
100
|
+
validations.push(Promise.resolve([Object.assign(awaited, { path: [...path] })]));
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
} else validations.push(v.then((v) => v ? [Object.assign(v, { path: [...path] })] : []));
|
|
104
|
+
} else if (v) {
|
|
105
|
+
establishedAssertionDomain = false;
|
|
106
|
+
validations.push(Promise.resolve([Object.assign(v, { path: [...path] })]));
|
|
107
|
+
if (c.bail) break;
|
|
108
|
+
} else establishedAssertionDomain = true;
|
|
109
|
+
}
|
|
110
|
+
return settle(value, path, validations);
|
|
139
111
|
};
|
|
112
|
+
collectViolations.sync = false;
|
|
113
|
+
var collectViolationsSync = (value, constraints, path = []) => {
|
|
114
|
+
const violations = [];
|
|
115
|
+
let establishedAssertionDomain = false;
|
|
116
|
+
for (const c of arrayify(constraints)) {
|
|
117
|
+
if (isValidator(c)) {
|
|
118
|
+
establishedAssertionDomain = false;
|
|
119
|
+
violations.push(...c.run(collectViolationsSync, value, path));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (!establishedAssertionDomain && isRefinementAssertion(c)) throw new Error(`Refinement ${String(c.name)} requires a compatible preceding guard`);
|
|
123
|
+
const v = establishedAssertionDomain && isRefinementAssertion(c) ? runRefinementAssertion(c, value) : c(value);
|
|
124
|
+
if (v instanceof Promise) throw new Error("Found asynchronous validator " + String(c.name));
|
|
125
|
+
else if (v) {
|
|
126
|
+
establishedAssertionDomain = false;
|
|
127
|
+
violations.push(Object.assign(v, { path: [...path] }));
|
|
128
|
+
if (c.bail) break;
|
|
129
|
+
} else establishedAssertionDomain = true;
|
|
130
|
+
}
|
|
131
|
+
return flatten(violations);
|
|
132
|
+
};
|
|
133
|
+
collectViolationsSync.sync = true;
|
|
140
134
|
function toResult(value, violations) {
|
|
141
|
-
|
|
135
|
+
return violations.length === 0 ? [
|
|
136
|
+
true,
|
|
137
|
+
value,
|
|
138
|
+
[]
|
|
139
|
+
] : [
|
|
140
|
+
false,
|
|
141
|
+
value,
|
|
142
|
+
violations
|
|
143
|
+
];
|
|
142
144
|
}
|
|
143
145
|
function flatten(recursive) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
const flattened = [];
|
|
147
|
+
recursive.forEach((element) => {
|
|
148
|
+
flattened.push(...Array.isArray(element) ? flatten(element) : [element]);
|
|
149
|
+
});
|
|
150
|
+
return flattened;
|
|
149
151
|
}
|
|
150
152
|
async function settle(value, path, validations) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
153
|
+
const violations = [];
|
|
154
|
+
(await Promise.allSettled(validations)).forEach((result) => {
|
|
155
|
+
if (result.status === "fulfilled") violations.push(...result.value);
|
|
156
|
+
else violations.push({
|
|
157
|
+
value,
|
|
158
|
+
path,
|
|
159
|
+
violates: {
|
|
160
|
+
kind: "runtime",
|
|
161
|
+
name: "validate",
|
|
162
|
+
code: "runtime.rejection",
|
|
163
|
+
args: [result.reason]
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
return violations;
|
|
165
168
|
}
|
|
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
|
-
};
|
|
169
|
+
var matches = { sync(value, constraints) {
|
|
170
|
+
return collectViolationsSync(value, constraints).length === 0;
|
|
171
|
+
} };
|
|
172
|
+
var validate = /* @__PURE__ */ Object.assign(async (value, constraints) => {
|
|
173
|
+
return toResult(value, await collectViolations(value, constraints));
|
|
174
|
+
}, { sync(value, constraints) {
|
|
175
|
+
return toResult(value, collectViolationsSync(value, constraints));
|
|
176
|
+
} });
|
|
177
|
+
//#endregion
|
|
178
|
+
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 };
|