@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.
Files changed (84) hide show
  1. package/CHANGELOG.md +27 -1
  2. package/README.md +12 -2
  3. package/dist/assert-BDOtHdLx.cjs +289 -0
  4. package/dist/assert-CzMg5aO7.mjs +206 -0
  5. package/dist/assert.cjs +7 -65
  6. package/dist/assert.d.cts +37 -0
  7. package/dist/assert.d.mts +37 -0
  8. package/dist/assert.d.ts +24 -3
  9. package/dist/assert.mjs +2 -66
  10. package/dist/assertions-DPYCHREw.mjs +297 -0
  11. package/dist/assertions-nfOKqcjs.cjs +476 -0
  12. package/dist/assertions.cjs +34 -206
  13. package/dist/assertions.d.cts +56 -0
  14. package/dist/assertions.d.mts +56 -0
  15. package/dist/assertions.d.ts +43 -45
  16. package/dist/assertions.mjs +3 -207
  17. package/dist/checkers.d.cts +8 -0
  18. package/dist/checkers.d.mts +8 -0
  19. package/dist/checkers.d.ts +1 -1
  20. package/dist/combinators.cjs +303 -304
  21. package/dist/combinators.d.cts +16 -0
  22. package/dist/combinators.d.mts +16 -0
  23. package/dist/combinators.d.ts +15 -16
  24. package/dist/combinators.mjs +304 -315
  25. package/dist/constraints.d.cts +4 -0
  26. package/dist/constraints.d.mts +4 -0
  27. package/dist/constraints.d.ts +2 -2
  28. package/dist/extractors.d.cts +2 -0
  29. package/dist/extractors.d.mts +2 -0
  30. package/dist/index.cjs +210 -213
  31. package/dist/index.d.cts +12 -0
  32. package/dist/index.d.mts +12 -0
  33. package/dist/index.d.ts +9 -9
  34. package/dist/index.mjs +165 -218
  35. package/dist/json-schema.cjs +364 -489
  36. package/dist/json-schema.d.cts +14 -0
  37. package/dist/json-schema.d.mts +14 -0
  38. package/dist/json-schema.d.ts +3 -3
  39. package/dist/json-schema.mjs +365 -492
  40. package/dist/metadata.cjs +6 -7
  41. package/dist/metadata.d.cts +8 -0
  42. package/dist/metadata.d.mts +8 -0
  43. package/dist/metadata.d.ts +2 -2
  44. package/dist/metadata.mjs +2 -8
  45. package/dist/predicates.cjs +98 -41
  46. package/dist/predicates.d.cts +77 -0
  47. package/dist/predicates.d.mts +77 -0
  48. package/dist/predicates.d.ts +25 -4
  49. package/dist/predicates.mjs +92 -66
  50. package/dist/types/index.d.cts +984 -0
  51. package/dist/types/index.d.mts +984 -0
  52. package/dist/types/index.d.ts +984 -0
  53. package/dist/types/json-schema.d.cts +75 -0
  54. package/dist/types/json-schema.d.mts +75 -0
  55. package/dist/types/json-schema.d.ts +75 -0
  56. package/dist/violations.d.cts +29 -0
  57. package/dist/violations.d.mts +29 -0
  58. package/dist/violations.d.ts +1 -1
  59. package/docs/RELEASING.md +66 -0
  60. package/docs/en/00-index.md +2 -0
  61. package/docs/en/01-shape-api.md +35 -10
  62. package/docs/en/02-metadata-and-introspection.md +2 -1
  63. package/docs/en/03-violations.md +1 -1
  64. package/docs/en/04-json-schema-export.md +5 -0
  65. package/docs/en/05-public-api.md +35 -3
  66. package/docs/en/06-common-recipes.md +2 -1
  67. package/docs/en/07-ai-reference.md +5 -2
  68. package/docs/en/08-violation-code-types.md +28 -2
  69. package/docs/en/09-migration.md +75 -0
  70. package/docs/ru/00-index.md +2 -0
  71. package/docs/ru/01-shape-api.md +35 -10
  72. package/docs/ru/02-metadata-and-introspection.md +2 -1
  73. package/docs/ru/03-violations.md +1 -1
  74. package/docs/ru/04-json-schema-export.md +5 -0
  75. package/docs/ru/05-public-api.md +35 -3
  76. package/docs/ru/06-common-recipes.md +2 -1
  77. package/docs/ru/07-ai-reference.md +5 -2
  78. package/docs/ru/08-violation-code-types.md +28 -2
  79. package/docs/ru/09-migration.md +76 -0
  80. package/docs/ru/README.md +10 -2
  81. package/package.json +63 -37
  82. package/types/index.d.ts +275 -115
  83. package/dist/metadata.cjs.js +0 -130
  84. package/dist/metadata.es.js +0 -131
package/dist/index.mjs CHANGED
@@ -1,231 +1,178 @@
1
- import { endsWith, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isFile, isFunction, isMap, isNaN, isNull, isNumber, isSet, isString, isSymbol, multipleOf, oneOf, startsWith } from "./assertions.mjs";
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
- import { a as arrayify, i as isValidator } from "./metadata.es.js";
4
- import { c, b, m } from "./metadata.es.js";
5
- import { assert } from "./assert.mjs";
6
- const ROOT_PATH = Object.freeze([]);
7
- const normalizePath = (path) => path ?? ROOT_PATH;
8
- const isSamePath = (left, right = ROOT_PATH) => {
9
- const normalizedLeft = normalizePath(left);
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
- const isPathPrefix = (path, prefix) => {
16
- if (prefix.length > path.length) {
17
- return false;
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
- const createTreeState = (path) => ({
22
- path: Object.freeze([...path]),
23
- self: [],
24
- subtree: [],
25
- children: /* @__PURE__ */ new Map()
16
+ var createTreeState = (path) => ({
17
+ path: Object.freeze([...path]),
18
+ self: [],
19
+ subtree: [],
20
+ children: /* @__PURE__ */ new Map()
26
21
  });
27
- class TreeNode {
28
- constructor(state) {
29
- this.path = state.path;
30
- this.self = new ViolationCollection(state.self);
31
- this.subtree = new ViolationCollection(state.subtree);
32
- this.children = new Map(
33
- [...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)])
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);
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
- 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);
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
- 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);
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
- return violations.length === 0 ? [true, value, []] : [false, value, violations];
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
- const flattened = [];
145
- recursive.forEach((element) => {
146
- flattened.push(...Array.isArray(element) ? flatten(element) : [element]);
147
- });
148
- return flattened;
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
- const violations = [];
152
- const settled = await Promise.allSettled(validations);
153
- settled.forEach((result) => {
154
- if (result.status === "fulfilled") {
155
- violations.push(...result.value);
156
- } else {
157
- violations.push({
158
- value,
159
- path,
160
- violates: { kind: "runtime", name: "validate", code: "runtime.rejection", args: [result.reason] }
161
- });
162
- }
163
- });
164
- return violations;
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
- const matches = {
167
- sync(value, constraints) {
168
- return collectViolationsSync(value, constraints).length === 0;
169
- }
170
- };
171
- const validate = Object.assign(
172
- async (value, constraints) => {
173
- const violations = await collectViolations(value, constraints);
174
- return toResult(
175
- value,
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 };