@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.
Files changed (88) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/README.md +12 -2
  3. package/dist/assert.cjs +99 -63
  4. package/dist/assert.d.cts +37 -0
  5. package/dist/assert.d.mts +37 -0
  6. package/dist/assert.d.ts +24 -3
  7. package/dist/assert.mjs +94 -64
  8. package/dist/assertions.cjs +283 -178
  9. package/dist/assertions.d.cts +56 -0
  10. package/dist/assertions.d.mts +56 -0
  11. package/dist/assertions.d.ts +43 -45
  12. package/dist/assertions.mjs +274 -201
  13. package/dist/checkers.cjs +23 -0
  14. package/dist/checkers.d.cts +8 -0
  15. package/dist/checkers.d.mts +8 -0
  16. package/dist/checkers.d.ts +1 -1
  17. package/dist/checkers.mjs +16 -0
  18. package/dist/combinators.cjs +305 -304
  19. package/dist/combinators.d.cts +16 -0
  20. package/dist/combinators.d.mts +16 -0
  21. package/dist/combinators.d.ts +15 -16
  22. package/dist/combinators.mjs +305 -314
  23. package/dist/constraints.cjs +23 -0
  24. package/dist/constraints.d.cts +4 -0
  25. package/dist/constraints.d.mts +4 -0
  26. package/dist/constraints.d.ts +2 -2
  27. package/dist/constraints.mjs +21 -0
  28. package/dist/extractors.cjs +6 -0
  29. package/dist/extractors.d.cts +2 -0
  30. package/dist/extractors.d.mts +2 -0
  31. package/dist/extractors.mjs +5 -0
  32. package/dist/index.cjs +140 -219
  33. package/dist/index.d.cts +12 -0
  34. package/dist/index.d.mts +12 -0
  35. package/dist/index.d.ts +9 -9
  36. package/dist/index.mjs +93 -222
  37. package/dist/json-schema.cjs +364 -489
  38. package/dist/json-schema.d.cts +14 -0
  39. package/dist/json-schema.d.mts +14 -0
  40. package/dist/json-schema.d.ts +3 -3
  41. package/dist/json-schema.mjs +365 -492
  42. package/dist/metadata.cjs +98 -7
  43. package/dist/metadata.d.cts +8 -0
  44. package/dist/metadata.d.mts +8 -0
  45. package/dist/metadata.d.ts +2 -2
  46. package/dist/metadata.mjs +93 -7
  47. package/dist/predicates.cjs +98 -41
  48. package/dist/predicates.d.cts +77 -0
  49. package/dist/predicates.d.mts +77 -0
  50. package/dist/predicates.d.ts +25 -4
  51. package/dist/predicates.mjs +92 -66
  52. package/dist/types/index.d.cts +984 -0
  53. package/dist/types/index.d.mts +984 -0
  54. package/dist/types/index.d.ts +984 -0
  55. package/dist/types/json-schema.d.cts +75 -0
  56. package/dist/types/json-schema.d.mts +75 -0
  57. package/dist/types/json-schema.d.ts +75 -0
  58. package/dist/violations.cjs +81 -0
  59. package/dist/violations.d.cts +29 -0
  60. package/dist/violations.d.mts +29 -0
  61. package/dist/violations.d.ts +1 -1
  62. package/dist/violations.mjs +80 -0
  63. package/docs/RELEASING.md +66 -0
  64. package/docs/en/00-index.md +2 -0
  65. package/docs/en/01-shape-api.md +35 -10
  66. package/docs/en/02-metadata-and-introspection.md +2 -1
  67. package/docs/en/03-violations.md +1 -1
  68. package/docs/en/04-json-schema-export.md +5 -0
  69. package/docs/en/05-public-api.md +35 -3
  70. package/docs/en/06-common-recipes.md +2 -1
  71. package/docs/en/07-ai-reference.md +5 -2
  72. package/docs/en/08-violation-code-types.md +28 -2
  73. package/docs/en/09-migration.md +75 -0
  74. package/docs/ru/00-index.md +2 -0
  75. package/docs/ru/01-shape-api.md +35 -10
  76. package/docs/ru/02-metadata-and-introspection.md +2 -1
  77. package/docs/ru/03-violations.md +1 -1
  78. package/docs/ru/04-json-schema-export.md +5 -0
  79. package/docs/ru/05-public-api.md +35 -3
  80. package/docs/ru/06-common-recipes.md +2 -1
  81. package/docs/ru/07-ai-reference.md +5 -2
  82. package/docs/ru/08-violation-code-types.md +28 -2
  83. package/docs/ru/09-migration.md +76 -0
  84. package/docs/ru/README.md +10 -2
  85. package/package.json +63 -37
  86. package/types/index.d.ts +275 -115
  87. package/dist/metadata.cjs.js +0 -130
  88. package/dist/metadata.es.js +0 -131
package/dist/index.mjs CHANGED
@@ -1,231 +1,102 @@
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 { 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 { 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]));
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
- 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]));
20
- };
21
- const createTreeState = (path) => ({
22
- path: Object.freeze([...path]),
23
- self: [],
24
- subtree: [],
25
- children: /* @__PURE__ */ new Map()
26
- });
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);
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
- return violations.length === 0 ? [true, value, []] : [false, value, violations];
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
- const flattened = [];
145
- recursive.forEach((element) => {
146
- flattened.push(...Array.isArray(element) ? flatten(element) : [element]);
147
- });
148
- return flattened;
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
- 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;
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
- 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
- };
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 };