@modulify/validator 0.3.0 → 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.
@@ -1,289 +0,0 @@
1
- //#region src/constraints.ts
2
- var isValidator = (constraint) => "run" in constraint;
3
- function arrayify(value) {
4
- return Array.isArray(value) ? [...value] : [value];
5
- }
6
- function matchesConstraints(value, constraints) {
7
- let establishedDomain = false;
8
- return arrayify(constraints).every((constraint) => {
9
- if (isValidator(constraint)) {
10
- establishedDomain = false;
11
- return constraint.check(value);
12
- }
13
- if (!establishedDomain && isRefinementAssertion(constraint)) return false;
14
- const matched = establishedDomain && isRefinementAssertion(constraint) ? checkRefinementAssertion(constraint, value) : constraint.check(value);
15
- if (matched) establishedDomain = true;
16
- return matched;
17
- });
18
- }
19
- //#endregion
20
- //#region src/metadata.ts
21
- var constraintDescriptorSymbol = Symbol("modulify.validator.descriptor");
22
- var constraintMetadataSymbol = Symbol("modulify.validator.metadata");
23
- var cloneCallableConstraint = (constraint) => {
24
- const source = constraint;
25
- const cloned = ((value) => source(value));
26
- const { length: _length, name: _name, prototype: _prototype, ...descriptors } = Object.getOwnPropertyDescriptors(source);
27
- Object.defineProperties(cloned, descriptors);
28
- try {
29
- Object.defineProperty(cloned, "name", {
30
- configurable: true,
31
- value: source.name
32
- });
33
- } catch {}
34
- Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
35
- return cloned;
36
- };
37
- var cloneConstraint = (constraint) => {
38
- if (typeof constraint === "function") return cloneCallableConstraint(constraint);
39
- return Object.create(Object.getPrototypeOf(constraint), Object.getOwnPropertyDescriptors(constraint));
40
- };
41
- var freezeMetadata = (metadata) => Object.freeze({ ...metadata });
42
- var getConstraintMetadata = (constraint) => {
43
- return constraint[constraintMetadataSymbol];
44
- };
45
- var getDescriptorFactory = (constraint) => {
46
- return constraint[constraintDescriptorSymbol];
47
- };
48
- var getPublicDescriptor = (constraint) => {
49
- if (!isValidator(constraint)) return;
50
- const { describe } = constraint;
51
- return typeof describe === "function" ? describe.call(constraint) : void 0;
52
- };
53
- var inferAssertionDescriptor = (assertion) => ({
54
- kind: "assertion",
55
- name: assertion.name,
56
- bail: assertion.bail,
57
- code: assertion.name,
58
- args: [],
59
- constraints: assertion.constraints.map(([, , code, ...args]) => ({
60
- code,
61
- args
62
- }))
63
- });
64
- var withMetadata = (descriptor, metadata) => {
65
- if (!metadata) return descriptor;
66
- return {
67
- ...descriptor,
68
- metadata
69
- };
70
- };
71
- var attachConstraintDescriptor = (constraint, describe) => {
72
- Object.defineProperty(constraint, constraintDescriptorSymbol, {
73
- configurable: false,
74
- enumerable: false,
75
- value: describe,
76
- writable: false
77
- });
78
- return constraint;
79
- };
80
- var meta = (constraint, metadata) => {
81
- const cloned = cloneConstraint(constraint);
82
- const nextMetadata = freezeMetadata({
83
- ...getConstraintMetadata(constraint) ?? {},
84
- ...metadata
85
- });
86
- Object.defineProperty(cloned, constraintMetadataSymbol, {
87
- configurable: true,
88
- enumerable: false,
89
- value: nextMetadata,
90
- writable: false
91
- });
92
- return cloned;
93
- };
94
- var custom = (validator) => validator;
95
- var describeConstraints = (constraints) => {
96
- const values = arrayify(constraints);
97
- return values.length === 1 ? describe(values[0]) : {
98
- kind: "allOf",
99
- constraints: values.map((value) => describe(value))
100
- };
101
- };
102
- var describe = (constraint) => {
103
- const factory = getDescriptorFactory(constraint);
104
- const metadata = getConstraintMetadata(constraint);
105
- if (factory) return withMetadata(factory(), metadata);
106
- const publicDescriptor = getPublicDescriptor(constraint);
107
- if (publicDescriptor) return withMetadata(publicDescriptor, metadata);
108
- if (isValidator(constraint)) return withMetadata({ kind: "validator" }, metadata);
109
- return withMetadata(inferAssertionDescriptor(constraint), metadata);
110
- };
111
- //#endregion
112
- //#region src/assert.ts
113
- var assertionStageSymbol = Symbol("modulify.validator.assertion.stage");
114
- var runRefinementSymbol = Symbol("modulify.validator.assertion.runRefinement");
115
- var checkRefinementSymbol = Symbol("modulify.validator.assertion.checkRefinement");
116
- var createAssertion = (stage, predicate, meta, constraints = []) => {
117
- const assertionCode = meta.code ?? meta.name;
118
- const assertionArgs = meta.args ?? [];
119
- const violationSubject = {
120
- kind: "assertion",
121
- name: meta.name,
122
- code: assertionCode,
123
- args: assertionArgs
124
- };
125
- const runConstraints = (value) => {
126
- for (const [extract, check, code, ...args] of constraints) if (!check(extract(value), ...args)) return {
127
- value,
128
- violates: {
129
- kind: "assertion",
130
- name: meta.name,
131
- code,
132
- args
133
- }
134
- };
135
- return null;
136
- };
137
- const checkConstraints = (value) => !constraints.length || constraints.every(([extract, check, , ...args]) => check(extract(value), ...args));
138
- const assertion = ((value) => {
139
- if (!predicate(value)) return {
140
- value,
141
- violates: violationSubject
142
- };
143
- return runConstraints(value);
144
- });
145
- Object.defineProperties(assertion, {
146
- name: {
147
- configurable: true,
148
- value: meta.name
149
- },
150
- bail: {
151
- enumerable: true,
152
- value: meta.bail
153
- },
154
- constraints: {
155
- enumerable: true,
156
- value: constraints
157
- },
158
- check: {
159
- enumerable: true,
160
- value: (value) => predicate(value) && checkConstraints(value)
161
- },
162
- [assertionStageSymbol]: {
163
- enumerable: false,
164
- value: stage
165
- },
166
- [runRefinementSymbol]: {
167
- enumerable: false,
168
- value: (value) => runConstraints(value)
169
- },
170
- [checkRefinementSymbol]: {
171
- enumerable: false,
172
- value: (value) => checkConstraints(value)
173
- }
174
- });
175
- return /* @__PURE__ */ attachConstraintDescriptor(assertion, () => ({
176
- kind: "assertion",
177
- name: meta.name,
178
- bail: meta.bail,
179
- code: assertionCode,
180
- args: assertionArgs,
181
- constraints: constraints.map(([, , code, ...args]) => ({
182
- code,
183
- args
184
- }))
185
- }));
186
- };
187
- var assert = (predicate, meta, constraints = []) => {
188
- return createAssertion("guard", predicate, meta, constraints);
189
- };
190
- var isRefinementAssertion = (constraint) => {
191
- return typeof constraint === "function" && constraint[assertionStageSymbol] === "refinement";
192
- };
193
- var runRefinementAssertion = (constraint, value) => {
194
- const runRefinement = constraint[runRefinementSymbol];
195
- return runRefinement ? runRefinement(value) : constraint(value);
196
- };
197
- var checkRefinementAssertion = (constraint, value) => {
198
- const checkRefinement = constraint[checkRefinementSymbol];
199
- return checkRefinement ? checkRefinement(value) : constraint.check(value);
200
- };
201
- var createRefinement = (meta, constraints = []) => {
202
- return createAssertion("refinement", ((value) => true), meta, constraints);
203
- };
204
- var refine = createRefinement;
205
- //#endregion
206
- Object.defineProperty(exports, "arrayify", {
207
- enumerable: true,
208
- get: function() {
209
- return arrayify;
210
- }
211
- });
212
- Object.defineProperty(exports, "assert", {
213
- enumerable: true,
214
- get: function() {
215
- return assert;
216
- }
217
- });
218
- Object.defineProperty(exports, "attachConstraintDescriptor", {
219
- enumerable: true,
220
- get: function() {
221
- return attachConstraintDescriptor;
222
- }
223
- });
224
- Object.defineProperty(exports, "checkRefinementAssertion", {
225
- enumerable: true,
226
- get: function() {
227
- return checkRefinementAssertion;
228
- }
229
- });
230
- Object.defineProperty(exports, "createRefinement", {
231
- enumerable: true,
232
- get: function() {
233
- return createRefinement;
234
- }
235
- });
236
- Object.defineProperty(exports, "custom", {
237
- enumerable: true,
238
- get: function() {
239
- return custom;
240
- }
241
- });
242
- Object.defineProperty(exports, "describe", {
243
- enumerable: true,
244
- get: function() {
245
- return describe;
246
- }
247
- });
248
- Object.defineProperty(exports, "describeConstraints", {
249
- enumerable: true,
250
- get: function() {
251
- return describeConstraints;
252
- }
253
- });
254
- Object.defineProperty(exports, "isRefinementAssertion", {
255
- enumerable: true,
256
- get: function() {
257
- return isRefinementAssertion;
258
- }
259
- });
260
- Object.defineProperty(exports, "isValidator", {
261
- enumerable: true,
262
- get: function() {
263
- return isValidator;
264
- }
265
- });
266
- Object.defineProperty(exports, "matchesConstraints", {
267
- enumerable: true,
268
- get: function() {
269
- return matchesConstraints;
270
- }
271
- });
272
- Object.defineProperty(exports, "meta", {
273
- enumerable: true,
274
- get: function() {
275
- return meta;
276
- }
277
- });
278
- Object.defineProperty(exports, "refine", {
279
- enumerable: true,
280
- get: function() {
281
- return refine;
282
- }
283
- });
284
- Object.defineProperty(exports, "runRefinementAssertion", {
285
- enumerable: true,
286
- get: function() {
287
- return runRefinementAssertion;
288
- }
289
- });
@@ -1,206 +0,0 @@
1
- //#region src/constraints.ts
2
- var isValidator = (constraint) => "run" in constraint;
3
- function arrayify(value) {
4
- return Array.isArray(value) ? [...value] : [value];
5
- }
6
- function matchesConstraints(value, constraints) {
7
- let establishedDomain = false;
8
- return arrayify(constraints).every((constraint) => {
9
- if (isValidator(constraint)) {
10
- establishedDomain = false;
11
- return constraint.check(value);
12
- }
13
- if (!establishedDomain && isRefinementAssertion(constraint)) return false;
14
- const matched = establishedDomain && isRefinementAssertion(constraint) ? checkRefinementAssertion(constraint, value) : constraint.check(value);
15
- if (matched) establishedDomain = true;
16
- return matched;
17
- });
18
- }
19
- //#endregion
20
- //#region src/metadata.ts
21
- var constraintDescriptorSymbol = Symbol("modulify.validator.descriptor");
22
- var constraintMetadataSymbol = Symbol("modulify.validator.metadata");
23
- var cloneCallableConstraint = (constraint) => {
24
- const source = constraint;
25
- const cloned = ((value) => source(value));
26
- const { length: _length, name: _name, prototype: _prototype, ...descriptors } = Object.getOwnPropertyDescriptors(source);
27
- Object.defineProperties(cloned, descriptors);
28
- try {
29
- Object.defineProperty(cloned, "name", {
30
- configurable: true,
31
- value: source.name
32
- });
33
- } catch {}
34
- Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
35
- return cloned;
36
- };
37
- var cloneConstraint = (constraint) => {
38
- if (typeof constraint === "function") return cloneCallableConstraint(constraint);
39
- return Object.create(Object.getPrototypeOf(constraint), Object.getOwnPropertyDescriptors(constraint));
40
- };
41
- var freezeMetadata = (metadata) => Object.freeze({ ...metadata });
42
- var getConstraintMetadata = (constraint) => {
43
- return constraint[constraintMetadataSymbol];
44
- };
45
- var getDescriptorFactory = (constraint) => {
46
- return constraint[constraintDescriptorSymbol];
47
- };
48
- var getPublicDescriptor = (constraint) => {
49
- if (!isValidator(constraint)) return;
50
- const { describe } = constraint;
51
- return typeof describe === "function" ? describe.call(constraint) : void 0;
52
- };
53
- var inferAssertionDescriptor = (assertion) => ({
54
- kind: "assertion",
55
- name: assertion.name,
56
- bail: assertion.bail,
57
- code: assertion.name,
58
- args: [],
59
- constraints: assertion.constraints.map(([, , code, ...args]) => ({
60
- code,
61
- args
62
- }))
63
- });
64
- var withMetadata = (descriptor, metadata) => {
65
- if (!metadata) return descriptor;
66
- return {
67
- ...descriptor,
68
- metadata
69
- };
70
- };
71
- var attachConstraintDescriptor = (constraint, describe) => {
72
- Object.defineProperty(constraint, constraintDescriptorSymbol, {
73
- configurable: false,
74
- enumerable: false,
75
- value: describe,
76
- writable: false
77
- });
78
- return constraint;
79
- };
80
- var meta = (constraint, metadata) => {
81
- const cloned = cloneConstraint(constraint);
82
- const nextMetadata = freezeMetadata({
83
- ...getConstraintMetadata(constraint) ?? {},
84
- ...metadata
85
- });
86
- Object.defineProperty(cloned, constraintMetadataSymbol, {
87
- configurable: true,
88
- enumerable: false,
89
- value: nextMetadata,
90
- writable: false
91
- });
92
- return cloned;
93
- };
94
- var custom = (validator) => validator;
95
- var describeConstraints = (constraints) => {
96
- const values = arrayify(constraints);
97
- return values.length === 1 ? describe(values[0]) : {
98
- kind: "allOf",
99
- constraints: values.map((value) => describe(value))
100
- };
101
- };
102
- var describe = (constraint) => {
103
- const factory = getDescriptorFactory(constraint);
104
- const metadata = getConstraintMetadata(constraint);
105
- if (factory) return withMetadata(factory(), metadata);
106
- const publicDescriptor = getPublicDescriptor(constraint);
107
- if (publicDescriptor) return withMetadata(publicDescriptor, metadata);
108
- if (isValidator(constraint)) return withMetadata({ kind: "validator" }, metadata);
109
- return withMetadata(inferAssertionDescriptor(constraint), metadata);
110
- };
111
- //#endregion
112
- //#region src/assert.ts
113
- var assertionStageSymbol = Symbol("modulify.validator.assertion.stage");
114
- var runRefinementSymbol = Symbol("modulify.validator.assertion.runRefinement");
115
- var checkRefinementSymbol = Symbol("modulify.validator.assertion.checkRefinement");
116
- var createAssertion = (stage, predicate, meta, constraints = []) => {
117
- const assertionCode = meta.code ?? meta.name;
118
- const assertionArgs = meta.args ?? [];
119
- const violationSubject = {
120
- kind: "assertion",
121
- name: meta.name,
122
- code: assertionCode,
123
- args: assertionArgs
124
- };
125
- const runConstraints = (value) => {
126
- for (const [extract, check, code, ...args] of constraints) if (!check(extract(value), ...args)) return {
127
- value,
128
- violates: {
129
- kind: "assertion",
130
- name: meta.name,
131
- code,
132
- args
133
- }
134
- };
135
- return null;
136
- };
137
- const checkConstraints = (value) => !constraints.length || constraints.every(([extract, check, , ...args]) => check(extract(value), ...args));
138
- const assertion = ((value) => {
139
- if (!predicate(value)) return {
140
- value,
141
- violates: violationSubject
142
- };
143
- return runConstraints(value);
144
- });
145
- Object.defineProperties(assertion, {
146
- name: {
147
- configurable: true,
148
- value: meta.name
149
- },
150
- bail: {
151
- enumerable: true,
152
- value: meta.bail
153
- },
154
- constraints: {
155
- enumerable: true,
156
- value: constraints
157
- },
158
- check: {
159
- enumerable: true,
160
- value: (value) => predicate(value) && checkConstraints(value)
161
- },
162
- [assertionStageSymbol]: {
163
- enumerable: false,
164
- value: stage
165
- },
166
- [runRefinementSymbol]: {
167
- enumerable: false,
168
- value: (value) => runConstraints(value)
169
- },
170
- [checkRefinementSymbol]: {
171
- enumerable: false,
172
- value: (value) => checkConstraints(value)
173
- }
174
- });
175
- return /* @__PURE__ */ attachConstraintDescriptor(assertion, () => ({
176
- kind: "assertion",
177
- name: meta.name,
178
- bail: meta.bail,
179
- code: assertionCode,
180
- args: assertionArgs,
181
- constraints: constraints.map(([, , code, ...args]) => ({
182
- code,
183
- args
184
- }))
185
- }));
186
- };
187
- var assert = (predicate, meta, constraints = []) => {
188
- return createAssertion("guard", predicate, meta, constraints);
189
- };
190
- var isRefinementAssertion = (constraint) => {
191
- return typeof constraint === "function" && constraint[assertionStageSymbol] === "refinement";
192
- };
193
- var runRefinementAssertion = (constraint, value) => {
194
- const runRefinement = constraint[runRefinementSymbol];
195
- return runRefinement ? runRefinement(value) : constraint(value);
196
- };
197
- var checkRefinementAssertion = (constraint, value) => {
198
- const checkRefinement = constraint[checkRefinementSymbol];
199
- return checkRefinement ? checkRefinement(value) : constraint.check(value);
200
- };
201
- var createRefinement = (meta, constraints = []) => {
202
- return createAssertion("refinement", ((value) => true), meta, constraints);
203
- };
204
- var refine = createRefinement;
205
- //#endregion
206
- export { refine as a, custom as c, meta as d, arrayify as f, isRefinementAssertion as i, describe as l, matchesConstraints as m, checkRefinementAssertion as n, runRefinementAssertion as o, isValidator as p, createRefinement as r, attachConstraintDescriptor as s, assert as t, describeConstraints as u };