@orion-js/schema 4.0.0-next.3 → 4.0.0-next.4
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/index.cjs +138 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +116 -55
- package/dist/index.d.ts +116 -55
- package/dist/index.js +131 -68
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -34,9 +34,16 @@ __export(index_exports, {
|
|
|
34
34
|
cleanKey: () => cleanKey_default,
|
|
35
35
|
createEnum: () => createEnum,
|
|
36
36
|
dotGetSchema: () => dotGetSchema_default2,
|
|
37
|
+
fieldTypes: () => fieldTypes_default,
|
|
37
38
|
getFieldType: () => getFieldType,
|
|
39
|
+
getSchemaFromAnyOrionForm: () => getSchemaFromAnyOrionForm,
|
|
40
|
+
getSchemaModelName: () => getSchemaModelName,
|
|
41
|
+
getSchemaWithMetadataFromAnyOrionForm: () => getSchemaWithMetadataFromAnyOrionForm,
|
|
38
42
|
getValidationErrors: () => getValidationErrors2,
|
|
43
|
+
isSchemaLike: () => isSchemaLike,
|
|
44
|
+
isSchemaOrFieldLike: () => isSchemaOrFieldLike,
|
|
39
45
|
isValid: () => isValid,
|
|
46
|
+
schemaWithName: () => schemaWithName,
|
|
40
47
|
validate: () => validate,
|
|
41
48
|
validateKey: () => validateKey_default
|
|
42
49
|
});
|
|
@@ -85,36 +92,13 @@ var ValidationError = class _ValidationError extends Error {
|
|
|
85
92
|
};
|
|
86
93
|
};
|
|
87
94
|
|
|
88
|
-
// src/
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (obj.prototype === void 0) {
|
|
92
|
-
return isCtorClass;
|
|
93
|
-
}
|
|
94
|
-
const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString && obj.prototype.constructor.toString().substring(0, 5) === "class";
|
|
95
|
-
return isCtorClass || isPrototypeCtorClass;
|
|
96
|
-
}
|
|
97
|
-
var convertOnParam = (info, paramName) => {
|
|
98
|
-
if (!info[paramName]) return;
|
|
99
|
-
const type = info[paramName].type;
|
|
100
|
-
if (!type) return;
|
|
101
|
-
if (typeof type !== "function") return;
|
|
102
|
-
if (!isClass(type)) return;
|
|
103
|
-
if (!type.getModel || !type.__schemaId) return;
|
|
104
|
-
info[paramName].type = type.getModel().getCleanSchema();
|
|
105
|
-
};
|
|
106
|
-
var convertTypedModel = (info) => {
|
|
107
|
-
convertOnParam(info, "schema");
|
|
108
|
-
convertOnParam(info, "currentSchema");
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
// src/getSchemaFromTypedModel.ts
|
|
112
|
-
var getSchemaFromTypedModel = (schema) => {
|
|
95
|
+
// src/getSchemaFromTypedSchema.ts
|
|
96
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
97
|
+
var getSchemaFromTypedSchema = (schema) => {
|
|
113
98
|
const item = schema;
|
|
114
|
-
if (
|
|
115
|
-
if (!
|
|
116
|
-
|
|
117
|
-
return item.getModel().getCleanSchema();
|
|
99
|
+
if (!schema[Symbol.metadata]) return item;
|
|
100
|
+
if (!schema[Symbol.metadata]._isTypedSchema) return item;
|
|
101
|
+
return schema[Symbol.metadata]._getModel().getSchema();
|
|
118
102
|
};
|
|
119
103
|
|
|
120
104
|
// src/getValidationErrors/getError/index.ts
|
|
@@ -148,7 +132,8 @@ function fieldType(opts) {
|
|
|
148
132
|
name,
|
|
149
133
|
validate: overwrittenValidate,
|
|
150
134
|
clean: overwrittenClean,
|
|
151
|
-
|
|
135
|
+
__isFieldType: true,
|
|
136
|
+
__tsFieldType: null
|
|
152
137
|
};
|
|
153
138
|
}
|
|
154
139
|
|
|
@@ -223,12 +208,12 @@ var string_default = fieldType({
|
|
|
223
208
|
name: "string",
|
|
224
209
|
validate(value, { currentSchema }) {
|
|
225
210
|
if (!(0, import_isString.default)(value)) return Errors_default.NOT_A_STRING;
|
|
226
|
-
if (isFinite(currentSchema.min)) {
|
|
211
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
227
212
|
if (value.length < currentSchema.min) {
|
|
228
213
|
return Errors_default.STRING_TOO_SHORT;
|
|
229
214
|
}
|
|
230
215
|
}
|
|
231
|
-
if (isFinite(currentSchema.max)) {
|
|
216
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
232
217
|
if (value.length > currentSchema.max) {
|
|
233
218
|
return Errors_default.STRING_TOO_LONG;
|
|
234
219
|
}
|
|
@@ -269,13 +254,13 @@ var date_default = fieldType({
|
|
|
269
254
|
if (options.autoConvert) {
|
|
270
255
|
if ((0, import_isString2.default)(value)) {
|
|
271
256
|
const result = new Date(value);
|
|
272
|
-
if (isNaN(result.getTime())) {
|
|
257
|
+
if (Number.isNaN(result.getTime())) {
|
|
273
258
|
return value;
|
|
274
259
|
}
|
|
275
260
|
value = result;
|
|
276
261
|
} else if ((0, import_isNumber.default)(value)) {
|
|
277
262
|
const result = new Date(value);
|
|
278
|
-
if (isNaN(result.getTime())) {
|
|
263
|
+
if (Number.isNaN(result.getTime())) {
|
|
279
264
|
return value;
|
|
280
265
|
}
|
|
281
266
|
value = result;
|
|
@@ -289,18 +274,17 @@ var date_default = fieldType({
|
|
|
289
274
|
var import_isInteger = __toESM(require("lodash/isInteger"), 1);
|
|
290
275
|
|
|
291
276
|
// src/fieldTypes/number.ts
|
|
292
|
-
var import_isFinite = __toESM(require("lodash/isFinite"), 1);
|
|
293
277
|
var import_toNumber = __toESM(require("lodash/toNumber"), 1);
|
|
294
278
|
var number_default = fieldType({
|
|
295
279
|
name: "number",
|
|
296
280
|
validate(value, { currentSchema }) {
|
|
297
|
-
if (!
|
|
298
|
-
if (
|
|
281
|
+
if (!Number.isFinite(value)) return Errors_default.NOT_A_NUMBER;
|
|
282
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
299
283
|
if (value < currentSchema.min) {
|
|
300
284
|
return Errors_default.NUMBER_TOO_SMALL;
|
|
301
285
|
}
|
|
302
286
|
}
|
|
303
|
-
if (
|
|
287
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
304
288
|
if (value > currentSchema.max) {
|
|
305
289
|
return Errors_default.NUMBER_TOO_BIG;
|
|
306
290
|
}
|
|
@@ -355,10 +339,11 @@ var boolean_default = fieldType({
|
|
|
355
339
|
clean(value, { options }) {
|
|
356
340
|
if (options.autoConvert) {
|
|
357
341
|
if (typeof value === "string") {
|
|
358
|
-
|
|
342
|
+
const stringValue = value;
|
|
343
|
+
if (stringValue === "true") {
|
|
359
344
|
value = true;
|
|
360
345
|
}
|
|
361
|
-
if (
|
|
346
|
+
if (stringValue === "false") {
|
|
362
347
|
value = false;
|
|
363
348
|
}
|
|
364
349
|
}
|
|
@@ -407,7 +392,7 @@ var blackbox_default = fieldType({
|
|
|
407
392
|
// src/fieldTypes/any.ts
|
|
408
393
|
var any_default = fieldType({
|
|
409
394
|
name: "any",
|
|
410
|
-
validate(
|
|
395
|
+
validate() {
|
|
411
396
|
}
|
|
412
397
|
});
|
|
413
398
|
|
|
@@ -428,19 +413,19 @@ var fieldTypes_default = {
|
|
|
428
413
|
|
|
429
414
|
// src/getValidationErrors/getError/getFieldValidator.ts
|
|
430
415
|
var import_has = __toESM(require("lodash/has"), 1);
|
|
431
|
-
function
|
|
416
|
+
function getFieldValidator(type) {
|
|
432
417
|
if ((0, import_isPlainObject4.default)(type)) {
|
|
433
|
-
if (type.
|
|
418
|
+
if (type.__isFieldType) return "custom";
|
|
434
419
|
return "plainObject";
|
|
435
420
|
}
|
|
436
421
|
if ((0, import_isArray3.default)(type)) return "array";
|
|
437
422
|
if (type === String) return "string";
|
|
438
|
-
if (type === Date) return "date";
|
|
423
|
+
if (typeof type === "function" && type.name === "Date") return "date";
|
|
439
424
|
if (type === Number) return "number";
|
|
440
425
|
if (type === Boolean) return "boolean";
|
|
441
426
|
if (type === "enum") return "string";
|
|
442
427
|
if (!(0, import_isString5.default)(type)) {
|
|
443
|
-
throw new Error(
|
|
428
|
+
throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${type}`);
|
|
444
429
|
}
|
|
445
430
|
const exists = (0, import_has.default)(fieldTypes_default, type);
|
|
446
431
|
if (!exists) {
|
|
@@ -458,7 +443,7 @@ async function getValidationErrors(params) {
|
|
|
458
443
|
return Errors_default.REQUIRED;
|
|
459
444
|
}
|
|
460
445
|
} else {
|
|
461
|
-
const validatorKey =
|
|
446
|
+
const validatorKey = getFieldValidator(currentSchema.type);
|
|
462
447
|
const validator = validatorKey === "custom" ? currentSchema.type : fieldTypes_default[validatorKey];
|
|
463
448
|
const error = await validator.validate(value, info, ...args);
|
|
464
449
|
if (error) {
|
|
@@ -488,8 +473,22 @@ var import_isArray4 = __toESM(require("lodash/isArray"), 1);
|
|
|
488
473
|
var import_clone = __toESM(require("lodash/clone"), 1);
|
|
489
474
|
var import_isNil3 = __toESM(require("lodash/isNil"), 1);
|
|
490
475
|
var import_difference2 = __toESM(require("lodash/difference"), 1);
|
|
476
|
+
|
|
477
|
+
// src/getValidationErrors/convertTypedSchema.ts
|
|
478
|
+
var convertOnParam = (info, paramName) => {
|
|
479
|
+
if (!info[paramName]) return;
|
|
480
|
+
const type = info[paramName].type;
|
|
481
|
+
if (!type) return;
|
|
482
|
+
info[paramName].type = getSchemaFromTypedSchema(type);
|
|
483
|
+
};
|
|
484
|
+
var convertTypedSchema = (info) => {
|
|
485
|
+
convertOnParam(info, "schema");
|
|
486
|
+
convertOnParam(info, "currentSchema");
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
// src/getValidationErrors/doValidation.ts
|
|
491
490
|
async function doValidation(params) {
|
|
492
|
-
|
|
491
|
+
convertTypedSchema(params);
|
|
493
492
|
const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
|
|
494
493
|
const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
|
|
495
494
|
const error = await getValidationErrors(info);
|
|
@@ -501,7 +500,7 @@ async function doValidation(params) {
|
|
|
501
500
|
if ((0, import_isPlainObject5.default)(currentSchema.type)) {
|
|
502
501
|
const type = currentSchema.type;
|
|
503
502
|
if (type) {
|
|
504
|
-
if (type.
|
|
503
|
+
if (type.__isFieldType) {
|
|
505
504
|
return;
|
|
506
505
|
}
|
|
507
506
|
if (typeof type.__skipChildValidation === "function") {
|
|
@@ -565,10 +564,10 @@ var defaultOptions = {
|
|
|
565
564
|
omitRequired: false
|
|
566
565
|
};
|
|
567
566
|
async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
|
|
568
|
-
schema =
|
|
567
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
569
568
|
const options = { ...defaultOptions, ...passedOptions };
|
|
570
569
|
const errors = [];
|
|
571
|
-
const addError =
|
|
570
|
+
const addError = (keys, code) => {
|
|
572
571
|
errors.push({
|
|
573
572
|
key: keys.join("."),
|
|
574
573
|
code
|
|
@@ -603,7 +602,7 @@ async function isValid(schema, doc, passedOptions = {}, ...args) {
|
|
|
603
602
|
|
|
604
603
|
// src/getValidationErrors/getError/getFieldType.ts
|
|
605
604
|
function getFieldType(type) {
|
|
606
|
-
const validatorKey =
|
|
605
|
+
const validatorKey = getFieldValidator(type);
|
|
607
606
|
const validator = validatorKey === "custom" ? type : fieldTypes_default[validatorKey];
|
|
608
607
|
return validator;
|
|
609
608
|
}
|
|
@@ -650,11 +649,6 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
650
649
|
value = defaultValue;
|
|
651
650
|
}
|
|
652
651
|
}
|
|
653
|
-
const { autoValue } = fieldSchema;
|
|
654
|
-
if (autoValue) {
|
|
655
|
-
needReClean = true;
|
|
656
|
-
value = await autoValue(value, info, ...args);
|
|
657
|
-
}
|
|
658
652
|
const { clean: clean3 } = fieldSchema;
|
|
659
653
|
if (clean3) {
|
|
660
654
|
needReClean = true;
|
|
@@ -668,11 +662,11 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
668
662
|
|
|
669
663
|
// src/clean/recursiveClean.ts
|
|
670
664
|
var import_isNil5 = __toESM(require("lodash/isNil"), 1);
|
|
671
|
-
var cleanObjectFields = async
|
|
665
|
+
var cleanObjectFields = async ({
|
|
672
666
|
schema,
|
|
673
667
|
value,
|
|
674
668
|
...other
|
|
675
|
-
}) {
|
|
669
|
+
}) => {
|
|
676
670
|
const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
|
|
677
671
|
const newDoc = {};
|
|
678
672
|
for (const key of keys) {
|
|
@@ -693,13 +687,13 @@ var cleanObjectFields = async function({
|
|
|
693
687
|
}
|
|
694
688
|
return newDoc;
|
|
695
689
|
};
|
|
696
|
-
var cleanArrayItems = async
|
|
690
|
+
var cleanArrayItems = async ({
|
|
697
691
|
schema,
|
|
698
692
|
value,
|
|
699
693
|
...other
|
|
700
|
-
}) {
|
|
694
|
+
}) => {
|
|
701
695
|
const schemaType = schema.type[0];
|
|
702
|
-
const promises = value.map(async (item
|
|
696
|
+
const promises = value.map(async (item) => {
|
|
703
697
|
const newValue = await clean({
|
|
704
698
|
...other,
|
|
705
699
|
schema: {
|
|
@@ -720,9 +714,9 @@ function getArrayNode(schema, value) {
|
|
|
720
714
|
}
|
|
721
715
|
return null;
|
|
722
716
|
}
|
|
723
|
-
var clean = async
|
|
724
|
-
|
|
725
|
-
|
|
717
|
+
var clean = async (info) => {
|
|
718
|
+
convertTypedSchema(info);
|
|
719
|
+
const { schema, args = [], value } = info;
|
|
726
720
|
const currSchema = schema.type === void 0 ? { type: schema } : schema;
|
|
727
721
|
const objectSchema = getObjectNode(currSchema, value);
|
|
728
722
|
if (objectSchema) {
|
|
@@ -762,7 +756,7 @@ var defaultOptions2 = {
|
|
|
762
756
|
};
|
|
763
757
|
async function clean2(schema, doc, opts = {}, ...args) {
|
|
764
758
|
if (!doc) return doc;
|
|
765
|
-
schema =
|
|
759
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
766
760
|
const options = { ...defaultOptions2, ...opts };
|
|
767
761
|
const params = {
|
|
768
762
|
schema: { type: schema },
|
|
@@ -787,7 +781,8 @@ var dotGet = function dotGet2(object, path) {
|
|
|
787
781
|
const levelObject = object.type;
|
|
788
782
|
if (first === "$" || /^[0-9]+$/.test(first)) {
|
|
789
783
|
return dotGet2({ type: levelObject[0] }, remainingPath);
|
|
790
|
-
}
|
|
784
|
+
}
|
|
785
|
+
if ((0, import_isPlainObject7.default)(levelObject[first])) {
|
|
791
786
|
return dotGet2(levelObject[first], remainingPath);
|
|
792
787
|
}
|
|
793
788
|
if (levelObject === "blackbox") {
|
|
@@ -833,9 +828,8 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
|
|
|
833
828
|
if (!keySchema) {
|
|
834
829
|
if (options.filter) {
|
|
835
830
|
return Errors_default.NOT_IN_SCHEMA;
|
|
836
|
-
} else {
|
|
837
|
-
return null;
|
|
838
831
|
}
|
|
832
|
+
return null;
|
|
839
833
|
}
|
|
840
834
|
if (keySchema.isBlackboxChild) {
|
|
841
835
|
return null;
|
|
@@ -897,6 +891,75 @@ function createEnum(name, values) {
|
|
|
897
891
|
})
|
|
898
892
|
};
|
|
899
893
|
}
|
|
894
|
+
|
|
895
|
+
// src/models.ts
|
|
896
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
897
|
+
function isSchemaLike(type) {
|
|
898
|
+
var _a;
|
|
899
|
+
if (!type) return false;
|
|
900
|
+
if (objectHasSubObjectWithKey(type, "type")) return true;
|
|
901
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
|
|
902
|
+
if (type.getModel) return true;
|
|
903
|
+
if (type.getSchema) return true;
|
|
904
|
+
if (type.getCleanSchema) return true;
|
|
905
|
+
if (type.__isModel) return true;
|
|
906
|
+
if (type.__modelName) return true;
|
|
907
|
+
return false;
|
|
908
|
+
}
|
|
909
|
+
function isSchemaOrFieldLike(type) {
|
|
910
|
+
if (Array.isArray(type)) {
|
|
911
|
+
if (type.length !== 1) return false;
|
|
912
|
+
return isSchemaOrFieldLike(type[0]);
|
|
913
|
+
}
|
|
914
|
+
if (isSchemaLike(type)) return true;
|
|
915
|
+
try {
|
|
916
|
+
if (getFieldValidator(type)) return true;
|
|
917
|
+
} catch {
|
|
918
|
+
return false;
|
|
919
|
+
}
|
|
920
|
+
return false;
|
|
921
|
+
}
|
|
922
|
+
function getSchemaModelName(type) {
|
|
923
|
+
if (!type) return null;
|
|
924
|
+
if (type.__modelName) return type.__modelName;
|
|
925
|
+
if (type.getModel) return type.getModel().name;
|
|
926
|
+
if (type.getSchema) return type.getSchema().__modelName;
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
function getSchemaFromAnyOrionForm(type) {
|
|
930
|
+
var _a, _b;
|
|
931
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
932
|
+
return (_b = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
|
|
933
|
+
}
|
|
934
|
+
if (type == null ? void 0 : type.getModel) return type.getModel().getSchema();
|
|
935
|
+
if (type.getSchema) {
|
|
936
|
+
return type.getSchema();
|
|
937
|
+
}
|
|
938
|
+
if (type.getSchema) {
|
|
939
|
+
return type.getSchema();
|
|
940
|
+
}
|
|
941
|
+
if (objectHasSubObjectWithKey(type, "type")) return type;
|
|
942
|
+
return null;
|
|
943
|
+
}
|
|
944
|
+
function objectHasSubObjectWithKey(object, key) {
|
|
945
|
+
if (!object || typeof object !== "object") return false;
|
|
946
|
+
for (const key1 in object) {
|
|
947
|
+
const value = object[key1];
|
|
948
|
+
if (value && typeof value === "object" && key in value) {
|
|
949
|
+
return true;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return false;
|
|
953
|
+
}
|
|
954
|
+
function getSchemaWithMetadataFromAnyOrionForm(type) {
|
|
955
|
+
return getSchemaFromAnyOrionForm(type);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// src/schemaWithName/index.ts
|
|
959
|
+
function schemaWithName(name, schema) {
|
|
960
|
+
schema.__modelName = name;
|
|
961
|
+
return schema;
|
|
962
|
+
}
|
|
900
963
|
// Annotate the CommonJS export names for ESM import in node:
|
|
901
964
|
0 && (module.exports = {
|
|
902
965
|
ValidationError,
|
|
@@ -904,9 +967,16 @@ function createEnum(name, values) {
|
|
|
904
967
|
cleanKey,
|
|
905
968
|
createEnum,
|
|
906
969
|
dotGetSchema,
|
|
970
|
+
fieldTypes,
|
|
907
971
|
getFieldType,
|
|
972
|
+
getSchemaFromAnyOrionForm,
|
|
973
|
+
getSchemaModelName,
|
|
974
|
+
getSchemaWithMetadataFromAnyOrionForm,
|
|
908
975
|
getValidationErrors,
|
|
976
|
+
isSchemaLike,
|
|
977
|
+
isSchemaOrFieldLike,
|
|
909
978
|
isValid,
|
|
979
|
+
schemaWithName,
|
|
910
980
|
validate,
|
|
911
981
|
validateKey
|
|
912
982
|
});
|