@tstdl/base 0.83.20 → 0.83.22
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/api/response.js +1 -0
- package/package.json +2 -2
- package/schema/decorators/types.d.ts +1 -1
- package/schema/schema.js +10 -3
- package/schema/schemas/assign.js +4 -6
- package/schema/schemas/object.d.ts +5 -4
- package/schema/schemas/record.d.ts +2 -2
- package/schema/schemas/record.js +3 -2
- package/schema/types/types.d.ts +15 -5
- package/schema/types/types.js +43 -21
- package/schema/utils/schema.js +11 -5
- package/templates/template.model.js +2 -2
- package/utils/array/array.d.ts +2 -0
- package/utils/array/array.js +5 -0
package/api/response.js
CHANGED
|
@@ -37,6 +37,7 @@ var import_type_guards = require("../utils/type-guards.js");
|
|
|
37
37
|
var import_default_error_handlers = require("./default-error-handlers.js");
|
|
38
38
|
const errorHandlers = /* @__PURE__ */ new Map();
|
|
39
39
|
function registerErrorHandler(constructor, statusCode, serializer, deserializer) {
|
|
40
|
+
(0, import_type_guards.assertString)(constructor.errorName, "static property 'errorName' missing.");
|
|
40
41
|
if (errorHandlers.has(constructor.errorName)) {
|
|
41
42
|
throw new Error(`A handler for ${constructor.errorName} already registered.`);
|
|
42
43
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.83.
|
|
3
|
+
"version": "0.83.22",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"preact-render-to-string": "^5.2",
|
|
63
63
|
"puppeteer": "^19.8",
|
|
64
64
|
"undici": "^5.21",
|
|
65
|
-
"urlpattern-polyfill": "^
|
|
65
|
+
"urlpattern-polyfill": "^7.0"
|
|
66
66
|
},
|
|
67
67
|
"peerDependenciesMeta": {
|
|
68
68
|
"@tstdl/angular": {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { OneOrMany } from '../../types.js';
|
|
2
2
|
import type { SchemaTestable } from '../schema.js';
|
|
3
3
|
import type { ObjectSchema, SchemaArrayConstraint, SchemaFactoryFunction, SchemaValueCoercer, SchemaValueConstraint, SchemaValueTransformer, TypeSchema, ValueSchema, ValueType } from '../types/index.js';
|
|
4
|
-
export type SchemaTypeReflectionData = Partial<Pick<ObjectSchema, 'mask' | '
|
|
4
|
+
export type SchemaTypeReflectionData = Partial<Pick<ObjectSchema, 'mask' | 'unknownProperties' | 'unknownPropertiesKey'>> & {
|
|
5
5
|
schema?: ObjectSchema | TypeSchema | ValueType;
|
|
6
6
|
factory?: SchemaFactoryFunction<any>;
|
|
7
7
|
};
|
package/schema/schema.js
CHANGED
|
@@ -147,7 +147,7 @@ function testObject(objectSchema, value, options = {}, path = import_json_path.J
|
|
|
147
147
|
const schemaPropertyKeys = (0, import_object.objectKeys)(schema.properties);
|
|
148
148
|
const valuePropertyKeys = (0, import_object.objectKeys)(value);
|
|
149
149
|
const unknownValuePropertyKeys = (0, import_set.differenceSets)(new Set(valuePropertyKeys), new Set(schemaPropertyKeys));
|
|
150
|
-
if (unknownValuePropertyKeys.length > 0 && !
|
|
150
|
+
if (unknownValuePropertyKeys.length > 0 && !mask && !schema.allowUnknownProperties) {
|
|
151
151
|
return { valid: false, error: new import_schema_error.SchemaError("Unknown property", { path: path.add(unknownValuePropertyKeys[0]), fast: options.fastErrors }) };
|
|
152
152
|
}
|
|
153
153
|
for (const key of schemaPropertyKeys) {
|
|
@@ -157,9 +157,16 @@ function testObject(objectSchema, value, options = {}, path = import_json_path.J
|
|
|
157
157
|
}
|
|
158
158
|
resultValue[key] = propertyResult.value;
|
|
159
159
|
}
|
|
160
|
-
if (schema.allowUnknownProperties
|
|
160
|
+
if (schema.allowUnknownProperties) {
|
|
161
161
|
for (const key of unknownValuePropertyKeys) {
|
|
162
|
-
const
|
|
162
|
+
const propertyPath = path.add(key);
|
|
163
|
+
if ((0, import_type_guards.isDefined)(schema.unknownPropertiesKey)) {
|
|
164
|
+
const keyResult = testWithFastError(schema.unknownPropertiesKey, key, options);
|
|
165
|
+
if (!keyResult.valid && !mask) {
|
|
166
|
+
return { valid: false, error: new import_schema_error.SchemaError("Invalid property key.", { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const propertyResult = testWithFastError(schema.unknownProperties, value[key], options, path.add(key));
|
|
163
170
|
if (!propertyResult.valid && !mask) {
|
|
164
171
|
return propertyResult;
|
|
165
172
|
}
|
package/schema/schemas/assign.js
CHANGED
|
@@ -23,6 +23,7 @@ __export(assign_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(assign_exports);
|
|
24
24
|
var import_array = require("../../utils/array/array.js");
|
|
25
25
|
var import_type_guards = require("../../utils/type-guards.js");
|
|
26
|
+
var import_types = require("../types/index.js");
|
|
26
27
|
var import_utils = require("../utils/index.js");
|
|
27
28
|
function assign(...inputs) {
|
|
28
29
|
const schemas = inputs.map(import_utils.getObjectSchema);
|
|
@@ -36,14 +37,11 @@ function assign(...inputs) {
|
|
|
36
37
|
...result.properties,
|
|
37
38
|
...schemas[i].properties
|
|
38
39
|
},
|
|
39
|
-
|
|
40
|
+
unknownProperties: [...(0, import_array.toArray)(result.unknownProperties ?? []), ...(0, import_array.toArray)(schemas[i].unknownProperties ?? [])],
|
|
41
|
+
unknownPropertiesKey: [...(0, import_array.toArray)(result.unknownPropertiesKey ?? []), ...(0, import_array.toArray)(schemas[i].unknownPropertiesKey ?? [])]
|
|
40
42
|
};
|
|
41
43
|
}
|
|
42
|
-
if (result.allowUnknownProperties.length == 0) {
|
|
43
|
-
const { allowUnknownProperties: _, ...rest } = result;
|
|
44
|
-
result = rest;
|
|
45
|
-
}
|
|
46
44
|
result.sourceType = void 0;
|
|
47
45
|
result.factory = void 0;
|
|
48
|
-
return result;
|
|
46
|
+
return (0, import_types.objectSchema)(result);
|
|
49
47
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { Record, SimplifiedOptionalize, TypedOmit } from '../../types.js';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
1
|
+
import type { Record, SimplifiedOptionalize, SimplifyObject, TypedOmit } from '../../types.js';
|
|
2
|
+
import type { IfNever } from 'type-fest';
|
|
3
|
+
import type { ObjectSchema, ObjectSchemaProperties, TypedObjectSchemaUnknownProperties } from '../types/index.js';
|
|
4
|
+
export type ObjectOptions<T extends Record = Record> = TypedOmit<ObjectSchema<T>, 'properties' | 'unknownProperties' | 'unknownPropertiesKey'>;
|
|
4
5
|
export declare function explicitObject<T extends Record>(properties: ObjectSchemaProperties<T>, options?: ObjectOptions<T>): ObjectSchema<T>;
|
|
5
|
-
export declare function object<T extends Record>(properties: ObjectSchemaProperties<T>, options?: ObjectOptions<T>): ObjectSchema<SimplifiedOptionalize<T
|
|
6
|
+
export declare function object<T extends Record = Record<never>, K extends PropertyKey = any, V = never>(properties: ObjectSchemaProperties<T>, options?: ObjectOptions<T> & TypedObjectSchemaUnknownProperties<K, V>): ObjectSchema<SimplifyObject<SimplifiedOptionalize<T> & IfNever<V, {}, Record<K, V>>>>;
|
|
6
7
|
export declare const emptyObjectSchema: ObjectSchema<{}>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { OneOrMany, Record, TypedOmit } from '../../types.js';
|
|
2
2
|
import type { SchemaTestable } from '../schema.js';
|
|
3
3
|
import type { ObjectSchema } from '../types/index.js';
|
|
4
|
-
export type RecordOptions<T extends Record = Record> = TypedOmit<ObjectSchema<T>, 'properties' | '
|
|
5
|
-
export declare function record<T>(
|
|
4
|
+
export type RecordOptions<T extends Record = Record> = TypedOmit<ObjectSchema<T>, 'properties' | 'unknownProperties' | 'unknownPropertiesKey' | 'mask'>;
|
|
5
|
+
export declare function record<K extends PropertyKey, T>(keySchema: OneOrMany<SchemaTestable<K>>, valueSchema: OneOrMany<SchemaTestable<T>>, options?: RecordOptions): ObjectSchema<Record<K, T>>;
|
package/schema/schemas/record.js
CHANGED
|
@@ -22,10 +22,11 @@ __export(record_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(record_exports);
|
|
24
24
|
var import_types = require("../types/index.js");
|
|
25
|
-
function record(
|
|
25
|
+
function record(keySchema, valueSchema, options) {
|
|
26
26
|
return (0, import_types.objectSchema)({
|
|
27
27
|
properties: {},
|
|
28
|
-
|
|
28
|
+
unknownProperties: valueSchema,
|
|
29
|
+
unknownPropertiesKey: keySchema,
|
|
29
30
|
...options
|
|
30
31
|
});
|
|
31
32
|
}
|
package/schema/types/types.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { AbstractConstructor, OneOrMany, Record, Type, TypedOmit } from '../../types.js';
|
|
2
|
-
import type { NormalizedSchema, Schema, SchemaTestable } from '../schema.js';
|
|
3
2
|
import type { SchemaError } from '../schema.error.js';
|
|
3
|
+
import type { NormalizedSchema, Schema, SchemaTestable } from '../schema.js';
|
|
4
4
|
import type { SchemaArrayConstraint } from './schema-array-constraint.js';
|
|
5
5
|
import type { SchemaValueCoercer } from './schema-value-coercer.js';
|
|
6
6
|
import type { SchemaValueConstraint } from './schema-value-constraint.js';
|
|
7
7
|
import type { SchemaValueTransformer } from './schema-value-transformer.js';
|
|
8
8
|
declare const schemaOutputTypeSymbol: unique symbol;
|
|
9
|
+
declare const optimized: unique symbol;
|
|
9
10
|
export type SchemaFactoryFunction<T> = (data: T) => T;
|
|
10
11
|
export type SchemaFactory<T> = {
|
|
11
12
|
type: Type<T>;
|
|
@@ -26,12 +27,18 @@ export type TupleSchemaOutput<T extends readonly SchemaTestable[]> = {
|
|
|
26
27
|
};
|
|
27
28
|
export type ObjectSchemaOrType<T = any> = ObjectSchema<T> | AbstractConstructor<T>;
|
|
28
29
|
export type ObjectSchema<T = any> = {
|
|
30
|
+
[optimized]?: boolean;
|
|
29
31
|
[schemaOutputTypeSymbol]?: T;
|
|
30
32
|
sourceType?: ValueType;
|
|
31
33
|
factory?: SchemaFactory<T>;
|
|
32
34
|
properties: ObjectSchemaProperties<T>;
|
|
33
35
|
mask?: boolean;
|
|
34
|
-
|
|
36
|
+
unknownProperties?: OneOrMany<SchemaTestable>;
|
|
37
|
+
unknownPropertiesKey?: OneOrMany<SchemaTestable>;
|
|
38
|
+
};
|
|
39
|
+
export type TypedObjectSchemaUnknownProperties<K extends PropertyKey, V> = {
|
|
40
|
+
unknownProperties?: OneOrMany<SchemaTestable<V>>;
|
|
41
|
+
unknownPropertiesKey?: OneOrMany<SchemaTestable<K>>;
|
|
35
42
|
};
|
|
36
43
|
export type TypeSchema<T = any> = {
|
|
37
44
|
type: ValueType<T>;
|
|
@@ -40,6 +47,7 @@ export type NormalizedTypeSchema<T = any> = {
|
|
|
40
47
|
foo: ResolvedValueType<T>;
|
|
41
48
|
};
|
|
42
49
|
export type ValueSchema<T = unknown> = {
|
|
50
|
+
[optimized]?: boolean;
|
|
43
51
|
[schemaOutputTypeSymbol]?: T;
|
|
44
52
|
schema: OneOrMany<SchemaTestable<T>>;
|
|
45
53
|
array?: boolean;
|
|
@@ -59,11 +67,13 @@ export type NormalizedObjectSchema<T = any> = {
|
|
|
59
67
|
factory?: SchemaFactory<T>;
|
|
60
68
|
properties: NormalizedObjectSchemaProperties<T>;
|
|
61
69
|
mask?: boolean;
|
|
62
|
-
allowUnknownProperties:
|
|
70
|
+
allowUnknownProperties: boolean;
|
|
71
|
+
unknownProperties?: Schema;
|
|
72
|
+
unknownPropertiesKey?: Schema;
|
|
63
73
|
};
|
|
64
74
|
export type NormalizedValueSchema<T = any> = {
|
|
65
75
|
[schemaOutputTypeSymbol]?: T;
|
|
66
|
-
schema:
|
|
76
|
+
schema: Schema<T>[];
|
|
67
77
|
array: boolean;
|
|
68
78
|
optional: boolean;
|
|
69
79
|
nullable: boolean;
|
|
@@ -156,7 +166,7 @@ export declare function isDeferredValueType<T>(value: SchemaTestable<T>): value
|
|
|
156
166
|
export declare function isDeferredValueType(value: any): value is DeferredValueType;
|
|
157
167
|
export declare function resolveValueTypes<T>(valueTypes: OneOrMany<ValueType<T>>): OneOrMany<ResolvedValueType<T>>;
|
|
158
168
|
export declare function resolveValueType<T>(valueType: ValueType<T>): ResolvedValueType<T>;
|
|
159
|
-
export declare function
|
|
169
|
+
export declare function schemaTestablesToSchemas<T>(schemaTestables: OneOrMany<SchemaTestable<T>>): OneOrMany<Schema<T>>;
|
|
160
170
|
export declare function schemaTestableToSchema<T>(valueTypeOrSchema: SchemaTestable<T>): Schema<T>;
|
|
161
171
|
export declare function optimizeObjectSchema<T>(schema: ObjectSchema<T>): ObjectSchema<T>;
|
|
162
172
|
export declare function optimizeObjectSchemaProperties<T>(properties: ObjectSchemaProperties<T>): ObjectSchemaProperties<T>;
|
package/schema/types/types.js
CHANGED
|
@@ -35,15 +35,17 @@ __export(types_exports, {
|
|
|
35
35
|
resolveValueType: () => resolveValueType,
|
|
36
36
|
resolveValueTypes: () => resolveValueTypes,
|
|
37
37
|
schemaTestableToSchema: () => schemaTestableToSchema,
|
|
38
|
+
schemaTestablesToSchemas: () => schemaTestablesToSchemas,
|
|
38
39
|
transformErrorResult: () => transformErrorResult,
|
|
39
40
|
transformErrorResultSymbol: () => transformErrorResultSymbol,
|
|
40
41
|
typeSchema: () => typeSchema,
|
|
41
|
-
valueSchema: () => valueSchema
|
|
42
|
-
valueTypesOrSchemasToSchemas: () => valueTypesOrSchemasToSchemas
|
|
42
|
+
valueSchema: () => valueSchema
|
|
43
43
|
});
|
|
44
44
|
module.exports = __toCommonJS(types_exports);
|
|
45
|
+
var import_array = require("../../utils/array/array.js");
|
|
45
46
|
var import_object = require("../../utils/object/object.js");
|
|
46
47
|
var import_type_guards = require("../../utils/type-guards.js");
|
|
48
|
+
const optimized = Symbol("schema optimized");
|
|
47
49
|
const primitiveConstructors = [String, Number, Boolean, Symbol, BigInt, Function, "undefined", "null"];
|
|
48
50
|
const primitiveConstructorSet = new Set(primitiveConstructors);
|
|
49
51
|
const transformErrorResultSymbol = Symbol("Transform error");
|
|
@@ -60,6 +62,7 @@ function objectSchema(schema) {
|
|
|
60
62
|
return optimizeObjectSchema(schema);
|
|
61
63
|
}
|
|
62
64
|
function valueSchema(schema, options) {
|
|
65
|
+
(0, import_type_guards.assert)(!(0, import_type_guards.isArray)(schema) || schema.length > 0, "No schema provided.");
|
|
63
66
|
return optimizeValueSchema({ schema, ...options });
|
|
64
67
|
}
|
|
65
68
|
function typeSchema(type) {
|
|
@@ -93,11 +96,17 @@ function resolveValueTypes(valueTypes) {
|
|
|
93
96
|
function resolveValueType(valueType) {
|
|
94
97
|
return isDeferredValueType(valueType) ? resolveValueType(valueType.deferred()) : valueType;
|
|
95
98
|
}
|
|
96
|
-
function
|
|
97
|
-
if ((0, import_type_guards.isArray)(
|
|
98
|
-
|
|
99
|
+
function schemaTestablesToSchemas(schemaTestables) {
|
|
100
|
+
if ((0, import_type_guards.isArray)(schemaTestables)) {
|
|
101
|
+
if (schemaTestables.length == 1) {
|
|
102
|
+
return schemaTestableToSchema(schemaTestables[0]);
|
|
103
|
+
}
|
|
104
|
+
if (schemaTestables.length == 0) {
|
|
105
|
+
throw new Error("No schema provided.");
|
|
106
|
+
}
|
|
107
|
+
return schemaTestables.map(schemaTestableToSchema);
|
|
99
108
|
}
|
|
100
|
-
return schemaTestableToSchema(
|
|
109
|
+
return schemaTestableToSchema(schemaTestables);
|
|
101
110
|
}
|
|
102
111
|
function schemaTestableToSchema(valueTypeOrSchema) {
|
|
103
112
|
if (isSchema(valueTypeOrSchema)) {
|
|
@@ -106,25 +115,38 @@ function schemaTestableToSchema(valueTypeOrSchema) {
|
|
|
106
115
|
return typeSchema(valueTypeOrSchema);
|
|
107
116
|
}
|
|
108
117
|
function optimizeObjectSchema(schema) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
118
|
+
if (schema[optimized] == true) {
|
|
119
|
+
return schema;
|
|
120
|
+
}
|
|
121
|
+
return (0, import_object.filterObject)({
|
|
122
|
+
[optimized]: true,
|
|
123
|
+
...schema,
|
|
124
|
+
properties: optimizeObjectSchemaProperties(schema.properties),
|
|
125
|
+
unknownProperties: (0, import_type_guards.isUndefined)(schema.unknownProperties) || (0, import_type_guards.isArray)(schema.unknownProperties) && schema.unknownProperties.length == 0 ? void 0 : optimizeSchema(schema.unknownProperties),
|
|
126
|
+
unknownPropertiesKey: (0, import_type_guards.isUndefined)(schema.unknownPropertiesKey) || (0, import_type_guards.isArray)(schema.unknownPropertiesKey) && schema.unknownPropertiesKey.length == 0 ? void 0 : optimizeSchema(schema.unknownPropertiesKey)
|
|
127
|
+
}, import_type_guards.isDefined);
|
|
113
128
|
}
|
|
114
129
|
function optimizeObjectSchemaProperties(properties) {
|
|
115
|
-
|
|
116
|
-
return Object.fromEntries(entries);
|
|
130
|
+
return (0, import_object.mapObjectValues)(properties, optimizeSchema);
|
|
117
131
|
}
|
|
118
132
|
function optimizeValueSchema(schema) {
|
|
119
|
-
const
|
|
120
|
-
return isValueSchema(
|
|
133
|
+
const optimizedSchema = optimizeSchema(schema);
|
|
134
|
+
return isValueSchema(optimizedSchema) ? optimizedSchema : { [optimized]: true, schema: optimizedSchema };
|
|
121
135
|
}
|
|
122
136
|
function optimizeSchema(schema) {
|
|
123
137
|
if ((0, import_type_guards.isArray)(schema)) {
|
|
138
|
+
if (schema[optimized] == true) {
|
|
139
|
+
return schema;
|
|
140
|
+
}
|
|
124
141
|
if (schema.length == 1) {
|
|
125
142
|
return optimizeSchema(schema[0]);
|
|
126
143
|
}
|
|
127
|
-
|
|
144
|
+
if (schema.length == 0) {
|
|
145
|
+
throw new Error("No schema provided.");
|
|
146
|
+
}
|
|
147
|
+
const optimizedSchemas = (0, import_array.distinct)((0, import_array.distinct)(schema).flatMap(optimizeSchema));
|
|
148
|
+
optimizedSchemas[optimized] = true;
|
|
149
|
+
return optimizedSchemas;
|
|
128
150
|
}
|
|
129
151
|
if ((0, import_type_guards.isFunction)(schema) || (0, import_type_guards.isString)(schema) || isDeferredValueType(schema)) {
|
|
130
152
|
return schema;
|
|
@@ -133,19 +155,18 @@ function optimizeSchema(schema) {
|
|
|
133
155
|
return schema.type;
|
|
134
156
|
}
|
|
135
157
|
if (isValueSchema(schema)) {
|
|
158
|
+
if (schema[optimized] == true) {
|
|
159
|
+
return schema;
|
|
160
|
+
}
|
|
136
161
|
const entries = (0, import_object.objectEntries)(schema).filter(([, value]) => (0, import_type_guards.isDefined)(value));
|
|
137
162
|
if (entries.length == 1) {
|
|
138
|
-
|
|
139
|
-
return optimizeSchema(schema.schema);
|
|
140
|
-
} else if (schema.schema.length == 1) {
|
|
141
|
-
return optimizeSchema(schema.schema[0]);
|
|
142
|
-
}
|
|
143
|
-
return schema.schema.map(optimizeSchema);
|
|
163
|
+
return optimizeSchema(schema.schema);
|
|
144
164
|
}
|
|
145
165
|
if (isValueSchema(schema.schema)) {
|
|
146
166
|
const combinedProperties = /* @__PURE__ */ new Set([...(0, import_object.objectKeys)(schema), ...(0, import_object.objectKeys)(schema.schema)]);
|
|
147
167
|
if (isValueSchema(schema.schema) && !combinedProperties.has("array") && !combinedProperties.has("coerce") && !combinedProperties.has("coercers") && !combinedProperties.has("transformers") && !combinedProperties.has("arrayConstraints") && !combinedProperties.has("valueConstraints")) {
|
|
148
168
|
return {
|
|
169
|
+
[optimized]: true,
|
|
149
170
|
schema: optimizeSchema(schema.schema.schema),
|
|
150
171
|
...(0, import_object.filterObject)({
|
|
151
172
|
optional: (schema.optional ?? false) || (schema.schema.optional ?? false) ? true : void 0,
|
|
@@ -156,6 +177,7 @@ function optimizeSchema(schema) {
|
|
|
156
177
|
}
|
|
157
178
|
const { schema: innerSchema, optional, nullable, coercers, transformers, arrayConstraints, valueConstraints, ...rest } = schema;
|
|
158
179
|
return (0, import_object.filterObject)({
|
|
180
|
+
[optimized]: true,
|
|
159
181
|
schema: optimizeSchema(innerSchema),
|
|
160
182
|
optional: optional ?? false ? true : void 0,
|
|
161
183
|
nullable: nullable ?? false ? true : void 0,
|
package/schema/utils/schema.js
CHANGED
|
@@ -57,17 +57,22 @@ function _normalizeSchema(schema) {
|
|
|
57
57
|
throw new Error("Unsupported schema.");
|
|
58
58
|
}
|
|
59
59
|
function _normalizeObjectSchema(schema) {
|
|
60
|
+
const unknownPropertiesSchema = (0, import_type_guards.isDefined)(schema.unknownProperties) ? (0, import_types.schemaTestablesToSchemas)(schema.unknownProperties) : void 0;
|
|
61
|
+
const unknownPropertiesKeySchema = (0, import_type_guards.isDefined)(schema.unknownPropertiesKey) ? (0, import_types.schemaTestablesToSchemas)(schema.unknownPropertiesKey) : void 0;
|
|
62
|
+
const allowUnknownProperties = (0, import_type_guards.isArray)(unknownPropertiesSchema) && unknownPropertiesSchema.length > 0 || (0, import_type_guards.isDefined)(unknownPropertiesSchema);
|
|
60
63
|
const normalizedSchema = {
|
|
61
64
|
factory: schema.factory,
|
|
62
|
-
properties: (0, import_object.mapObjectValues)(schema.properties, (propertyValueType) => (0, import_types.
|
|
65
|
+
properties: (0, import_object.mapObjectValues)(schema.properties, (propertyValueType) => (0, import_types.schemaTestablesToSchemas)(propertyValueType)),
|
|
63
66
|
mask: schema.mask,
|
|
64
|
-
allowUnknownProperties
|
|
67
|
+
allowUnknownProperties,
|
|
68
|
+
unknownProperties: (0, import_type_guards.isArray)(unknownPropertiesSchema) ? (0, import_types.valueSchema)(unknownPropertiesSchema) : unknownPropertiesSchema,
|
|
69
|
+
unknownPropertiesKey: (0, import_type_guards.isArray)(unknownPropertiesKeySchema) ? (0, import_types.valueSchema)(unknownPropertiesKeySchema) : unknownPropertiesKeySchema
|
|
65
70
|
};
|
|
66
71
|
return normalizedSchema;
|
|
67
72
|
}
|
|
68
73
|
function _normalizeValueSchema(schema) {
|
|
69
74
|
const normalizedValueSchema = {
|
|
70
|
-
schema: new Set((0, import_array.toArray)(schema.schema).map(import_types.schemaTestableToSchema)
|
|
75
|
+
schema: [...new Set((0, import_array.toArray)(schema.schema))].map(import_types.schemaTestableToSchema),
|
|
71
76
|
array: schema.array ?? false,
|
|
72
77
|
optional: schema.optional ?? false,
|
|
73
78
|
nullable: schema.nullable ?? false,
|
|
@@ -120,7 +125,8 @@ function _tryGetObjectSchemaFromReflection(type) {
|
|
|
120
125
|
factory: (0, import_type_guards.isDefined)(typeData.factory) ? { builder: typeData.factory } : { type },
|
|
121
126
|
properties: dataSchema?.properties ?? getObjectSchemaPropertiesFromReflection(metadata, type),
|
|
122
127
|
mask: typeData.mask ?? dataSchema?.mask,
|
|
123
|
-
|
|
128
|
+
unknownProperties: ((0, import_type_guards.isUndefined)(typeData.unknownProperties) || (0, import_type_guards.isArray)(typeData.unknownProperties) && typeData.unknownProperties.length == 0 ? void 0 : typeData.unknownProperties) ?? dataSchema?.unknownProperties,
|
|
129
|
+
unknownPropertiesKey: ((0, import_type_guards.isUndefined)(typeData.unknownPropertiesKey) || (0, import_type_guards.isArray)(typeData.unknownPropertiesKey) && typeData.unknownPropertiesKey.length == 0 ? void 0 : typeData.unknownPropertiesKey) ?? dataSchema?.unknownPropertiesKey
|
|
124
130
|
});
|
|
125
131
|
if ((0, import_type_guards.isUndefined)(dataSchema)) {
|
|
126
132
|
const prototype = Reflect.getPrototypeOf(type);
|
|
@@ -140,7 +146,7 @@ function getObjectSchemaPropertiesFromReflection(metadata, type) {
|
|
|
140
146
|
if (array && ((0, import_type_guards.isUndefined)(itemType) || (0, import_type_guards.isArray)(itemType) && itemType.length == 0)) {
|
|
141
147
|
throw new Error(`Item type missing on type ${type.name} at property "${String(key)}"`);
|
|
142
148
|
}
|
|
143
|
-
properties[key] = (0, import_types.valueSchema)((0, import_types.
|
|
149
|
+
properties[key] = (0, import_types.valueSchema)((0, import_types.schemaTestablesToSchemas)(itemType ?? propertyMetadata.type), {
|
|
144
150
|
array,
|
|
145
151
|
optional: reflectionData?.optional,
|
|
146
152
|
nullable: reflectionData?.nullable,
|
|
@@ -59,7 +59,7 @@ __decorate([
|
|
|
59
59
|
__metadata("design:type", Object)
|
|
60
60
|
], TemplateField.prototype, "options", void 0);
|
|
61
61
|
TemplateField = __decorate([
|
|
62
|
-
(0, import_schema.Class)({
|
|
62
|
+
(0, import_schema.Class)({ unknownProperties: (0, import_schema.any)() })
|
|
63
63
|
], TemplateField);
|
|
64
64
|
class Template {
|
|
65
65
|
/** name of template */
|
|
@@ -72,7 +72,7 @@ __decorate([
|
|
|
72
72
|
__metadata("design:type", String)
|
|
73
73
|
], Template.prototype, "name", void 0);
|
|
74
74
|
__decorate([
|
|
75
|
-
(0, import_schema.Property)({ schema: (0, import_schema.object)({}, {
|
|
75
|
+
(0, import_schema.Property)({ schema: (0, import_schema.object)({}, { unknownProperties: TemplateField }) }),
|
|
76
76
|
__metadata("design:type", Object)
|
|
77
77
|
], Template.prototype, "fields", void 0);
|
|
78
78
|
__decorate([
|
package/utils/array/array.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IteratorFunction } from '../iterable-helpers/types.js';
|
|
1
2
|
/**
|
|
2
3
|
* Returns value as is, if it is an array, otherwise puts the single value into an array.
|
|
3
4
|
*/
|
|
@@ -12,6 +13,7 @@ export declare function toArrayCopy<T>(value: T | T[] | readonly T[]): T[];
|
|
|
12
13
|
*/
|
|
13
14
|
export declare function extractValueOfArrayIfSingleElement<T>(value: T | T[]): T | T[];
|
|
14
15
|
export declare function extractValueOfArrayIfSingleElement<T>(value: T | readonly T[]): T | readonly T[];
|
|
16
|
+
export declare function distinct<T>(values: Iterable<T>, selector?: IteratorFunction<T, any>): T[];
|
|
15
17
|
/**
|
|
16
18
|
* creates a new array of specified length and fills it with values from the specified value provider function
|
|
17
19
|
* @param length length of the new array
|
package/utils/array/array.js
CHANGED
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var array_exports = {};
|
|
20
20
|
__export(array_exports, {
|
|
21
21
|
createArray: () => createArray,
|
|
22
|
+
distinct: () => distinct,
|
|
22
23
|
extractValueOfArrayIfSingleElement: () => extractValueOfArrayIfSingleElement,
|
|
23
24
|
randomItem: () => randomItem,
|
|
24
25
|
randomItems: () => randomItems,
|
|
@@ -27,6 +28,7 @@ __export(array_exports, {
|
|
|
27
28
|
toArrayCopy: () => toArrayCopy
|
|
28
29
|
});
|
|
29
30
|
module.exports = __toCommonJS(array_exports);
|
|
31
|
+
var import_distinct = require("../iterable-helpers/distinct.js");
|
|
30
32
|
var import_math = require("../math.js");
|
|
31
33
|
var import_type_guards = require("../type-guards.js");
|
|
32
34
|
function toArray(value) {
|
|
@@ -41,6 +43,9 @@ function extractValueOfArrayIfSingleElement(value) {
|
|
|
41
43
|
}
|
|
42
44
|
return value;
|
|
43
45
|
}
|
|
46
|
+
function distinct(values, selector) {
|
|
47
|
+
return [...(0, import_type_guards.isDefined)(selector) ? (0, import_distinct.distinct)(values, selector) : new Set(values)];
|
|
48
|
+
}
|
|
44
49
|
function createArray(length, valueProvider) {
|
|
45
50
|
const arr = [];
|
|
46
51
|
arr.length = length;
|