@tstdl/base 0.91.0 → 0.91.2
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/authentication/authentication.api.d.ts +1 -1
- package/enumerable/async-enumerable.d.ts +7 -3
- package/enumerable/async-enumerable.js +8 -8
- package/enumerable/enumerable.d.ts +5 -1
- package/enumerable/enumerable.js +8 -8
- package/package.json +5 -5
- package/schema-old/array-constraints/index.d.ts +0 -2
- package/schema-old/array-constraints/index.js +0 -2
- package/schema-old/array-constraints/maximum-length.d.ts +0 -11
- package/schema-old/array-constraints/maximum-length.js +0 -22
- package/schema-old/array-constraints/minimum-length.d.ts +0 -11
- package/schema-old/array-constraints/minimum-length.js +0 -22
- package/schema-old/coercers/boolean.coercer.d.ts +0 -9
- package/schema-old/coercers/boolean.coercer.js +0 -27
- package/schema-old/coercers/date.coercer.d.ts +0 -9
- package/schema-old/coercers/date.coercer.js +0 -16
- package/schema-old/coercers/index.d.ts +0 -6
- package/schema-old/coercers/index.js +0 -6
- package/schema-old/coercers/number.coercer.d.ts +0 -9
- package/schema-old/coercers/number.coercer.js +0 -14
- package/schema-old/coercers/regexp.coercer.d.ts +0 -11
- package/schema-old/coercers/regexp.coercer.js +0 -20
- package/schema-old/coercers/string.coercer.d.ts +0 -9
- package/schema-old/coercers/string.coercer.js +0 -14
- package/schema-old/coercers/uint8-array.coercer.d.ts +0 -11
- package/schema-old/coercers/uint8-array.coercer.js +0 -22
- package/schema-old/constraints/enumeration.d.ts +0 -13
- package/schema-old/constraints/enumeration.js +0 -29
- package/schema-old/constraints/generic.d.ts +0 -20
- package/schema-old/constraints/generic.js +0 -23
- package/schema-old/constraints/index.d.ts +0 -12
- package/schema-old/constraints/index.js +0 -12
- package/schema-old/constraints/integer.d.ts +0 -12
- package/schema-old/constraints/integer.js +0 -23
- package/schema-old/constraints/length.d.ts +0 -6
- package/schema-old/constraints/length.js +0 -15
- package/schema-old/constraints/literal.d.ts +0 -10
- package/schema-old/constraints/literal.js +0 -24
- package/schema-old/constraints/maximum-date.d.ts +0 -12
- package/schema-old/constraints/maximum-date.js +0 -26
- package/schema-old/constraints/maximum-length.d.ts +0 -12
- package/schema-old/constraints/maximum-length.js +0 -25
- package/schema-old/constraints/maximum.d.ts +0 -12
- package/schema-old/constraints/maximum.js +0 -24
- package/schema-old/constraints/minimum-date.d.ts +0 -12
- package/schema-old/constraints/minimum-date.js +0 -26
- package/schema-old/constraints/minimum-length.d.ts +0 -12
- package/schema-old/constraints/minimum-length.js +0 -25
- package/schema-old/constraints/minimum.d.ts +0 -12
- package/schema-old/constraints/minimum.js +0 -24
- package/schema-old/constraints/pattern.d.ts +0 -13
- package/schema-old/constraints/pattern.js +0 -26
- package/schema-old/decorators/class.d.ts +0 -3
- package/schema-old/decorators/class.js +0 -5
- package/schema-old/decorators/coerce.d.ts +0 -2
- package/schema-old/decorators/coerce.js +0 -5
- package/schema-old/decorators/constraint.d.ts +0 -4
- package/schema-old/decorators/constraint.js +0 -7
- package/schema-old/decorators/index.d.ts +0 -6
- package/schema-old/decorators/index.js +0 -6
- package/schema-old/decorators/property.d.ts +0 -6
- package/schema-old/decorators/property.js +0 -17
- package/schema-old/decorators/types.d.ts +0 -19
- package/schema-old/decorators/types.js +0 -1
- package/schema-old/decorators/utils.d.ts +0 -15
- package/schema-old/decorators/utils.js +0 -48
- package/schema-old/index.d.ts +0 -10
- package/schema-old/index.js +0 -10
- package/schema-old/schema.d.ts +0 -15
- package/schema-old/schema.error.d.ts +0 -26
- package/schema-old/schema.error.js +0 -64
- package/schema-old/schema.js +0 -283
- package/schema-old/schemas/any.d.ts +0 -5
- package/schema-old/schemas/any.js +0 -9
- package/schema-old/schemas/array.d.ts +0 -12
- package/schema-old/schemas/array.js +0 -23
- package/schema-old/schemas/assign.d.ts +0 -7
- package/schema-old/schemas/assign.js +0 -24
- package/schema-old/schemas/boolean.d.ts +0 -5
- package/schema-old/schemas/boolean.js +0 -9
- package/schema-old/schemas/constraint.d.ts +0 -5
- package/schema-old/schemas/constraint.js +0 -7
- package/schema-old/schemas/date.d.ts +0 -8
- package/schema-old/schemas/date.js +0 -22
- package/schema-old/schemas/defaulted.d.ts +0 -6
- package/schema-old/schemas/defaulted.js +0 -10
- package/schema-old/schemas/enumeration.d.ts +0 -6
- package/schema-old/schemas/enumeration.js +0 -17
- package/schema-old/schemas/index.d.ts +0 -26
- package/schema-old/schemas/index.js +0 -26
- package/schema-old/schemas/instance.d.ts +0 -3
- package/schema-old/schemas/instance.js +0 -5
- package/schema-old/schemas/literal.d.ts +0 -5
- package/schema-old/schemas/literal.js +0 -17
- package/schema-old/schemas/nullable.d.ts +0 -6
- package/schema-old/schemas/nullable.js +0 -9
- package/schema-old/schemas/number.d.ts +0 -9
- package/schema-old/schemas/number.js +0 -27
- package/schema-old/schemas/object.d.ts +0 -7
- package/schema-old/schemas/object.js +0 -12
- package/schema-old/schemas/omit.d.ts +0 -3
- package/schema-old/schemas/omit.js +0 -15
- package/schema-old/schemas/one-or-many.d.ts +0 -9
- package/schema-old/schemas/one-or-many.js +0 -15
- package/schema-old/schemas/optional.d.ts +0 -7
- package/schema-old/schemas/optional.js +0 -8
- package/schema-old/schemas/partial.d.ts +0 -4
- package/schema-old/schemas/partial.js +0 -17
- package/schema-old/schemas/pick.d.ts +0 -3
- package/schema-old/schemas/pick.js +0 -15
- package/schema-old/schemas/readable-stream.d.ts +0 -6
- package/schema-old/schemas/readable-stream.js +0 -9
- package/schema-old/schemas/record.d.ts +0 -5
- package/schema-old/schemas/record.js +0 -10
- package/schema-old/schemas/regexp.d.ts +0 -6
- package/schema-old/schemas/regexp.js +0 -9
- package/schema-old/schemas/string.d.ts +0 -22
- package/schema-old/schemas/string.js +0 -38
- package/schema-old/schemas/transform.d.ts +0 -5
- package/schema-old/schemas/transform.js +0 -7
- package/schema-old/schemas/uint8-array.d.ts +0 -10
- package/schema-old/schemas/uint8-array.js +0 -29
- package/schema-old/schemas/union.d.ts +0 -7
- package/schema-old/schemas/union.js +0 -13
- package/schema-old/schemas/unknown.d.ts +0 -5
- package/schema-old/schemas/unknown.js +0 -9
- package/schema-old/transformers/generic.d.ts +0 -12
- package/schema-old/transformers/generic.js +0 -18
- package/schema-old/transformers/index.d.ts +0 -4
- package/schema-old/transformers/index.js +0 -4
- package/schema-old/transformers/lowercase.d.ts +0 -8
- package/schema-old/transformers/lowercase.js +0 -12
- package/schema-old/transformers/trim.d.ts +0 -8
- package/schema-old/transformers/trim.js +0 -12
- package/schema-old/transformers/uppercase.d.ts +0 -8
- package/schema-old/transformers/uppercase.js +0 -12
- package/schema-old/types/index.d.ts +0 -5
- package/schema-old/types/index.js +0 -5
- package/schema-old/types/schema-array-constraint.d.ts +0 -5
- package/schema-old/types/schema-array-constraint.js +0 -2
- package/schema-old/types/schema-value-coercer.d.ts +0 -8
- package/schema-old/types/schema-value-coercer.js +0 -2
- package/schema-old/types/schema-value-constraint.d.ts +0 -8
- package/schema-old/types/schema-value-constraint.js +0 -2
- package/schema-old/types/schema-value-transformer.d.ts +0 -7
- package/schema-old/types/schema-value-transformer.js +0 -2
- package/schema-old/types/types.d.ts +0 -175
- package/schema-old/types/types.js +0 -172
- package/schema-old/utils/index.d.ts +0 -2
- package/schema-old/utils/index.js +0 -2
- package/schema-old/utils/schema.d.ts +0 -18
- package/schema-old/utils/schema.js +0 -139
- package/schema-old/utils/value-type.d.ts +0 -8
- package/schema-old/utils/value-type.js +0 -56
package/schema-old/schema.js
DELETED
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import { JsonPath } from '../json-path/index.js';
|
|
2
|
-
import { toArray } from '../utils/array/array.js';
|
|
3
|
-
import { noop } from '../utils/noop.js';
|
|
4
|
-
import { objectKeys } from '../utils/object/object.js';
|
|
5
|
-
import { differenceSets } from '../utils/set.js';
|
|
6
|
-
import { isArray, isDefined, isFunction, isNotNull, isNull, isUndefined } from '../utils/type-guards.js';
|
|
7
|
-
import { booleanCoercer, dateCoercer, numberCoercer, regExpCoercer, stringCoercer, uint8ArrayCoercer } from './coercers/index.js';
|
|
8
|
-
import { SchemaError } from './schema.error.js';
|
|
9
|
-
import { isObjectSchema, isTransformErrorResult, isTypeSchema, isValueSchema, resolveValueType, resolveValueTypes, schemaTestableToSchema, transformErrorResultSymbol } from './types/index.js';
|
|
10
|
-
import { getArrayItemSchema, getSchemaTypeNames, getSchemaValueTypes, getValueType, includesValueType, normalizeObjectSchema, normalizeValueSchema, tryGetObjectSchemaFromReflection } from './utils/index.js';
|
|
11
|
-
const defaultCoercers = new Map();
|
|
12
|
-
let initialize = () => {
|
|
13
|
-
Schema.registerDefaultCoercer(numberCoercer);
|
|
14
|
-
Schema.registerDefaultCoercer(booleanCoercer);
|
|
15
|
-
Schema.registerDefaultCoercer(stringCoercer);
|
|
16
|
-
Schema.registerDefaultCoercer(dateCoercer);
|
|
17
|
-
Schema.registerDefaultCoercer(regExpCoercer);
|
|
18
|
-
Schema.registerDefaultCoercer(uint8ArrayCoercer);
|
|
19
|
-
initialize = noop;
|
|
20
|
-
};
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare, @typescript-eslint/naming-convention
|
|
22
|
-
export const Schema = {
|
|
23
|
-
registerDefaultCoercer(coercer) {
|
|
24
|
-
for (const sourceType of toArray(coercer.sourceType)) {
|
|
25
|
-
if (!defaultCoercers.has(sourceType)) {
|
|
26
|
-
defaultCoercers.set(sourceType, []);
|
|
27
|
-
}
|
|
28
|
-
defaultCoercers.get(sourceType).push(coercer);
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
test(schemaOrValueType, value, options, path = JsonPath.ROOT) {
|
|
32
|
-
const normalizedOptions = { fastErrors: true, ...options };
|
|
33
|
-
const schema = schemaTestableToSchema(schemaOrValueType);
|
|
34
|
-
const result = testSchema(schema, value, normalizedOptions, path);
|
|
35
|
-
if (result.valid) {
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
if (isUndefined(result.error.stack)) {
|
|
39
|
-
result.error.stack = new Error().stack;
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
},
|
|
43
|
-
validate(schemaOrValueType, value, options) {
|
|
44
|
-
const schema = schemaTestableToSchema(schemaOrValueType);
|
|
45
|
-
const result = this.test(schema, value, options);
|
|
46
|
-
return result.valid;
|
|
47
|
-
},
|
|
48
|
-
parse(schemaOrValueType, value, options) {
|
|
49
|
-
const schema = schemaTestableToSchema(schemaOrValueType);
|
|
50
|
-
const result = this.test(schema, value, options);
|
|
51
|
-
if (result.valid) {
|
|
52
|
-
return result.value;
|
|
53
|
-
}
|
|
54
|
-
throw result.error;
|
|
55
|
-
},
|
|
56
|
-
function(argumentSchemas, returnSchema, handler) {
|
|
57
|
-
const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
|
|
58
|
-
const schema = getFunctionParametersSchema(argumentSchemas);
|
|
59
|
-
return {
|
|
60
|
-
[name](...unsafeArgs) {
|
|
61
|
-
const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
|
|
62
|
-
const unsafeResult = handler(...safeArgs);
|
|
63
|
-
return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
|
|
64
|
-
}
|
|
65
|
-
}[name];
|
|
66
|
-
},
|
|
67
|
-
asyncFunction(argumentSchemas, returnSchema, handler) {
|
|
68
|
-
const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
|
|
69
|
-
const schema = getFunctionParametersSchema(argumentSchemas);
|
|
70
|
-
return {
|
|
71
|
-
async [name](...unsafeArgs) {
|
|
72
|
-
const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
|
|
73
|
-
const unsafeResult = await handler(...safeArgs);
|
|
74
|
-
return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
|
|
75
|
-
}
|
|
76
|
-
}[name];
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
function getFunctionParametersSchema(argumentSchemas) {
|
|
80
|
-
const schema = {
|
|
81
|
-
factory: { type: Array },
|
|
82
|
-
properties: {}
|
|
83
|
-
};
|
|
84
|
-
argumentSchemas.forEach((arg, index) => (schema.properties[index] = arg));
|
|
85
|
-
return schema;
|
|
86
|
-
}
|
|
87
|
-
export function testSchema(schema, value, options, path = JsonPath.ROOT) {
|
|
88
|
-
initialize();
|
|
89
|
-
if (isValueSchema(schema)) {
|
|
90
|
-
return testValue(schema, value, options, path);
|
|
91
|
-
}
|
|
92
|
-
if (isTypeSchema(schema)) {
|
|
93
|
-
return testType(schema, value, options, path);
|
|
94
|
-
}
|
|
95
|
-
if (isObjectSchema(schema)) {
|
|
96
|
-
return testObject(schema, value, options, path);
|
|
97
|
-
}
|
|
98
|
-
throw new Error('Unsupported schema');
|
|
99
|
-
}
|
|
100
|
-
function testType(schema, value, options = {}, path = JsonPath.ROOT) {
|
|
101
|
-
const resolvedValueType = resolveValueType(schema.type);
|
|
102
|
-
if (isFunction(resolvedValueType)) {
|
|
103
|
-
if ((value instanceof resolvedValueType) || (getValueType(value) == resolvedValueType)) {
|
|
104
|
-
return { valid: true, value: value };
|
|
105
|
-
}
|
|
106
|
-
const objectSchema = tryGetObjectSchemaFromReflection(resolvedValueType);
|
|
107
|
-
if (isNotNull(objectSchema)) {
|
|
108
|
-
return testObject(objectSchema, value, options, path);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
else if ((resolvedValueType == 'any') || ((resolvedValueType == 'null') && isNull(value)) || ((resolvedValueType == 'undefined') && isUndefined(value))) {
|
|
112
|
-
return { valid: true, value: value };
|
|
113
|
-
}
|
|
114
|
-
return { valid: false, error: SchemaError.expectedButGot(resolvedValueType, getValueType(value), path, { fast: options.fastErrors }) };
|
|
115
|
-
}
|
|
116
|
-
// eslint-disable-next-line complexity
|
|
117
|
-
function testObject(objectSchema, value, options = {}, path = JsonPath.ROOT) {
|
|
118
|
-
if (!(value instanceof Object)) {
|
|
119
|
-
return { valid: false, error: SchemaError.expectedButGot(objectSchema.sourceType ?? 'object', getValueType(value), path, { fast: options.fastErrors }) };
|
|
120
|
-
}
|
|
121
|
-
const schema = normalizeObjectSchema(objectSchema);
|
|
122
|
-
const mask = schema.mask ?? options.mask ?? false;
|
|
123
|
-
const resultValue = isDefined(schema.factory?.type) ? new schema.factory.type() : {};
|
|
124
|
-
const schemaPropertyKeys = objectKeys(schema.properties);
|
|
125
|
-
const valuePropertyKeys = objectKeys(value);
|
|
126
|
-
const unknownValuePropertyKeys = differenceSets(new Set(valuePropertyKeys), new Set(schemaPropertyKeys));
|
|
127
|
-
if ((unknownValuePropertyKeys.length > 0) && !mask && !schema.allowUnknownProperties) {
|
|
128
|
-
return { valid: false, error: new SchemaError('Unknown property', { path: path.add(unknownValuePropertyKeys[0]), fast: options.fastErrors }) };
|
|
129
|
-
}
|
|
130
|
-
for (const key of schemaPropertyKeys) {
|
|
131
|
-
const propertyResult = testSchema(schema.properties[key], value[key], options, path.add(key));
|
|
132
|
-
if (!propertyResult.valid) {
|
|
133
|
-
return propertyResult;
|
|
134
|
-
}
|
|
135
|
-
resultValue[key] = propertyResult.value;
|
|
136
|
-
}
|
|
137
|
-
if (schema.allowUnknownProperties) {
|
|
138
|
-
for (const key of unknownValuePropertyKeys) {
|
|
139
|
-
const propertyPath = path.add(key);
|
|
140
|
-
if (isDefined(schema.unknownPropertiesKey)) {
|
|
141
|
-
const keyResult = testSchema(schema.unknownPropertiesKey, key, options);
|
|
142
|
-
if (!keyResult.valid && !mask) {
|
|
143
|
-
return { valid: false, error: new SchemaError('Invalid property key.', { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const propertyResult = testSchema(schema.unknownProperties, value[key], options, path.add(key));
|
|
147
|
-
if (!propertyResult.valid && !mask) {
|
|
148
|
-
return propertyResult;
|
|
149
|
-
}
|
|
150
|
-
resultValue[key] = propertyResult.value;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const testResultValue = isUndefined(schema.factory) ? resultValue : isDefined(schema.factory.type) ? resultValue : schema.factory.builder(resultValue);
|
|
154
|
-
return { valid: true, value: testResultValue };
|
|
155
|
-
}
|
|
156
|
-
// eslint-disable-next-line max-lines-per-function, max-statements, complexity
|
|
157
|
-
function testValue(schema, value, options = {}, path = JsonPath.ROOT) {
|
|
158
|
-
const normalizedValueSchema = normalizeValueSchema(schema);
|
|
159
|
-
if (normalizedValueSchema.optional && isUndefined(value)) {
|
|
160
|
-
return { valid: true, value: value };
|
|
161
|
-
}
|
|
162
|
-
if (normalizedValueSchema.nullable && isNull(value)) {
|
|
163
|
-
return { valid: true, value: value };
|
|
164
|
-
}
|
|
165
|
-
const context = {
|
|
166
|
-
schema: normalizedValueSchema,
|
|
167
|
-
options
|
|
168
|
-
};
|
|
169
|
-
/** handle arrays */
|
|
170
|
-
if (normalizedValueSchema.array) {
|
|
171
|
-
if (!isArray(value)) {
|
|
172
|
-
if (normalizedValueSchema.coerce) {
|
|
173
|
-
return testValue(schema, [value], options, path);
|
|
174
|
-
}
|
|
175
|
-
return { valid: false, error: SchemaError.expectedButGot(Array, getValueType(value), path, { fast: options.fastErrors }) };
|
|
176
|
-
}
|
|
177
|
-
for (const arrayConstraint of normalizedValueSchema.arrayConstraints) {
|
|
178
|
-
const result = arrayConstraint.validate(value, path, context);
|
|
179
|
-
if (!result.valid) {
|
|
180
|
-
return { valid: false, error: result.error };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
const itemSchema = getArrayItemSchema(schema);
|
|
184
|
-
const validatedItems = [];
|
|
185
|
-
for (let i = 0; i < value.length; i++) {
|
|
186
|
-
const result = testValue(itemSchema, value[i], options, path.add(i));
|
|
187
|
-
if (!result.valid) {
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
|
-
validatedItems.push(result.value);
|
|
191
|
-
}
|
|
192
|
-
return { valid: true, value: validatedItems };
|
|
193
|
-
}
|
|
194
|
-
let valueTestResult;
|
|
195
|
-
let resultValue;
|
|
196
|
-
let valueType;
|
|
197
|
-
function updateCurrentState(newValue) {
|
|
198
|
-
valueTestResult = isValidValue(normalizedValueSchema.schema, newValue, options, path);
|
|
199
|
-
resultValue = valueTestResult.valid ? valueTestResult.value : newValue;
|
|
200
|
-
valueType = getValueType(resultValue);
|
|
201
|
-
}
|
|
202
|
-
updateCurrentState(value);
|
|
203
|
-
/** try to coerce */
|
|
204
|
-
if (!valueTestResult.valid) {
|
|
205
|
-
const targetTypes = getSchemaValueTypes(schema);
|
|
206
|
-
const coercers = [
|
|
207
|
-
...(normalizedValueSchema.coercers.get(valueType) ?? []),
|
|
208
|
-
...((normalizedValueSchema.coerce || options.coerce == true) ? (defaultCoercers.get(valueType) ?? []) : [])
|
|
209
|
-
]
|
|
210
|
-
.filter((coercer) => includesValueType(coercer.targetType, targetTypes));
|
|
211
|
-
const errors = [];
|
|
212
|
-
let success = false;
|
|
213
|
-
for (const coercer of coercers) {
|
|
214
|
-
const coerceResult = coercer.coerce(resultValue, path, context);
|
|
215
|
-
if (!coerceResult.success) {
|
|
216
|
-
errors.push(coerceResult.error);
|
|
217
|
-
continue;
|
|
218
|
-
}
|
|
219
|
-
success = true;
|
|
220
|
-
updateCurrentState(coerceResult.value);
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
if (!success && (errors.length > 0)) {
|
|
224
|
-
return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value could not be coerced.', { inner: errors, path, fast: options.fastErrors }) };
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!valueTestResult.valid) {
|
|
228
|
-
return valueTestResult;
|
|
229
|
-
}
|
|
230
|
-
if (normalizedValueSchema.valueConstraints.length > 0) {
|
|
231
|
-
const errors = [];
|
|
232
|
-
for (const constraint of normalizedValueSchema.valueConstraints) {
|
|
233
|
-
const result = constraint.validate(resultValue, path, context);
|
|
234
|
-
if (!result.valid) {
|
|
235
|
-
errors.push(result.error);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (errors.length > 0) {
|
|
239
|
-
return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value did not match schema.', { path, inner: errors, fast: options.fastErrors }) };
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (normalizedValueSchema.transformers.length > 0) {
|
|
243
|
-
for (const transformer of normalizedValueSchema.transformers) {
|
|
244
|
-
if (isDefined(transformer.sourceType) && !includesValueType(valueType, resolveValueTypes(transformer.sourceType))) {
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
const transformResult = transformer.transform(resultValue, path, context);
|
|
248
|
-
if (isTransformErrorResult(transformResult)) {
|
|
249
|
-
return { valid: false, error: transformResult[transformErrorResultSymbol] };
|
|
250
|
-
}
|
|
251
|
-
return { valid: true, value: transformResult };
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return { valid: true, value: resultValue };
|
|
255
|
-
}
|
|
256
|
-
function isValidValue(validSchemas, value, options, path) {
|
|
257
|
-
const errorResults = [];
|
|
258
|
-
for (const schema of validSchemas) {
|
|
259
|
-
const result = testSchema(schema, value, options, path);
|
|
260
|
-
if (result.valid) {
|
|
261
|
-
return result;
|
|
262
|
-
}
|
|
263
|
-
errorResults.push(result);
|
|
264
|
-
}
|
|
265
|
-
if (errorResults.length == 1) {
|
|
266
|
-
return errorResults[0];
|
|
267
|
-
}
|
|
268
|
-
const errors = errorResults.map((result) => result.error);
|
|
269
|
-
const expectStrings = [];
|
|
270
|
-
for (const schema of validSchemas) {
|
|
271
|
-
expectStrings.push(getExpectString(schema));
|
|
272
|
-
}
|
|
273
|
-
const expectString = expectStrings.length > 1 ? `(${expectStrings.join(' | ')})` : expectStrings[0];
|
|
274
|
-
return { valid: false, error: SchemaError.expectedButGot(expectString, getValueType(value), path, { inner: errors, fast: options.fastErrors }) };
|
|
275
|
-
}
|
|
276
|
-
export function getExpectString(schema) {
|
|
277
|
-
const expectedNames = getSchemaTypeNames(schema);
|
|
278
|
-
const arraySuffix = (isValueSchema(schema) && (schema.array == true)) ? '[]' : '';
|
|
279
|
-
const expectedTypeString = (expectedNames.length == 1) ? `${expectedNames[0]}${arraySuffix}` : `(${expectedNames.join(' | ')})${arraySuffix}`;
|
|
280
|
-
const expects = isValueSchema(schema) ? toArray(schema.valueConstraints ?? []).map((constraint) => constraint.expects) : [];
|
|
281
|
-
const expectsString = (expects.length > 0) ? `[${expects.join(', ')}]` : '';
|
|
282
|
-
return `${expectedTypeString}${expectsString}`;
|
|
283
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
|
|
3
|
-
export type AnyOptions = ValueSchemaOptions;
|
|
4
|
-
export declare function any(options?: AnyOptions): ValueSchema<any>;
|
|
5
|
-
export declare function Any(options?: AnyOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
|
|
3
|
-
import { valueSchema } from '../types/index.js';
|
|
4
|
-
export function any(options) {
|
|
5
|
-
return valueSchema('any', options);
|
|
6
|
-
}
|
|
7
|
-
export function Any(options) {
|
|
8
|
-
return createSchemaPropertyDecoratorFromSchema(any(options));
|
|
9
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { OneOrMany } from '../../types.js';
|
|
3
|
-
import type { SchemaTestable } from '../schema.js';
|
|
4
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
|
|
5
|
-
export type ArrayOptions = ValueSchemaOptions & {
|
|
6
|
-
/** minimum array length */
|
|
7
|
-
minimumLength?: number;
|
|
8
|
-
/** maximum array length */
|
|
9
|
-
maximumLength?: number;
|
|
10
|
-
};
|
|
11
|
-
export declare function array<T>(innerValues: OneOrMany<SchemaTestable<T>>, options?: ArrayOptions): ValueSchema<T[]>;
|
|
12
|
-
export declare function Array(innerValues: OneOrMany<SchemaTestable>, options?: ArrayOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { toArrayCopy } from '../../utils/array/array.js';
|
|
3
|
-
import { isDefined } from '../../utils/type-guards.js';
|
|
4
|
-
import { ArrayMaximumLengthConstraint } from '../array-constraints/maximum-length.js';
|
|
5
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
|
|
6
|
-
import { valueSchema } from '../types/types.js';
|
|
7
|
-
export function array(innerValues, options = {}) {
|
|
8
|
-
const arrayConstraints = toArrayCopy(options.arrayConstraints ?? []);
|
|
9
|
-
if (isDefined(options.minimumLength)) {
|
|
10
|
-
arrayConstraints.push(new ArrayMaximumLengthConstraint(options.minimumLength));
|
|
11
|
-
}
|
|
12
|
-
if (isDefined(options.maximumLength)) {
|
|
13
|
-
arrayConstraints.push(new ArrayMaximumLengthConstraint(options.maximumLength));
|
|
14
|
-
}
|
|
15
|
-
return valueSchema(innerValues, {
|
|
16
|
-
...options,
|
|
17
|
-
array: true,
|
|
18
|
-
arrayConstraints
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
export function Array(innerValues, options) {
|
|
22
|
-
return createSchemaPropertyDecoratorFromSchema(array(innerValues, options));
|
|
23
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Merge } from '../../types.js';
|
|
2
|
-
import type { ObjectSchema, ObjectSchemaOrType } from '../types/index.js';
|
|
3
|
-
export declare function assign<T1, T2>(schema1: ObjectSchemaOrType<T1>, schema2: ObjectSchemaOrType<T2>): ObjectSchema<Merge<T1, T2>>;
|
|
4
|
-
export declare function assign<T1, T2, T3>(schema1: ObjectSchemaOrType<T1>, schema2: ObjectSchemaOrType<T2>, schema3: ObjectSchemaOrType<T3>): ObjectSchema<Merge<Merge<T1, T2>, T3>>;
|
|
5
|
-
export declare function assign<T1, T2, T3, T4>(schema1: ObjectSchemaOrType<T1>, schema2: ObjectSchemaOrType<T2>, schema3: ObjectSchemaOrType<T3>, schema4: ObjectSchemaOrType<T4>): ObjectSchema<Merge<Merge<T1, T2>, Merge<T3, T4>>>;
|
|
6
|
-
export declare function assign<T1, T2, T3, T4, T5>(schema1: ObjectSchemaOrType<T1>, schema2: ObjectSchemaOrType<T2>, schema3: ObjectSchemaOrType<T3>, schema4: ObjectSchemaOrType<T4>, schema5: ObjectSchemaOrType<T5>): ObjectSchema<Merge<Merge<Merge<T1, T2>, Merge<T3, T4>>, T5>>;
|
|
7
|
-
export declare function assign<T1, T2, T3, T4, T5, T6>(schema1: ObjectSchemaOrType<T1>, schema2: ObjectSchemaOrType<T2>, schema3: ObjectSchemaOrType<T3>, schema4: ObjectSchemaOrType<T4>, schema5: ObjectSchemaOrType<T5>, schema6: ObjectSchemaOrType<T6>): ObjectSchema<Merge<Merge<Merge<T1, T2>, Merge<T3, T4>>, Merge<T5, T6>>>;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { toArray } from '../../utils/array/array.js';
|
|
2
|
-
import { assert } from '../../utils/type-guards.js';
|
|
3
|
-
import { objectSchema } from '../types/index.js';
|
|
4
|
-
import { getObjectSchema } from '../utils/index.js';
|
|
5
|
-
export function assign(...inputs) {
|
|
6
|
-
const schemas = inputs.map(getObjectSchema);
|
|
7
|
-
assert(schemas.length >= 2, 'Assign requires at least 2 schemas.');
|
|
8
|
-
let result = schemas[0];
|
|
9
|
-
for (let i = 1; i < schemas.length; i++) {
|
|
10
|
-
result = {
|
|
11
|
-
...result,
|
|
12
|
-
...schemas[i],
|
|
13
|
-
properties: {
|
|
14
|
-
...result.properties,
|
|
15
|
-
...schemas[i].properties
|
|
16
|
-
},
|
|
17
|
-
unknownProperties: [...toArray(result.unknownProperties ?? []), ...toArray(schemas[i].unknownProperties ?? [])],
|
|
18
|
-
unknownPropertiesKey: [...toArray(result.unknownPropertiesKey ?? []), ...toArray(schemas[i].unknownPropertiesKey ?? [])]
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
result.sourceType = undefined;
|
|
22
|
-
result.factory = undefined;
|
|
23
|
-
return objectSchema(result);
|
|
24
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
|
|
3
|
-
export type BooleanOptions = ValueSchemaOptions;
|
|
4
|
-
export declare function boolean(options?: BooleanOptions): ValueSchema<boolean>;
|
|
5
|
-
export declare function BooleanProperty(options?: BooleanOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
|
|
3
|
-
import { valueSchema } from '../types/index.js';
|
|
4
|
-
export function boolean(options = {}) {
|
|
5
|
-
return valueSchema(Boolean, options);
|
|
6
|
-
}
|
|
7
|
-
export function BooleanProperty(options) {
|
|
8
|
-
return createSchemaPropertyDecoratorFromSchema(boolean(options));
|
|
9
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { OneOrMany } from '../../types.js';
|
|
2
|
-
import type { GenericConstraintFunction } from '../constraints/generic.js';
|
|
3
|
-
import type { SchemaTestable } from '../schema.js';
|
|
4
|
-
import type { ValueSchema } from '../types/index.js';
|
|
5
|
-
export declare function constraint<T>(schema: SchemaTestable<T>, constraintFunction: GenericConstraintFunction<T>, expects?: OneOrMany<string>): ValueSchema<T>;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { GenericConstraint } from '../constraints/generic.js';
|
|
2
|
-
import { valueSchema } from '../types/index.js';
|
|
3
|
-
export function constraint(schema, constraintFunction, expects) {
|
|
4
|
-
return valueSchema(schema, {
|
|
5
|
-
valueConstraints: new GenericConstraint(constraintFunction, expects)
|
|
6
|
-
});
|
|
7
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
|
|
3
|
-
export type DateOptions = ValueSchemaOptions & {
|
|
4
|
-
minimum?: Date | number;
|
|
5
|
-
maximum?: Date | number;
|
|
6
|
-
};
|
|
7
|
-
export declare function date(options?: DateOptions): ValueSchema<Date>;
|
|
8
|
-
export declare function DateProperty(options?: DateOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { toArrayCopy } from '../../utils/array/array.js';
|
|
3
|
-
import { isDefined } from '../../utils/type-guards.js';
|
|
4
|
-
import { MaximumDateConstraint, MinimumDateConstraint } from '../constraints/index.js';
|
|
5
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
|
|
6
|
-
import { valueSchema } from '../types/types.js';
|
|
7
|
-
export function date(options = {}) {
|
|
8
|
-
const constraints = toArrayCopy(options.valueConstraints ?? []);
|
|
9
|
-
if (isDefined(options.minimum)) {
|
|
10
|
-
constraints.push(new MinimumDateConstraint(options.minimum));
|
|
11
|
-
}
|
|
12
|
-
if (isDefined(options.maximum)) {
|
|
13
|
-
constraints.push(new MaximumDateConstraint(options.maximum));
|
|
14
|
-
}
|
|
15
|
-
return valueSchema(Date, {
|
|
16
|
-
...options,
|
|
17
|
-
valueConstraints: constraints
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
export function DateProperty(options) {
|
|
21
|
-
return createSchemaPropertyDecoratorFromSchema(date(options));
|
|
22
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { OneOrMany } from '../../types.js';
|
|
3
|
-
import type { SchemaTestable } from '../schema.js';
|
|
4
|
-
import type { ValueSchema } from '../types/index.js';
|
|
5
|
-
export declare function defaulted<T, Default>(type: OneOrMany<SchemaTestable<T>>, defaultValue: Default): ValueSchema<NonNullable<T> | Default>;
|
|
6
|
-
export declare function Defaulted(type: OneOrMany<SchemaTestable>, defaultValue: any): Decorator<'property' | 'accessor'>;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
|
|
3
|
-
import { valueSchema } from '../types/index.js';
|
|
4
|
-
import { transform } from './transform.js';
|
|
5
|
-
export function defaulted(type, defaultValue) {
|
|
6
|
-
return transform(valueSchema(type, { optional: true, nullable: true }), (value) => value ?? defaultValue); // eslint-disable-line @typescript-eslint/no-unnecessary-condition
|
|
7
|
-
}
|
|
8
|
-
export function Defaulted(type, defaultValue) {
|
|
9
|
-
return createSchemaPropertyDecoratorFromSchema(defaulted(type, defaultValue));
|
|
10
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { Enumeration as EnumerationType, EnumerationValue } from '../../types.js';
|
|
3
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
|
|
4
|
-
export type EnumerationOptions = ValueSchemaOptions;
|
|
5
|
-
export declare function enumeration<T extends EnumerationType>(enumerationValue: T, options?: EnumerationOptions): ValueSchema<EnumerationValue<T>>;
|
|
6
|
-
export declare function Enumeration(enumerationValue: EnumerationType, options?: EnumerationOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { toArrayCopy } from '../../utils/array/array.js';
|
|
3
|
-
import { EnumerationConstraint } from '../constraints/index.js';
|
|
4
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
|
|
5
|
-
import { valueSchema } from '../types/types.js';
|
|
6
|
-
export function enumeration(enumerationValue, options) {
|
|
7
|
-
const valueConstraints = toArrayCopy(options?.valueConstraints ?? []);
|
|
8
|
-
const enumerationConstraint = new EnumerationConstraint(enumerationValue);
|
|
9
|
-
valueConstraints.push(enumerationConstraint);
|
|
10
|
-
return valueSchema(enumerationConstraint.suitableTypes, {
|
|
11
|
-
...options,
|
|
12
|
-
valueConstraints
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export function Enumeration(enumerationValue, options) {
|
|
16
|
-
return createSchemaPropertyDecoratorFromSchema(enumeration(enumerationValue, options));
|
|
17
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export * from './any.js';
|
|
2
|
-
export * from './array.js';
|
|
3
|
-
export * from './assign.js';
|
|
4
|
-
export * from './boolean.js';
|
|
5
|
-
export * from './constraint.js';
|
|
6
|
-
export * from './date.js';
|
|
7
|
-
export * from './defaulted.js';
|
|
8
|
-
export * from './enumeration.js';
|
|
9
|
-
export * from './instance.js';
|
|
10
|
-
export * from './literal.js';
|
|
11
|
-
export * from './nullable.js';
|
|
12
|
-
export * from './number.js';
|
|
13
|
-
export * from './object.js';
|
|
14
|
-
export * from './omit.js';
|
|
15
|
-
export * from './one-or-many.js';
|
|
16
|
-
export * from './optional.js';
|
|
17
|
-
export * from './partial.js';
|
|
18
|
-
export * from './pick.js';
|
|
19
|
-
export * from './readable-stream.js';
|
|
20
|
-
export * from './record.js';
|
|
21
|
-
export * from './regexp.js';
|
|
22
|
-
export * from './string.js';
|
|
23
|
-
export * from './transform.js';
|
|
24
|
-
export * from './uint8-array.js';
|
|
25
|
-
export * from './union.js';
|
|
26
|
-
export * from './unknown.js';
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export * from './any.js';
|
|
2
|
-
export * from './array.js';
|
|
3
|
-
export * from './assign.js';
|
|
4
|
-
export * from './boolean.js';
|
|
5
|
-
export * from './constraint.js';
|
|
6
|
-
export * from './date.js';
|
|
7
|
-
export * from './defaulted.js';
|
|
8
|
-
export * from './enumeration.js';
|
|
9
|
-
export * from './instance.js';
|
|
10
|
-
export * from './literal.js';
|
|
11
|
-
export * from './nullable.js';
|
|
12
|
-
export * from './number.js';
|
|
13
|
-
export * from './object.js';
|
|
14
|
-
export * from './omit.js';
|
|
15
|
-
export * from './one-or-many.js';
|
|
16
|
-
export * from './optional.js';
|
|
17
|
-
export * from './partial.js';
|
|
18
|
-
export * from './pick.js';
|
|
19
|
-
export * from './readable-stream.js';
|
|
20
|
-
export * from './record.js';
|
|
21
|
-
export * from './regexp.js';
|
|
22
|
-
export * from './string.js';
|
|
23
|
-
export * from './transform.js';
|
|
24
|
-
export * from './uint8-array.js';
|
|
25
|
-
export * from './union.js';
|
|
26
|
-
export * from './unknown.js';
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
|
|
3
|
-
export type LiteralOptions = ValueSchemaOptions;
|
|
4
|
-
export declare function literal<const T>(value: T, options?: LiteralOptions): ValueSchema<T>;
|
|
5
|
-
export declare function Literal(value: any): Decorator<'property' | 'accessor'>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { toArrayCopy } from '../../utils/array/array.js';
|
|
3
|
-
import { LiteralConstraint } from '../constraints/index.js';
|
|
4
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
|
|
5
|
-
import { valueSchema } from '../types/types.js';
|
|
6
|
-
import { getValueType } from '../utils/value-type.js';
|
|
7
|
-
export function literal(value, options) {
|
|
8
|
-
const valueConstraints = toArrayCopy(options?.valueConstraints ?? []);
|
|
9
|
-
valueConstraints.push(new LiteralConstraint(value));
|
|
10
|
-
return valueSchema(getValueType(value), {
|
|
11
|
-
...options,
|
|
12
|
-
valueConstraints
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export function Literal(value) {
|
|
16
|
-
return createSchemaPropertyDecoratorFromSchema(literal(value));
|
|
17
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { SchemaTestable } from '../schema.js';
|
|
3
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
|
|
4
|
-
export type NullableOptions = ValueSchemaOptions;
|
|
5
|
-
export declare function nullable<T>(schema: SchemaTestable<T>, options?: NullableOptions): ValueSchema<T | null>;
|
|
6
|
-
export declare function Nullable(options?: NullableOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { createSchemaPropertyDecorator } from '../decorators/utils.js';
|
|
2
|
-
import { valueSchema } from '../types/index.js';
|
|
3
|
-
export function nullable(schema, options) {
|
|
4
|
-
return valueSchema(schema, { ...options, nullable: true });
|
|
5
|
-
}
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
7
|
-
export function Nullable(options) {
|
|
8
|
-
return createSchemaPropertyDecorator({ ...options, nullable: true });
|
|
9
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
-
import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
|
|
3
|
-
export type NumberOptions = ValueSchemaOptions & {
|
|
4
|
-
minimum?: number;
|
|
5
|
-
maximum?: number;
|
|
6
|
-
integer?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export declare function number(options?: NumberOptions): ValueSchema<number>;
|
|
9
|
-
export declare function NumberProperty(options?: NumberOptions): Decorator<'property' | 'accessor'>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { toArrayCopy } from '../../utils/array/array.js';
|
|
3
|
-
import { isDefined } from '../../utils/type-guards.js';
|
|
4
|
-
import { integerConstraint } from '../constraints/integer.js';
|
|
5
|
-
import { MaximumConstraint } from '../constraints/maximum.js';
|
|
6
|
-
import { MinimumConstraint } from '../constraints/minimum.js';
|
|
7
|
-
import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
|
|
8
|
-
import { valueSchema } from '../types/types.js';
|
|
9
|
-
export function number(options = {}) {
|
|
10
|
-
const valueConstraints = toArrayCopy(options.valueConstraints ?? []);
|
|
11
|
-
if (isDefined(options.minimum)) {
|
|
12
|
-
valueConstraints.push(new MinimumConstraint(options.minimum));
|
|
13
|
-
}
|
|
14
|
-
if (isDefined(options.maximum)) {
|
|
15
|
-
valueConstraints.push(new MaximumConstraint(options.maximum));
|
|
16
|
-
}
|
|
17
|
-
if (options.integer == true) {
|
|
18
|
-
valueConstraints.push(integerConstraint);
|
|
19
|
-
}
|
|
20
|
-
return valueSchema(Number, {
|
|
21
|
-
...options,
|
|
22
|
-
valueConstraints
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
export function NumberProperty(options) {
|
|
26
|
-
return createSchemaPropertyDecoratorFromSchema(number(options));
|
|
27
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ObjectLiteral, Optionalize, Record, SimplifyObject, TypedOmit } from '../../types.js';
|
|
2
|
-
import type { IfNever } from 'type-fest';
|
|
3
|
-
import { type ObjectSchema, type ObjectSchemaProperties, type TypedObjectSchemaUnknownProperties } from '../types/index.js';
|
|
4
|
-
export type ObjectOptions<T extends Record = Record> = TypedOmit<ObjectSchema<T>, 'properties' | 'unknownProperties' | 'unknownPropertiesKey'>;
|
|
5
|
-
export declare function explicitObject<T extends Record>(properties: ObjectSchemaProperties<T>, options?: ObjectOptions<T>): ObjectSchema<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<Optionalize<T> & IfNever<V, ObjectLiteral, Record<K, V>>>>;
|
|
7
|
-
export declare const emptyObjectSchema: ObjectSchema<ObjectLiteral>;
|