@travetto/schema 7.0.0-rc.1 → 7.0.0-rc.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/README.md +31 -31
- package/package.json +3 -3
- package/src/bind-util.ts +90 -90
- package/src/data.ts +40 -39
- package/src/decorator/common.ts +5 -5
- package/src/decorator/field.ts +9 -7
- package/src/decorator/input.ts +11 -9
- package/src/decorator/method.ts +3 -1
- package/src/decorator/schema.ts +2 -2
- package/src/internal/types.ts +3 -3
- package/src/name.ts +3 -3
- package/src/service/changes.ts +33 -33
- package/src/service/registry-adapter.ts +58 -58
- package/src/service/registry-index.ts +19 -19
- package/src/service/types.ts +5 -5
- package/src/types.ts +1 -1
- package/src/validate/error.ts +2 -2
- package/src/validate/regexp.ts +4 -4
- package/src/validate/validator.ts +75 -75
- package/support/transformer/util.ts +26 -23
- package/support/transformer.schema.ts +7 -7
package/README.md
CHANGED
|
@@ -64,28 +64,28 @@ User:
|
|
|
64
64
|
|
|
65
65
|
### Fields
|
|
66
66
|
This schema provides a powerful base for data binding and validation at runtime. Additionally there may be types that cannot be detected, or some information that the programmer would like to override. Below are the supported field decorators:
|
|
67
|
-
* [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#
|
|
68
|
-
* [@Required](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
69
|
-
* [@Enum](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
70
|
-
* [@Match](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
71
|
-
* [@MinLength](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
72
|
-
* [@MaxLength](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
73
|
-
* [@Min](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
74
|
-
* [@Max](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
75
|
-
* [@Email](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
76
|
-
* [@Telephone](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
77
|
-
* [@Url](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
67
|
+
* [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L23) defines a field that will be serialized.
|
|
68
|
+
* [@Required](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L49) defines a that field should be required
|
|
69
|
+
* [@Enum](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L58) defines the allowable values that a field can have
|
|
70
|
+
* [@Match](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L84) defines a regular expression that the field value should match
|
|
71
|
+
* [@MinLength](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L93) enforces min length of a string
|
|
72
|
+
* [@MaxLength](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L104) enforces max length of a string
|
|
73
|
+
* [@Min](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L93) enforces min value for a date or a number
|
|
74
|
+
* [@Max](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L104) enforces max value for a date or a number
|
|
75
|
+
* [@Email](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L134) ensures string field matches basic email regex
|
|
76
|
+
* [@Telephone](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L142) ensures string field matches basic telephone regex
|
|
77
|
+
* [@Url](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L150) ensures string field matches basic url regex
|
|
78
78
|
* [@Ignore](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/common.ts#L41) exclude from auto schema registration
|
|
79
|
-
* [@Integer](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
80
|
-
* [@Float](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
81
|
-
* [@Currency](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
82
|
-
* [@Text](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
83
|
-
* [@LongText](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
84
|
-
* [@Readonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#
|
|
85
|
-
* [@Writeonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#
|
|
86
|
-
* [@Secret](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#
|
|
87
|
-
* [@Specifier](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
88
|
-
* [@DiscriminatorField](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#
|
|
79
|
+
* [@Integer](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L166) ensures number passed in is only a whole number
|
|
80
|
+
* [@Float](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L173) ensures number passed in allows fractional values
|
|
81
|
+
* [@Currency](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L187) provides support for standard currency
|
|
82
|
+
* [@Text](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L68) indicates that a field is expecting natural language input, not just discrete values
|
|
83
|
+
* [@LongText](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L75) same as text, but expects longer form content
|
|
84
|
+
* [@Readonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L39) defines a that field should not be bindable external to the class
|
|
85
|
+
* [@Writeonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L32) defines a that field should not be exported in serialization, but that it can be bound to
|
|
86
|
+
* [@Secret](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L47) marks a field as being sensitive. This is used by certain logging activities to ensure sensitive information is not logged out.
|
|
87
|
+
* [@Specifier](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L195) attributes additional specifiers to a field, allowing for more specification beyond just the field's type.
|
|
88
|
+
* [@DiscriminatorField](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/input.ts#L202) allows for promoting a given field as the owner of the sub type discriminator.
|
|
89
89
|
|
|
90
90
|
Additionally, schemas can be nested to form more complex data structures that are able to bound and validated.
|
|
91
91
|
|
|
@@ -249,11 +249,11 @@ Within the schema framework, it is possible to add custom validators class level
|
|
|
249
249
|
import { Schema, Validator, ValidationError } from '@travetto/schema';
|
|
250
250
|
|
|
251
251
|
const passwordValidator = (user: User): ValidationError | undefined => {
|
|
252
|
-
const
|
|
253
|
-
const
|
|
254
|
-
const hasSpecial = /[!@#$%%^&*()<>?/,.;':"']/.test(
|
|
255
|
-
const noRepeat = !/(.)(\1)/.test(
|
|
256
|
-
if (!
|
|
252
|
+
const password = user.password;
|
|
253
|
+
const hasNumber = /\d/.test(password);
|
|
254
|
+
const hasSpecial = /[!@#$%%^&*()<>?/,.;':"']/.test(password);
|
|
255
|
+
const noRepeat = !/(.)(\1)/.test(password);
|
|
256
|
+
if (!hasNumber || !hasSpecial || !noRepeat) {
|
|
257
257
|
return {
|
|
258
258
|
kind: 'password-rules',
|
|
259
259
|
path: 'password',
|
|
@@ -322,7 +322,7 @@ This feature is meant to allow for simple Typescript types to be able to be back
|
|
|
322
322
|
/**
|
|
323
323
|
* Geometric Point as [number,number] with validation and binding support
|
|
324
324
|
*
|
|
325
|
-
* @concrete ./internal/types.ts#
|
|
325
|
+
* @concrete ./internal/types.ts#PointImplementation
|
|
326
326
|
*/
|
|
327
327
|
export type Point = [number, number];
|
|
328
328
|
```
|
|
@@ -336,7 +336,7 @@ const InvalidSymbol = Symbol();
|
|
|
336
336
|
/**
|
|
337
337
|
* Point Implementation
|
|
338
338
|
*/
|
|
339
|
-
export class
|
|
339
|
+
export class PointImplementation {
|
|
340
340
|
|
|
341
341
|
/**
|
|
342
342
|
* Validate we have an actual point
|
|
@@ -351,7 +351,7 @@ export class PointImpl {
|
|
|
351
351
|
*/
|
|
352
352
|
static bindSchema(input: unknown): [number, number] | typeof InvalidSymbol | undefined {
|
|
353
353
|
if (Array.isArray(input) && input.length === 2) {
|
|
354
|
-
const [a, b] = input.map(
|
|
354
|
+
const [a, b] = input.map(value => DataUtil.coerceType(value, Number, false));
|
|
355
355
|
return [a, b];
|
|
356
356
|
} else {
|
|
357
357
|
return InvalidSymbol;
|
|
@@ -359,7 +359,7 @@ export class PointImpl {
|
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
-
Object.defineProperty(
|
|
362
|
+
Object.defineProperty(PointImplementation, 'name', { value: 'Point' });
|
|
363
363
|
```
|
|
364
364
|
|
|
365
365
|
What you can see here is that the `Point` type is now backed by a class that supports:
|
|
@@ -410,4 +410,4 @@ Data utilities for binding values, and type conversion. Currently [DataUtil](htt
|
|
|
410
410
|
|
|
411
411
|
* `coerceType(input: unknown, type: Class<unknown>, strict = true)` which allows for converting an input type into a specified `type` instance, or throw an error if the types are incompatible.
|
|
412
412
|
* `shallowClone<T = unknown>(a: T): T` will shallowly clone a field
|
|
413
|
-
* `filterByKeys<T>(
|
|
413
|
+
* `filterByKeys<T>(input: T, exclude: (string | RegExp)[]): T` will filter a given object, and return a plain object (if applicable) with fields excluded using the values in the `exclude` input
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
4
4
|
"description": "Data type registry for runtime validation, reflection and binding.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"directory": "module/schema"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/registry": "^7.0.0-rc.
|
|
30
|
+
"@travetto/registry": "^7.0.0-rc.2"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^7.0.0-rc.
|
|
33
|
+
"@travetto/transformer": "^7.0.0-rc.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/bind-util.ts
CHANGED
|
@@ -10,8 +10,8 @@ type BindConfig = {
|
|
|
10
10
|
filterValue?: (value: unknown, input: SchemaInputConfig) => boolean;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
function isInstance<T>(
|
|
14
|
-
return !!
|
|
13
|
+
function isInstance<T>(value: unknown): value is T {
|
|
14
|
+
return !!value && !DataUtil.isPrimitive(value);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -21,24 +21,24 @@ export class BindUtil {
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Coerce a value to match the field config type
|
|
24
|
-
* @param
|
|
25
|
-
* @param
|
|
24
|
+
* @param config The field config to coerce to
|
|
25
|
+
* @param value The provided value
|
|
26
26
|
*/
|
|
27
|
-
static #coerceType<T>(
|
|
28
|
-
if (
|
|
29
|
-
|
|
27
|
+
static #coerceType<T>(config: SchemaInputConfig, value: unknown): T | null | undefined {
|
|
28
|
+
if (config.type?.bindSchema) {
|
|
29
|
+
value = config.type.bindSchema(value);
|
|
30
30
|
} else {
|
|
31
|
-
|
|
31
|
+
value = DataUtil.coerceType(value, config.type, false);
|
|
32
32
|
|
|
33
|
-
if (
|
|
34
|
-
if (
|
|
35
|
-
|
|
33
|
+
if (config.type === Number && config.precision && typeof value === 'number') {
|
|
34
|
+
if (config.precision[1]) { // Supports decimal
|
|
35
|
+
value = +value.toFixed(config.precision[1]);
|
|
36
36
|
} else { // 0 digits
|
|
37
|
-
|
|
37
|
+
value = Math.trunc(value);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
return castTo(
|
|
41
|
+
return castTo(value);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
@@ -46,22 +46,22 @@ export class BindUtil {
|
|
|
46
46
|
*
|
|
47
47
|
* This will convert `{ 'a.b[3].c[age]': 5 }` => `{ a : { b : [,,,{ c: { age: 5 }}]}}`
|
|
48
48
|
*
|
|
49
|
-
* @param
|
|
49
|
+
* @param input The object to convert
|
|
50
50
|
*/
|
|
51
|
-
static expandPaths(
|
|
51
|
+
static expandPaths(input: Record<string, unknown>): Record<string, unknown> {
|
|
52
52
|
const out: Record<string, unknown> = {};
|
|
53
|
-
for (const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const parts =
|
|
53
|
+
for (const property of Object.keys(input)) {
|
|
54
|
+
const valueInput = input[property];
|
|
55
|
+
const value = DataUtil.isPlainObject(valueInput) ? this.expandPaths(valueInput) : valueInput;
|
|
56
|
+
const parts = property.split('.');
|
|
57
57
|
const last = parts.pop()!;
|
|
58
58
|
let sub = out;
|
|
59
59
|
while (parts.length > 0) {
|
|
60
60
|
const part = parts.shift()!;
|
|
61
|
-
const
|
|
61
|
+
const partArrayIndex = part.indexOf('[') > 0;
|
|
62
62
|
const name = part.split(/[^A-Za-z_0-9]/)[0];
|
|
63
|
-
const idx =
|
|
64
|
-
const key =
|
|
63
|
+
const idx = partArrayIndex ? part.split(/[\[\]]/)[1] : '';
|
|
64
|
+
const key = partArrayIndex ? (/^\d+$/.test(idx) ? parseInt(idx, 10) : (idx.trim() || undefined)) : undefined;
|
|
65
65
|
|
|
66
66
|
if (!(name in sub)) {
|
|
67
67
|
sub[name] = typeof key === 'number' ? [] : {};
|
|
@@ -77,10 +77,10 @@ export class BindUtil {
|
|
|
77
77
|
const arr = last.indexOf('[') > 0;
|
|
78
78
|
|
|
79
79
|
if (!arr) {
|
|
80
|
-
if (sub[last] && DataUtil.isPlainObject(
|
|
81
|
-
sub[last] = DataUtil.deepAssign(sub[last],
|
|
80
|
+
if (sub[last] && DataUtil.isPlainObject(value)) {
|
|
81
|
+
sub[last] = DataUtil.deepAssign(sub[last], value, 'coerce');
|
|
82
82
|
} else {
|
|
83
|
-
sub[last] =
|
|
83
|
+
sub[last] = value;
|
|
84
84
|
}
|
|
85
85
|
} else {
|
|
86
86
|
const name = last.split(/[^A-Za-z_0-9]/)[0];
|
|
@@ -93,10 +93,10 @@ export class BindUtil {
|
|
|
93
93
|
if (key === undefined) {
|
|
94
94
|
key = arrSub.length;
|
|
95
95
|
}
|
|
96
|
-
if (arrSub[key] && DataUtil.isPlainObject(
|
|
97
|
-
arrSub[key] = DataUtil.deepAssign(arrSub[key],
|
|
96
|
+
if (arrSub[key] && DataUtil.isPlainObject(value) && DataUtil.isPlainObject(arrSub[key])) {
|
|
97
|
+
arrSub[key] = DataUtil.deepAssign(arrSub[key], value, 'coerce');
|
|
98
98
|
} else {
|
|
99
|
-
arrSub[key] =
|
|
99
|
+
arrSub[key] = value;
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -105,8 +105,8 @@ export class BindUtil {
|
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* Convert full object with nesting, into flat set of keys
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
108
|
+
* @param data The object to flatten the paths for
|
|
109
|
+
* @param prefix The starting prefix
|
|
110
110
|
*/
|
|
111
111
|
static flattenPaths<V extends string = string>(data: Record<string, unknown>, prefix: string = ''): Record<string, V> {
|
|
112
112
|
const out: Record<string, V> = {};
|
|
@@ -117,11 +117,11 @@ export class BindUtil {
|
|
|
117
117
|
);
|
|
118
118
|
} else if (Array.isArray(value)) {
|
|
119
119
|
for (let i = 0; i < value.length; i++) {
|
|
120
|
-
const
|
|
121
|
-
if (DataUtil.isPlainObject(
|
|
122
|
-
Object.assign(out, this.flattenPaths(
|
|
120
|
+
const element = value[i];
|
|
121
|
+
if (DataUtil.isPlainObject(element)) {
|
|
122
|
+
Object.assign(out, this.flattenPaths(element, `${pre}[${i}].`));
|
|
123
123
|
} else {
|
|
124
|
-
out[`${pre}[${i}]`] =
|
|
124
|
+
out[`${pre}[${i}]`] = element ?? '';
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
} else {
|
|
@@ -135,12 +135,12 @@ export class BindUtil {
|
|
|
135
135
|
* Bind data to the schema for a class, with an optional view
|
|
136
136
|
* @param cls The schema class to bind against
|
|
137
137
|
* @param data The provided data to bind
|
|
138
|
-
* @param
|
|
138
|
+
* @param config The bind configuration
|
|
139
139
|
*/
|
|
140
|
-
static bindSchema<T>(cls: Class<T>, data?: undefined,
|
|
141
|
-
static bindSchema<T>(cls: Class<T>, data?: null,
|
|
142
|
-
static bindSchema<T>(cls: Class<T>, data?: object | T,
|
|
143
|
-
static bindSchema<T>(cls: Class<T>, data?: object | T,
|
|
140
|
+
static bindSchema<T>(cls: Class<T>, data?: undefined, config?: BindConfig): undefined;
|
|
141
|
+
static bindSchema<T>(cls: Class<T>, data?: null, config?: BindConfig): null;
|
|
142
|
+
static bindSchema<T>(cls: Class<T>, data?: object | T, config?: BindConfig): T;
|
|
143
|
+
static bindSchema<T>(cls: Class<T>, data?: object | T, config?: BindConfig): T | null | undefined {
|
|
144
144
|
if (data === null || data === undefined) {
|
|
145
145
|
return data;
|
|
146
146
|
}
|
|
@@ -150,13 +150,13 @@ export class BindUtil {
|
|
|
150
150
|
const resolvedCls = SchemaRegistryIndex.resolveInstanceType<T>(cls, asFull<T>(data));
|
|
151
151
|
const instance = classConstruct<T & { type?: string }>(resolvedCls);
|
|
152
152
|
|
|
153
|
-
for (const
|
|
154
|
-
if (instance[
|
|
155
|
-
delete instance[
|
|
153
|
+
for (const key of TypedObject.keys(instance)) { // Do not retain undefined fields
|
|
154
|
+
if (instance[key] === undefined) {
|
|
155
|
+
delete instance[key];
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
const out = this.bindSchemaToObject(resolvedCls, instance, data,
|
|
159
|
+
const out = this.bindSchemaToObject(resolvedCls, instance, data, config);
|
|
160
160
|
SchemaRegistryIndex.get(resolvedCls).ensureInstanceTypeField(out);
|
|
161
161
|
return out;
|
|
162
162
|
}
|
|
@@ -165,35 +165,35 @@ export class BindUtil {
|
|
|
165
165
|
/**
|
|
166
166
|
* Bind the schema to the object
|
|
167
167
|
* @param cls The schema class
|
|
168
|
-
* @param
|
|
168
|
+
* @param input The target object (instance of cls)
|
|
169
169
|
* @param data The data to bind
|
|
170
|
-
* @param
|
|
170
|
+
* @param config The bind configuration
|
|
171
171
|
*/
|
|
172
|
-
static bindSchemaToObject<T>(cls: Class<T>,
|
|
173
|
-
const view =
|
|
174
|
-
delete
|
|
172
|
+
static bindSchemaToObject<T>(cls: Class<T>, input: T, data?: object, config: BindConfig = {}): T {
|
|
173
|
+
const view = config.view; // Does not convey
|
|
174
|
+
delete config.view;
|
|
175
175
|
|
|
176
176
|
if (!!data && isInstance<T>(data)) {
|
|
177
177
|
const adapter = SchemaRegistryIndex.get(cls);
|
|
178
|
-
const
|
|
178
|
+
const schemaConfig = adapter.get();
|
|
179
179
|
|
|
180
180
|
// If no configuration
|
|
181
|
-
if (!
|
|
182
|
-
for (const
|
|
183
|
-
|
|
181
|
+
if (!schemaConfig) {
|
|
182
|
+
for (const key of TypedObject.keys(data)) {
|
|
183
|
+
input[key] = data[key];
|
|
184
184
|
}
|
|
185
185
|
} else {
|
|
186
|
-
let schema: SchemaFieldMap =
|
|
186
|
+
let schema: SchemaFieldMap = schemaConfig.fields;
|
|
187
187
|
if (view) {
|
|
188
188
|
schema = adapter.getFields(view);
|
|
189
189
|
if (!schema) {
|
|
190
|
-
throw new Error(`View not found: ${view
|
|
190
|
+
throw new Error(`View not found: ${view}`);
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
for (const [schemaFieldName, field] of Object.entries(schema)) {
|
|
195
195
|
let inboundField: string | undefined = undefined;
|
|
196
|
-
if (field.access === 'readonly' ||
|
|
196
|
+
if (field.access === 'readonly' || config.filterInput?.(field) === false) {
|
|
197
197
|
continue; // Skip trying to write readonly fields
|
|
198
198
|
}
|
|
199
199
|
if (schemaFieldName in data) {
|
|
@@ -211,40 +211,40 @@ export class BindUtil {
|
|
|
211
211
|
continue;
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
-
let
|
|
214
|
+
let value: unknown = data[castKey<T>(inboundField)];
|
|
215
215
|
|
|
216
216
|
// Filtering values
|
|
217
|
-
if (
|
|
217
|
+
if (config.filterValue && !config.filterValue(value, field)) {
|
|
218
218
|
continue;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
if (
|
|
221
|
+
if (value !== undefined && value !== null) {
|
|
222
222
|
// Ensure its an array
|
|
223
|
-
if (!Array.isArray(
|
|
224
|
-
if (typeof
|
|
225
|
-
|
|
223
|
+
if (!Array.isArray(value) && field.array) {
|
|
224
|
+
if (typeof value === 'string' && value.includes(',')) {
|
|
225
|
+
value = value.split(/,/).map(part => part.trim());
|
|
226
226
|
} else {
|
|
227
|
-
|
|
227
|
+
value = [value];
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
if (SchemaRegistryIndex.has(field.type)) {
|
|
232
|
-
if (field.array && Array.isArray(
|
|
233
|
-
|
|
232
|
+
if (field.array && Array.isArray(value)) {
|
|
233
|
+
value = value.map(item => this.bindSchema(field.type, item, config));
|
|
234
234
|
} else {
|
|
235
|
-
|
|
235
|
+
value = this.bindSchema(field.type, value, config);
|
|
236
236
|
}
|
|
237
|
-
} else if (field.array && Array.isArray(
|
|
238
|
-
|
|
237
|
+
} else if (field.array && Array.isArray(value)) {
|
|
238
|
+
value = value.map(item => this.#coerceType(field, item));
|
|
239
239
|
} else {
|
|
240
|
-
|
|
240
|
+
value = this.#coerceType(field, value);
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
-
|
|
244
|
+
input[castKey<T>(schemaFieldName)] = castTo(value);
|
|
245
245
|
|
|
246
246
|
if (field.accessor) {
|
|
247
|
-
Object.defineProperty(
|
|
247
|
+
Object.defineProperty(input, schemaFieldName, {
|
|
248
248
|
...adapter.getAccessorDescriptor(schemaFieldName),
|
|
249
249
|
enumerable: true
|
|
250
250
|
});
|
|
@@ -253,40 +253,40 @@ export class BindUtil {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
return
|
|
256
|
+
return input;
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
/**
|
|
260
260
|
* Coerce field to type
|
|
261
|
-
* @param
|
|
262
|
-
* @param
|
|
261
|
+
* @param config
|
|
262
|
+
* @param value
|
|
263
263
|
* @param applyDefaults
|
|
264
264
|
* @returns
|
|
265
265
|
*/
|
|
266
|
-
static coerceInput(
|
|
267
|
-
if ((
|
|
268
|
-
|
|
266
|
+
static coerceInput(config: SchemaInputConfig, value: unknown, applyDefaults = false): unknown {
|
|
267
|
+
if ((value === undefined || value === null) && applyDefaults) {
|
|
268
|
+
value = Array.isArray(config.default) ? config.default.slice(0) : config.default;
|
|
269
269
|
}
|
|
270
|
-
if (
|
|
271
|
-
return
|
|
270
|
+
if (config.required?.active === false && (value === undefined || value === null)) {
|
|
271
|
+
return value;
|
|
272
272
|
}
|
|
273
|
-
const complex = SchemaRegistryIndex.has(
|
|
274
|
-
const
|
|
275
|
-
if (
|
|
276
|
-
const
|
|
273
|
+
const complex = SchemaRegistryIndex.has(config.type);
|
|
274
|
+
const bindConfig: BindConfig | undefined = (complex && 'view' in config && typeof config.view === 'string') ? { view: config.view } : undefined;
|
|
275
|
+
if (config.array) {
|
|
276
|
+
const subValue = !Array.isArray(value) ? [value] : value;
|
|
277
277
|
if (complex) {
|
|
278
|
-
|
|
278
|
+
value = subValue.map(item => this.bindSchema(config.type, item, bindConfig));
|
|
279
279
|
} else {
|
|
280
|
-
|
|
280
|
+
value = subValue.map(item => DataUtil.coerceType(item, config.type, false));
|
|
281
281
|
}
|
|
282
282
|
} else {
|
|
283
283
|
if (complex) {
|
|
284
|
-
|
|
284
|
+
value = this.bindSchema(config.type, value, bindConfig);
|
|
285
285
|
} else {
|
|
286
|
-
|
|
286
|
+
value = DataUtil.coerceType(value, config.type, false);
|
|
287
287
|
}
|
|
288
288
|
}
|
|
289
|
-
return
|
|
289
|
+
return value;
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
/**
|
|
@@ -298,8 +298,8 @@ export class BindUtil {
|
|
|
298
298
|
static coerceParameters(fields: SchemaParameterConfig[], params: unknown[], applyDefaults = true): unknown[] {
|
|
299
299
|
params = [...params];
|
|
300
300
|
// Coerce types
|
|
301
|
-
for (const
|
|
302
|
-
params[
|
|
301
|
+
for (const field of fields) {
|
|
302
|
+
params[field.index!] = this.coerceInput(field, params[field.index!], applyDefaults);
|
|
303
303
|
}
|
|
304
304
|
return params;
|
|
305
305
|
}
|
|
@@ -311,7 +311,7 @@ export class BindUtil {
|
|
|
311
311
|
* @param params
|
|
312
312
|
* @returns
|
|
313
313
|
*/
|
|
314
|
-
static coerceMethodParams<T>(cls: Class<T>, method: string
|
|
314
|
+
static coerceMethodParams<T>(cls: Class<T>, method: string, params: unknown[], applyDefaults = true): unknown[] {
|
|
315
315
|
const paramConfigs = SchemaRegistryIndex.get(cls).getMethod(method).parameters;
|
|
316
316
|
return this.coerceParameters(paramConfigs, params, applyDefaults);
|
|
317
317
|
}
|
package/src/data.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isNumberObject
|
|
1
|
+
import { isNumberObject, isBooleanObject, isStringObject } from 'node:util/types';
|
|
2
2
|
|
|
3
3
|
import { asConstructable, castTo, Class, asFull, TypedObject } from '@travetto/runtime';
|
|
4
4
|
import { UnknownType } from './types.ts';
|
|
@@ -12,24 +12,26 @@ export class DataUtil {
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Is a value a plain JS object, created using {}
|
|
15
|
-
* @param
|
|
15
|
+
* @param value Object to check
|
|
16
16
|
*/
|
|
17
|
-
static isPlainObject(
|
|
18
|
-
return typeof
|
|
19
|
-
&&
|
|
20
|
-
&&
|
|
21
|
-
&&
|
|
22
|
-
&& Object.prototype.toString.call(
|
|
17
|
+
static isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
18
|
+
return typeof value === 'object' // separate from primitives
|
|
19
|
+
&& value !== undefined
|
|
20
|
+
&& value !== null // is obvious
|
|
21
|
+
&& value.constructor === Object // separate instances (Array, DOM, ...)
|
|
22
|
+
&& Object.prototype.toString.call(value) === '[object Object]'; // separate build-in like Math
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Is a value of primitive type
|
|
27
|
-
* @param
|
|
27
|
+
* @param value Value to check
|
|
28
28
|
*/
|
|
29
|
-
static isPrimitive(
|
|
30
|
-
switch (typeof
|
|
29
|
+
static isPrimitive(value: unknown): value is (string | boolean | number | RegExp) {
|
|
30
|
+
switch (typeof value) {
|
|
31
31
|
case 'string': case 'boolean': case 'number': case 'bigint': return true;
|
|
32
|
-
case 'object': return !!
|
|
32
|
+
case 'object': return !!value && (
|
|
33
|
+
value instanceof RegExp || value instanceof Date || isStringObject(value) || isNumberObject(value) || isBooleanObject(value)
|
|
34
|
+
);
|
|
33
35
|
default: return false;
|
|
34
36
|
}
|
|
35
37
|
}
|
|
@@ -37,8 +39,8 @@ export class DataUtil {
|
|
|
37
39
|
/**
|
|
38
40
|
* Is simple, as a primitive, function or class
|
|
39
41
|
*/
|
|
40
|
-
static isSimpleValue(
|
|
41
|
-
return this.isPrimitive(
|
|
42
|
+
static isSimpleValue(value: unknown): value is Function | Class | string | number | RegExp | Date {
|
|
43
|
+
return this.isPrimitive(value) || typeof value === 'function';
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
static #deepAssignRaw(a: unknown, b: unknown, mode: 'replace' | 'loose' | 'strict' | 'coerce' = 'loose'): unknown {
|
|
@@ -68,10 +70,10 @@ export class DataUtil {
|
|
|
68
70
|
if (mode === 'replace') {
|
|
69
71
|
value = b;
|
|
70
72
|
} else {
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
for (let i = 0; i <
|
|
74
|
-
|
|
73
|
+
const valueArray: unknown[] = castTo(value);
|
|
74
|
+
const bArray = b;
|
|
75
|
+
for (let i = 0; i < bArray.length; i++) {
|
|
76
|
+
valueArray[i] = this.#deepAssignRaw(valueArray[i], bArray[i], mode);
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
79
|
} else if (isSimpB) { // Scalars
|
|
@@ -87,14 +89,14 @@ export class DataUtil {
|
|
|
87
89
|
}
|
|
88
90
|
} else { // Object merge
|
|
89
91
|
value = a;
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
+
const bObject: Record<string, unknown> = castTo(b);
|
|
93
|
+
const valueObject: Record<string, unknown> = castTo(value);
|
|
92
94
|
|
|
93
|
-
for (const key of Object.keys(
|
|
95
|
+
for (const key of Object.keys(bObject)) {
|
|
94
96
|
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
95
97
|
continue;
|
|
96
98
|
}
|
|
97
|
-
|
|
99
|
+
valueObject[key] = this.#deepAssignRaw(valueObject[key], bObject[key], mode);
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
}
|
|
@@ -119,7 +121,7 @@ export class DataUtil {
|
|
|
119
121
|
/**
|
|
120
122
|
* Coerce an input of any type to the class provided
|
|
121
123
|
* @param input Input value
|
|
122
|
-
* @param type Class to coerce to (String, Boolean, Number, Date,
|
|
124
|
+
* @param type Class to coerce to (String, Boolean, Number, Date, RegExp, Object)
|
|
123
125
|
* @param strict Should a failure to coerce throw an error?
|
|
124
126
|
*/
|
|
125
127
|
static coerceType(input: unknown, type: typeof String, strict?: boolean): string;
|
|
@@ -226,10 +228,10 @@ export class DataUtil {
|
|
|
226
228
|
|
|
227
229
|
/**
|
|
228
230
|
* Clone top level properties to a new object
|
|
229
|
-
* @param
|
|
231
|
+
* @param value Object to clone
|
|
230
232
|
*/
|
|
231
|
-
static shallowClone<T>(
|
|
232
|
-
return castTo(Array.isArray(
|
|
233
|
+
static shallowClone<T>(value: T): T {
|
|
234
|
+
return castTo(Array.isArray(value) ? value.slice(0) : (this.isSimpleValue(value) ? value : { ...castTo<object>(value) }));
|
|
233
235
|
}
|
|
234
236
|
|
|
235
237
|
/**
|
|
@@ -247,29 +249,28 @@ export class DataUtil {
|
|
|
247
249
|
|
|
248
250
|
/**
|
|
249
251
|
* Filter object by excluding specific keys
|
|
250
|
-
* @param
|
|
252
|
+
* @param input A value to filter, primitives will be untouched
|
|
251
253
|
* @param exclude Strings or patterns to exclude against
|
|
252
254
|
* @returns
|
|
253
255
|
*/
|
|
254
|
-
static filterByKeys<T>(
|
|
255
|
-
if (Array.isArray(
|
|
256
|
-
return castTo(
|
|
257
|
-
} else if (
|
|
256
|
+
static filterByKeys<T>(input: T, exclude: (string | RegExp)[]): T {
|
|
257
|
+
if (Array.isArray(input)) {
|
|
258
|
+
return castTo(input.map(value => this.filterByKeys(value, exclude)));
|
|
259
|
+
} else if (input !== null && input !== undefined && typeof input === 'object') {
|
|
258
260
|
const out: Partial<T> = {};
|
|
259
|
-
for (const key of TypedObject.keys(
|
|
260
|
-
if (!exclude.some(
|
|
261
|
-
const
|
|
262
|
-
if (typeof
|
|
263
|
-
out[key] = this.filterByKeys(
|
|
261
|
+
for (const key of TypedObject.keys(input)) {
|
|
262
|
+
if (!exclude.some(toMatch => typeof key === 'string' && (typeof toMatch === 'string' ? toMatch === key : toMatch.test(key)))) {
|
|
263
|
+
const value = input[key];
|
|
264
|
+
if (typeof value === 'object') {
|
|
265
|
+
out[key] = this.filterByKeys(value, exclude);
|
|
264
266
|
} else {
|
|
265
|
-
out[key] =
|
|
267
|
+
out[key] = value;
|
|
266
268
|
}
|
|
267
269
|
}
|
|
268
270
|
}
|
|
269
271
|
return asFull(out);
|
|
270
272
|
} else {
|
|
271
|
-
return
|
|
273
|
+
return input;
|
|
272
274
|
}
|
|
273
275
|
}
|
|
274
|
-
|
|
275
276
|
}
|