@dzeio/schema 0.0.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/dist/Schema.d.mts +332 -0
- package/dist/Schema.d.ts +332 -0
- package/dist/Schema.js +810 -0
- package/dist/Schema.mjs +769 -0
- package/package.json +42 -0
package/dist/Schema.js
ADDED
|
@@ -0,0 +1,810 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/Schema.ts
|
|
29
|
+
var Schema_exports = {};
|
|
30
|
+
__export(Schema_exports, {
|
|
31
|
+
SchemaArray: () => SchemaArray,
|
|
32
|
+
SchemaBoolean: () => SchemaBoolean,
|
|
33
|
+
SchemaDate: () => SchemaDate,
|
|
34
|
+
SchemaEnum: () => SchemaEnum,
|
|
35
|
+
SchemaItem: () => SchemaItem,
|
|
36
|
+
SchemaLiteral: () => SchemaLiteral,
|
|
37
|
+
SchemaNullable: () => SchemaNullable,
|
|
38
|
+
SchemaNumber: () => SchemaNumber,
|
|
39
|
+
SchemaObject: () => SchemaObject,
|
|
40
|
+
SchemaRecord: () => SchemaRecord,
|
|
41
|
+
SchemaString: () => SchemaString,
|
|
42
|
+
SchemaUnion: () => SchemaUnion,
|
|
43
|
+
Types: () => Types,
|
|
44
|
+
default: () => Schema,
|
|
45
|
+
parceable: () => parceable,
|
|
46
|
+
parseForm: () => parseForm,
|
|
47
|
+
parseFormData: () => parseFormData,
|
|
48
|
+
parseQuery: () => parseQuery,
|
|
49
|
+
s: () => s
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(Schema_exports);
|
|
52
|
+
|
|
53
|
+
// src/helpers.ts
|
|
54
|
+
var import_object_util3 = require("@dzeio/object-util");
|
|
55
|
+
|
|
56
|
+
// src/SchemaItem.ts
|
|
57
|
+
var import_object_util = require("@dzeio/object-util");
|
|
58
|
+
var SchemaItem = class _SchemaItem {
|
|
59
|
+
constructor(items) {
|
|
60
|
+
// standard Schema V1 spec
|
|
61
|
+
this["~standard"] = {
|
|
62
|
+
vendor: "aptatio",
|
|
63
|
+
version: 1,
|
|
64
|
+
validate: (value) => {
|
|
65
|
+
const res = this.parse(value);
|
|
66
|
+
if (!res.valid) {
|
|
67
|
+
return {
|
|
68
|
+
issues: res.errors
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
value: res.object,
|
|
73
|
+
issues: res.errors
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* keep public ?
|
|
79
|
+
*/
|
|
80
|
+
this.validations = [];
|
|
81
|
+
/**
|
|
82
|
+
* Function calls saved for serialization
|
|
83
|
+
*/
|
|
84
|
+
this.savedCalls = [];
|
|
85
|
+
/**
|
|
86
|
+
* Pre process the variable for various reasons
|
|
87
|
+
*
|
|
88
|
+
* It will execute every pre-process sequentially in order of being added
|
|
89
|
+
*
|
|
90
|
+
* note: The type of the final Pre-Process MUST be valid
|
|
91
|
+
*/
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
93
|
+
this.preProcess = [];
|
|
94
|
+
/**
|
|
95
|
+
* post process the variable after it being validated
|
|
96
|
+
*
|
|
97
|
+
* it will execute each post-processes sequentially
|
|
98
|
+
*
|
|
99
|
+
* note: the type of the final post-process MUST be valid
|
|
100
|
+
*/
|
|
101
|
+
this.postProcess = [];
|
|
102
|
+
/**
|
|
103
|
+
* list of attributes for custom works
|
|
104
|
+
*/
|
|
105
|
+
this.attributes = [];
|
|
106
|
+
this.invalidError = "the field is invalid";
|
|
107
|
+
if (items && items.length > 0) {
|
|
108
|
+
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
attrs(...attributes) {
|
|
112
|
+
this.attributes.concat(attributes);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
attr(...attributes) {
|
|
116
|
+
return this.attrs(...attributes);
|
|
117
|
+
}
|
|
118
|
+
setInvalidError(err) {
|
|
119
|
+
this.invalidError = err;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
clone() {
|
|
123
|
+
return Schema.fromJSON(this.toJSON());
|
|
124
|
+
}
|
|
125
|
+
parse(input, options) {
|
|
126
|
+
var _a;
|
|
127
|
+
for (const preProcess of this.preProcess) {
|
|
128
|
+
input = preProcess(input);
|
|
129
|
+
}
|
|
130
|
+
if (!this.isOfType(input)) {
|
|
131
|
+
return {
|
|
132
|
+
valid: false,
|
|
133
|
+
errors: [{ message: "invalid type" }]
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const errors = [];
|
|
137
|
+
for (const validation of this.validations) {
|
|
138
|
+
if (!validation.fn(input)) {
|
|
139
|
+
errors.push({
|
|
140
|
+
message: (_a = validation.error) != null ? _a : this.invalidError
|
|
141
|
+
});
|
|
142
|
+
if (options == null ? void 0 : options.fast) {
|
|
143
|
+
return {
|
|
144
|
+
valid: false,
|
|
145
|
+
object: input,
|
|
146
|
+
errors
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (errors.length > 0) {
|
|
152
|
+
return {
|
|
153
|
+
valid: false,
|
|
154
|
+
object: input,
|
|
155
|
+
errors
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
for (const postProcess of this.postProcess) {
|
|
159
|
+
input = postProcess(input);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
valid: true,
|
|
163
|
+
object: input
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
toJSON() {
|
|
167
|
+
var _a;
|
|
168
|
+
const res = {
|
|
169
|
+
i: this.constructor.name,
|
|
170
|
+
a: this.attributes.length > 0 ? this.attributes : void 0,
|
|
171
|
+
c: (_a = this.items) == null ? void 0 : _a.map((it) => it instanceof _SchemaItem ? it.toJSON() : it),
|
|
172
|
+
f: this.savedCalls.map((it) => it.args ? { n: it.name, a: Array.from(it.args) } : { n: it.name })
|
|
173
|
+
};
|
|
174
|
+
(0, import_object_util.objectClean)(res, { deep: false });
|
|
175
|
+
return res;
|
|
176
|
+
}
|
|
177
|
+
addValidation(fn, error) {
|
|
178
|
+
this.validations.push(typeof fn === "function" ? { fn, error } : fn);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
182
|
+
addPreProcess(fn) {
|
|
183
|
+
this.preProcess.push(fn);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
addPostProcess(fn) {
|
|
187
|
+
this.postProcess.push(fn);
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// src/items/array.ts
|
|
193
|
+
var SchemaArray = class extends SchemaItem {
|
|
194
|
+
constructor(values) {
|
|
195
|
+
super([values]);
|
|
196
|
+
this.values = values;
|
|
197
|
+
}
|
|
198
|
+
unique() {
|
|
199
|
+
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
parse(input, options) {
|
|
203
|
+
const { valid, object, errors = [] } = super.parse(input, options);
|
|
204
|
+
if (!valid) {
|
|
205
|
+
return {
|
|
206
|
+
valid,
|
|
207
|
+
object,
|
|
208
|
+
errors
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
const clone = [];
|
|
212
|
+
const errs = [];
|
|
213
|
+
for (let idx = 0; idx < object.length; idx++) {
|
|
214
|
+
const item = object[idx];
|
|
215
|
+
const res = this.values.parse(item);
|
|
216
|
+
if (res.valid) {
|
|
217
|
+
clone.push(res.object);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const errss = res.errors.map((it) => ({
|
|
221
|
+
...it,
|
|
222
|
+
field: it.field ? `${idx}.${it.field}` : idx.toString()
|
|
223
|
+
}));
|
|
224
|
+
if (options == null ? void 0 : options.fast) {
|
|
225
|
+
return {
|
|
226
|
+
valid: false,
|
|
227
|
+
object: clone,
|
|
228
|
+
errors: errss
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
errs.push(...errss);
|
|
232
|
+
}
|
|
233
|
+
if (errs.length > 0) {
|
|
234
|
+
return {
|
|
235
|
+
valid: false,
|
|
236
|
+
object: clone,
|
|
237
|
+
errors: errs
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
valid: true,
|
|
242
|
+
object: clone
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
unwrap() {
|
|
246
|
+
return this.values;
|
|
247
|
+
}
|
|
248
|
+
isOfType(input) {
|
|
249
|
+
return Array.isArray(input);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
__decorateClass([
|
|
253
|
+
parceable()
|
|
254
|
+
], SchemaArray.prototype, "unique", 1);
|
|
255
|
+
|
|
256
|
+
// src/items/boolean.ts
|
|
257
|
+
var SchemaBoolean = class extends SchemaItem {
|
|
258
|
+
parseString(trueValue = "true", falseValue = "false") {
|
|
259
|
+
this.addPreProcess((it) => {
|
|
260
|
+
if (typeof it !== "string") {
|
|
261
|
+
return it;
|
|
262
|
+
}
|
|
263
|
+
if (it === trueValue) {
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
if (it === falseValue) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
return it;
|
|
270
|
+
});
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
isOfType(input) {
|
|
274
|
+
return typeof input === "boolean";
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
__decorateClass([
|
|
278
|
+
parceable()
|
|
279
|
+
], SchemaBoolean.prototype, "parseString", 1);
|
|
280
|
+
|
|
281
|
+
// src/items/nullable.ts
|
|
282
|
+
var SchemaNullable = class extends SchemaItem {
|
|
283
|
+
constructor(child) {
|
|
284
|
+
super([child]);
|
|
285
|
+
this.child = child;
|
|
286
|
+
}
|
|
287
|
+
unwrap() {
|
|
288
|
+
return this.child;
|
|
289
|
+
}
|
|
290
|
+
parse(input, options) {
|
|
291
|
+
if (this.isNull(input)) {
|
|
292
|
+
return {
|
|
293
|
+
valid: true,
|
|
294
|
+
object: void 0
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return this.child.parse(input, options);
|
|
298
|
+
}
|
|
299
|
+
isOfType(input) {
|
|
300
|
+
return Array.isArray(input);
|
|
301
|
+
}
|
|
302
|
+
isNull(value) {
|
|
303
|
+
return typeof value === "undefined" || value === null;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// src/items/object.ts
|
|
308
|
+
var import_object_util2 = require("@dzeio/object-util");
|
|
309
|
+
var SchemaObject = class extends SchemaItem {
|
|
310
|
+
constructor(model) {
|
|
311
|
+
super([model]);
|
|
312
|
+
this.model = model;
|
|
313
|
+
this.id = "object";
|
|
314
|
+
}
|
|
315
|
+
parse(input, options) {
|
|
316
|
+
const { valid, object, errors = [] } = super.parse(input, options);
|
|
317
|
+
if (!valid) {
|
|
318
|
+
return {
|
|
319
|
+
valid,
|
|
320
|
+
object,
|
|
321
|
+
errors
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
const clone = (0, import_object_util2.objectClone)(object);
|
|
325
|
+
(0, import_object_util2.objectLoop)(this.model, (childSchema, key) => {
|
|
326
|
+
const childValue = clone[key];
|
|
327
|
+
const child = childSchema.parse(childValue);
|
|
328
|
+
if (!child.valid) {
|
|
329
|
+
errors.push(...child.errors.map((it) => ({
|
|
330
|
+
...it,
|
|
331
|
+
field: it.field ? `${key}.${it.field}` : key
|
|
332
|
+
})));
|
|
333
|
+
}
|
|
334
|
+
clone[key] = child.object;
|
|
335
|
+
return child.valid || !(options == null ? void 0 : options.fast);
|
|
336
|
+
});
|
|
337
|
+
return {
|
|
338
|
+
valid: errors.length === 0,
|
|
339
|
+
errors,
|
|
340
|
+
object: clone
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
isOfType(input) {
|
|
344
|
+
return (0, import_object_util2.isObject)(input);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// src/helpers.ts
|
|
349
|
+
function parseQuery(model, query, opts) {
|
|
350
|
+
const record = {};
|
|
351
|
+
for (const [key, value] of query) {
|
|
352
|
+
record[key] = value;
|
|
353
|
+
}
|
|
354
|
+
return model.parse(record, opts);
|
|
355
|
+
}
|
|
356
|
+
function parseFormData(model, data, opts) {
|
|
357
|
+
var _a;
|
|
358
|
+
const record = {};
|
|
359
|
+
for (const [key, value] of data) {
|
|
360
|
+
const isArray = (_a = model.model[key].isOfType([])) != null ? _a : false;
|
|
361
|
+
(0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), isArray ? data.getAll(key) : value);
|
|
362
|
+
}
|
|
363
|
+
const handleBoolean = (value, keys) => {
|
|
364
|
+
var _a2;
|
|
365
|
+
if (value instanceof SchemaNullable) {
|
|
366
|
+
handleBoolean(value.unwrap(), keys);
|
|
367
|
+
}
|
|
368
|
+
if (value instanceof SchemaArray) {
|
|
369
|
+
const elements = (0, import_object_util3.objectGet)(record, keys);
|
|
370
|
+
for (let it = 0; it < ((_a2 = elements == null ? void 0 : elements.length) != null ? _a2 : 0); it++) {
|
|
371
|
+
handleBoolean(value.unwrap(), [...keys, it]);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (value instanceof SchemaObject) {
|
|
375
|
+
handleSchemaForBoolean(value.model, keys);
|
|
376
|
+
}
|
|
377
|
+
if (value instanceof SchemaBoolean) {
|
|
378
|
+
(0, import_object_util3.objectSet)(record, keys, !!data.get(keys.join(".")));
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const handleSchemaForBoolean = (modl, keys = []) => {
|
|
382
|
+
(0, import_object_util3.objectLoop)(modl, (value, key) => {
|
|
383
|
+
handleBoolean(value, [...keys, key]);
|
|
384
|
+
});
|
|
385
|
+
};
|
|
386
|
+
handleSchemaForBoolean(model.model);
|
|
387
|
+
return model.parse(record, opts);
|
|
388
|
+
}
|
|
389
|
+
function parseForm(model, form, opts) {
|
|
390
|
+
return parseFormData(model, new FormData(form), opts);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/items/date.ts
|
|
394
|
+
var SchemaDate = class extends SchemaItem {
|
|
395
|
+
isOfType(input) {
|
|
396
|
+
return input instanceof Date;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
// src/items/record.ts
|
|
401
|
+
var import_object_util4 = require("@dzeio/object-util");
|
|
402
|
+
var SchemaRecord = class extends SchemaItem {
|
|
403
|
+
constructor(keys, values) {
|
|
404
|
+
super([keys, values]);
|
|
405
|
+
this.keys = keys;
|
|
406
|
+
this.values = values;
|
|
407
|
+
}
|
|
408
|
+
parse(input, options) {
|
|
409
|
+
const { valid, object, errors = [] } = super.parse(input, options);
|
|
410
|
+
if (!valid) {
|
|
411
|
+
return {
|
|
412
|
+
valid,
|
|
413
|
+
object,
|
|
414
|
+
errors
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
const clone = {};
|
|
418
|
+
(0, import_object_util4.objectLoop)(object, (value, key) => {
|
|
419
|
+
var _a, _b;
|
|
420
|
+
const res1 = this.keys.parse(key);
|
|
421
|
+
const res2 = this.values.parse(value);
|
|
422
|
+
if (!res1.valid || !res2.valid) {
|
|
423
|
+
errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
424
|
+
message: it.message,
|
|
425
|
+
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
426
|
+
})));
|
|
427
|
+
} else {
|
|
428
|
+
clone[res1.object] = res2.object;
|
|
429
|
+
}
|
|
430
|
+
return errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
431
|
+
});
|
|
432
|
+
return {
|
|
433
|
+
valid: errors.length === 0,
|
|
434
|
+
errors,
|
|
435
|
+
object: clone
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
isOfType(input) {
|
|
439
|
+
return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// src/items/enum.ts
|
|
444
|
+
var SchemaEnum = class extends SchemaItem {
|
|
445
|
+
constructor(templateEnum) {
|
|
446
|
+
super();
|
|
447
|
+
this.templateEnum = templateEnum;
|
|
448
|
+
this.type = "string";
|
|
449
|
+
const firstValue = Object.values(templateEnum)[0];
|
|
450
|
+
if (typeof firstValue === "number" || typeof templateEnum[firstValue] === "number") {
|
|
451
|
+
this.type = "number";
|
|
452
|
+
}
|
|
453
|
+
this.validations.push({
|
|
454
|
+
fn: (input) => Object.values(this.templateEnum).includes(input),
|
|
455
|
+
error: `Input is not part of ${templateEnum.constructor.name}`
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
isOfType(input) {
|
|
459
|
+
return typeof input === this.type;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// src/items/literal.ts
|
|
464
|
+
var SchemaLiteral = class extends SchemaItem {
|
|
465
|
+
constructor(value) {
|
|
466
|
+
super([value]);
|
|
467
|
+
this.value = value;
|
|
468
|
+
this.validations.push({ fn: (it) => it === this.value });
|
|
469
|
+
}
|
|
470
|
+
isOfType(input) {
|
|
471
|
+
return typeof input === typeof this.value;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
// src/items/number.ts
|
|
476
|
+
var SchemaNumber = class extends SchemaItem {
|
|
477
|
+
lte(value, message) {
|
|
478
|
+
this.addValidation({
|
|
479
|
+
fn(input) {
|
|
480
|
+
return input <= value;
|
|
481
|
+
},
|
|
482
|
+
error: message
|
|
483
|
+
});
|
|
484
|
+
return this;
|
|
485
|
+
}
|
|
486
|
+
gte(value, message) {
|
|
487
|
+
this.addValidation({
|
|
488
|
+
fn(input) {
|
|
489
|
+
return input >= value;
|
|
490
|
+
},
|
|
491
|
+
error: message
|
|
492
|
+
});
|
|
493
|
+
return this;
|
|
494
|
+
}
|
|
495
|
+
lt(value, message) {
|
|
496
|
+
this.addValidation({
|
|
497
|
+
fn(input) {
|
|
498
|
+
return input < value;
|
|
499
|
+
},
|
|
500
|
+
error: message
|
|
501
|
+
});
|
|
502
|
+
return this;
|
|
503
|
+
}
|
|
504
|
+
gt(value, message) {
|
|
505
|
+
this.addValidation({
|
|
506
|
+
fn(input) {
|
|
507
|
+
return input > value;
|
|
508
|
+
},
|
|
509
|
+
error: message
|
|
510
|
+
});
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
parseString() {
|
|
514
|
+
this.addPreProcess(
|
|
515
|
+
(input) => typeof input === "string" ? Number.parseFloat(input) : input
|
|
516
|
+
);
|
|
517
|
+
return this;
|
|
518
|
+
}
|
|
519
|
+
isOfType(input) {
|
|
520
|
+
return typeof input === "number" && !Number.isNaN(input);
|
|
521
|
+
}
|
|
522
|
+
min(...params) {
|
|
523
|
+
return this.gte(...params);
|
|
524
|
+
}
|
|
525
|
+
max(...params) {
|
|
526
|
+
return this.lte(...params);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
__decorateClass([
|
|
530
|
+
parceable()
|
|
531
|
+
], SchemaNumber.prototype, "lte", 1);
|
|
532
|
+
__decorateClass([
|
|
533
|
+
parceable()
|
|
534
|
+
], SchemaNumber.prototype, "gte", 1);
|
|
535
|
+
__decorateClass([
|
|
536
|
+
parceable()
|
|
537
|
+
], SchemaNumber.prototype, "lt", 1);
|
|
538
|
+
__decorateClass([
|
|
539
|
+
parceable()
|
|
540
|
+
], SchemaNumber.prototype, "gt", 1);
|
|
541
|
+
__decorateClass([
|
|
542
|
+
parceable()
|
|
543
|
+
], SchemaNumber.prototype, "parseString", 1);
|
|
544
|
+
|
|
545
|
+
// src/items/string.ts
|
|
546
|
+
var SchemaString = class extends SchemaItem {
|
|
547
|
+
min(value, message) {
|
|
548
|
+
this.addValidation({
|
|
549
|
+
fn(input) {
|
|
550
|
+
return input.length >= value;
|
|
551
|
+
},
|
|
552
|
+
error: message
|
|
553
|
+
});
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
toCasing(casing) {
|
|
557
|
+
const fn = casing === "lower" ? "toLowerCase" : "toUpperCase";
|
|
558
|
+
this.addPostProcess((it) => it[fn]());
|
|
559
|
+
return this;
|
|
560
|
+
}
|
|
561
|
+
max(value, message) {
|
|
562
|
+
this.addValidation({
|
|
563
|
+
fn(input) {
|
|
564
|
+
return input.length <= value;
|
|
565
|
+
},
|
|
566
|
+
error: message
|
|
567
|
+
});
|
|
568
|
+
return this;
|
|
569
|
+
}
|
|
570
|
+
notEmpty(message) {
|
|
571
|
+
this.addValidation({
|
|
572
|
+
fn(input) {
|
|
573
|
+
return input !== "";
|
|
574
|
+
},
|
|
575
|
+
error: message
|
|
576
|
+
});
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
579
|
+
regex(regex, message) {
|
|
580
|
+
this.addValidation({
|
|
581
|
+
fn(input) {
|
|
582
|
+
return regex.test(input);
|
|
583
|
+
},
|
|
584
|
+
error: message
|
|
585
|
+
});
|
|
586
|
+
return this;
|
|
587
|
+
}
|
|
588
|
+
minLength(value, message) {
|
|
589
|
+
return this.min(value, message);
|
|
590
|
+
}
|
|
591
|
+
maxLength(value, message) {
|
|
592
|
+
return this.max(value, message);
|
|
593
|
+
}
|
|
594
|
+
isOfType(input) {
|
|
595
|
+
return typeof input === "string";
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
__decorateClass([
|
|
599
|
+
parceable()
|
|
600
|
+
], SchemaString.prototype, "min", 1);
|
|
601
|
+
__decorateClass([
|
|
602
|
+
parceable()
|
|
603
|
+
], SchemaString.prototype, "toCasing", 1);
|
|
604
|
+
__decorateClass([
|
|
605
|
+
parceable()
|
|
606
|
+
], SchemaString.prototype, "max", 1);
|
|
607
|
+
__decorateClass([
|
|
608
|
+
parceable()
|
|
609
|
+
], SchemaString.prototype, "notEmpty", 1);
|
|
610
|
+
__decorateClass([
|
|
611
|
+
parceable()
|
|
612
|
+
], SchemaString.prototype, "regex", 1);
|
|
613
|
+
|
|
614
|
+
// src/items/union.ts
|
|
615
|
+
var SchemaUnion = class extends SchemaItem {
|
|
616
|
+
constructor(...schemas) {
|
|
617
|
+
super();
|
|
618
|
+
this.schemas = schemas;
|
|
619
|
+
}
|
|
620
|
+
parse(input, options) {
|
|
621
|
+
const { valid, object, errors = [] } = super.parse(input, options);
|
|
622
|
+
if (!valid) {
|
|
623
|
+
return {
|
|
624
|
+
valid,
|
|
625
|
+
object,
|
|
626
|
+
errors
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
for (const schema of this.schemas) {
|
|
630
|
+
const validation = schema.parse(input, options);
|
|
631
|
+
if (validation.valid) {
|
|
632
|
+
return validation;
|
|
633
|
+
} else {
|
|
634
|
+
errors.push(...validation.errors);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return {
|
|
638
|
+
valid: false,
|
|
639
|
+
errors,
|
|
640
|
+
object
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
isOfType(input) {
|
|
644
|
+
return this.schemas.some((it) => it.isOfType(input));
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
// src/Schema.ts
|
|
649
|
+
function parceable() {
|
|
650
|
+
return (target, propertyKey, descriptor) => {
|
|
651
|
+
if (!(target instanceof SchemaItem)) {
|
|
652
|
+
throw new Error("the decorator is only usable on Schema");
|
|
653
|
+
}
|
|
654
|
+
if (!(propertyKey in target)) {
|
|
655
|
+
throw new Error("property not set in object");
|
|
656
|
+
}
|
|
657
|
+
const original = target[propertyKey];
|
|
658
|
+
descriptor.value = function(...args) {
|
|
659
|
+
this.savedCalls.push({ name: propertyKey, args });
|
|
660
|
+
const res = original.call(this, ...args);
|
|
661
|
+
return res;
|
|
662
|
+
};
|
|
663
|
+
return descriptor;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
var Types = {
|
|
667
|
+
Array: SchemaArray,
|
|
668
|
+
Boolean: SchemaBoolean,
|
|
669
|
+
Date: SchemaDate,
|
|
670
|
+
Enum: SchemaEnum,
|
|
671
|
+
Literal: SchemaLiteral,
|
|
672
|
+
Nullable: SchemaNullable,
|
|
673
|
+
Object: SchemaObject,
|
|
674
|
+
Record: SchemaRecord,
|
|
675
|
+
String: SchemaString,
|
|
676
|
+
Union: SchemaUnion
|
|
677
|
+
};
|
|
678
|
+
var _Schema = class _Schema extends SchemaObject {
|
|
679
|
+
static register(module2) {
|
|
680
|
+
this.registeredModules.push(module2);
|
|
681
|
+
}
|
|
682
|
+
static getModule(name) {
|
|
683
|
+
return this.registeredModules.find((it) => it.name === name);
|
|
684
|
+
}
|
|
685
|
+
static array(...inputs) {
|
|
686
|
+
return new SchemaArray(...inputs);
|
|
687
|
+
}
|
|
688
|
+
static boolean(...inputs) {
|
|
689
|
+
return new SchemaBoolean(...inputs);
|
|
690
|
+
}
|
|
691
|
+
static date(...inputs) {
|
|
692
|
+
return new SchemaDate(...inputs);
|
|
693
|
+
}
|
|
694
|
+
static enum(...inputs) {
|
|
695
|
+
return new SchemaEnum(...inputs);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
*
|
|
699
|
+
* @param input the literal value (note: append `as const` else the typing won't work correctly)
|
|
700
|
+
* @returns
|
|
701
|
+
*/
|
|
702
|
+
static literal(input) {
|
|
703
|
+
return new SchemaLiteral(input);
|
|
704
|
+
}
|
|
705
|
+
static nullable(...inputs) {
|
|
706
|
+
return new SchemaNullable(...inputs);
|
|
707
|
+
}
|
|
708
|
+
static number(...inputs) {
|
|
709
|
+
return new SchemaNumber(...inputs);
|
|
710
|
+
}
|
|
711
|
+
static object(...inputs) {
|
|
712
|
+
return new SchemaObject(...inputs);
|
|
713
|
+
}
|
|
714
|
+
static record(...inputs) {
|
|
715
|
+
return new SchemaRecord(...inputs);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* See {@link SchemaString}
|
|
719
|
+
*/
|
|
720
|
+
static string(...inputs) {
|
|
721
|
+
return new SchemaString(...inputs);
|
|
722
|
+
}
|
|
723
|
+
static union(...inputs) {
|
|
724
|
+
return new SchemaUnion(...inputs);
|
|
725
|
+
}
|
|
726
|
+
static fromJSON(json) {
|
|
727
|
+
var _a, _b, _c, _d, _e, _f;
|
|
728
|
+
const fn = this.getModule(json.i);
|
|
729
|
+
if (!fn) {
|
|
730
|
+
throw new Error(`Schema cannot parse ${json.i}`);
|
|
731
|
+
}
|
|
732
|
+
const item = new fn(...(_b = (_a = json.c) == null ? void 0 : _a.map((it) => this.isSchemaJSON(it) ? _Schema.fromJSON(it) : it)) != null ? _b : []);
|
|
733
|
+
for (const validation of (_c = json.f) != null ? _c : []) {
|
|
734
|
+
if (!(validation.n in item)) {
|
|
735
|
+
throw new Error("validation not available in Schema Item");
|
|
736
|
+
}
|
|
737
|
+
item[validation.n](...(_e = (_d = validation.a) == null ? void 0 : _d.map((it) => _Schema.isSchemaJSON(it) ? _Schema.fromJSON(it) : it)) != null ? _e : []);
|
|
738
|
+
}
|
|
739
|
+
item.attrs(...(_f = json.a) != null ? _f : []);
|
|
740
|
+
return item;
|
|
741
|
+
}
|
|
742
|
+
static isSchemaJSON(data) {
|
|
743
|
+
if (typeof data !== "object" || data === null) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
if (!("i" in data)) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
return true;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* @deprecated use helper `parseQuery`
|
|
753
|
+
*/
|
|
754
|
+
validateQuery(query, fast = false) {
|
|
755
|
+
return parseQuery(this, query, { fast });
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* @deprecated use `parse`
|
|
759
|
+
*/
|
|
760
|
+
validate(input, fast = false) {
|
|
761
|
+
return this.parse(input, { fast });
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* @deprecated use helper `parseForm`
|
|
765
|
+
*/
|
|
766
|
+
validateForm(form, fast = false) {
|
|
767
|
+
return parseForm(this, form, { fast });
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* @deprecated use helper `parseFormData`
|
|
771
|
+
*/
|
|
772
|
+
validateFormData(data, fast = false) {
|
|
773
|
+
return parseFormData(this, data, { fast });
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
_Schema.registeredModules = [
|
|
777
|
+
SchemaArray,
|
|
778
|
+
SchemaBoolean,
|
|
779
|
+
SchemaDate,
|
|
780
|
+
SchemaEnum,
|
|
781
|
+
SchemaLiteral,
|
|
782
|
+
SchemaNullable,
|
|
783
|
+
SchemaObject,
|
|
784
|
+
SchemaRecord,
|
|
785
|
+
SchemaString,
|
|
786
|
+
SchemaUnion
|
|
787
|
+
];
|
|
788
|
+
var Schema = _Schema;
|
|
789
|
+
var s = Schema;
|
|
790
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
791
|
+
0 && (module.exports = {
|
|
792
|
+
SchemaArray,
|
|
793
|
+
SchemaBoolean,
|
|
794
|
+
SchemaDate,
|
|
795
|
+
SchemaEnum,
|
|
796
|
+
SchemaItem,
|
|
797
|
+
SchemaLiteral,
|
|
798
|
+
SchemaNullable,
|
|
799
|
+
SchemaNumber,
|
|
800
|
+
SchemaObject,
|
|
801
|
+
SchemaRecord,
|
|
802
|
+
SchemaString,
|
|
803
|
+
SchemaUnion,
|
|
804
|
+
Types,
|
|
805
|
+
parceable,
|
|
806
|
+
parseForm,
|
|
807
|
+
parseFormData,
|
|
808
|
+
parseQuery,
|
|
809
|
+
s
|
|
810
|
+
});
|