@travetto/schema 3.0.0-rc.9 → 3.0.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -9
- package/package.json +3 -3
- package/src/bind-util.ts +11 -0
- package/src/service/registry.ts +0 -12
package/README.md
CHANGED
|
@@ -6,10 +6,14 @@
|
|
|
6
6
|
**Install: @travetto/schema**
|
|
7
7
|
```bash
|
|
8
8
|
npm install @travetto/schema
|
|
9
|
+
|
|
10
|
+
# or
|
|
11
|
+
|
|
12
|
+
yarn add @travetto/schema
|
|
9
13
|
```
|
|
10
14
|
|
|
11
15
|
This module's purpose is to allow for proper declaration and validation of data types, in the course of running a program. The framework defined here, is
|
|
12
|
-
leveraged in the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support"), [Application](https://github.com/travetto/travetto/tree/main/module/app#readme "Application registration/management and run support."), [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module."), [OpenAPI Specification](https://github.com/travetto/travetto/tree/main/module/openapi#readme "OpenAPI integration support for the
|
|
16
|
+
leveraged in the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support"), [Application](https://github.com/travetto/travetto/tree/main/module/app#readme "Application registration/management and run support."), [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module."), [OpenAPI Specification](https://github.com/travetto/travetto/tree/main/module/openapi#readme "OpenAPI integration support for the Travetto framework") and [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") modules. The schema is the backbone of all data transfer, as it helps to
|
|
13
17
|
provide validation on correctness of input, whether it is a rest request, command line inputs, or a configuration file.
|
|
14
18
|
|
|
15
19
|
This module provides a mechanism for registering classes and field level information as well the ability to apply that information at runtime.
|
|
@@ -140,14 +144,13 @@ and the output would be a `Person` instance with the following structure
|
|
|
140
144
|
|
|
141
145
|
**Terminal: Sample data output after binding**
|
|
142
146
|
```bash
|
|
143
|
-
$ trv main
|
|
147
|
+
$ trv main doc/person-output.ts
|
|
144
148
|
|
|
145
149
|
Person {
|
|
146
150
|
name: 'Test',
|
|
147
151
|
age: 19,
|
|
148
152
|
address: Address { street1: '1234 Fun', street2: 'Unit 20' }
|
|
149
153
|
}
|
|
150
|
-
[s[r[u
|
|
151
154
|
```
|
|
152
155
|
|
|
153
156
|
**Note**: Binding will attempt to convert/coerce types as much as possible to honor the pattern of Javascript and it's dynamic nature.
|
|
@@ -201,9 +204,27 @@ would produce an exception similar to following structure
|
|
|
201
204
|
|
|
202
205
|
**Terminal: Sample error output**
|
|
203
206
|
```bash
|
|
204
|
-
$ trv main
|
|
205
|
-
|
|
206
|
-
|
|
207
|
+
$ trv main doc/person-invalid-output.ts
|
|
208
|
+
|
|
209
|
+
Validation Failed {
|
|
210
|
+
"message": "Validation errors have occurred",
|
|
211
|
+
"category": "data",
|
|
212
|
+
"type": "ValidationResultError",
|
|
213
|
+
"at": "2029-03-14T04:00:00.618Z",
|
|
214
|
+
"errors": [
|
|
215
|
+
{
|
|
216
|
+
"kind": "type",
|
|
217
|
+
"message": "age is not a valid number",
|
|
218
|
+
"path": "age",
|
|
219
|
+
"type": "number"
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
"kind": "required",
|
|
223
|
+
"message": "address.street2 is required",
|
|
224
|
+
"path": "address.street2"
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
}
|
|
207
228
|
```
|
|
208
229
|
|
|
209
230
|
### Custom Validators
|
|
@@ -324,7 +345,20 @@ All that happens now, is the type is exported, and the class above is able to pr
|
|
|
324
345
|
|
|
325
346
|
**Terminal: Custom Type Validation**
|
|
326
347
|
```bash
|
|
327
|
-
$ trv main
|
|
328
|
-
|
|
329
|
-
|
|
348
|
+
$ trv main doc/custom-type-output.ts
|
|
349
|
+
|
|
350
|
+
Validation Failed {
|
|
351
|
+
"message": "Validation errors have occurred",
|
|
352
|
+
"category": "data",
|
|
353
|
+
"type": "ValidationResultError",
|
|
354
|
+
"at": "2029-03-14T04:00:00.837Z",
|
|
355
|
+
"errors": [
|
|
356
|
+
{
|
|
357
|
+
"kind": "type",
|
|
358
|
+
"message": "point is not a valid PointImpl",
|
|
359
|
+
"path": "point",
|
|
360
|
+
"type": "PointImpl"
|
|
361
|
+
}
|
|
362
|
+
]
|
|
363
|
+
}
|
|
330
364
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1-rc.1",
|
|
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": "^3.0.
|
|
30
|
+
"@travetto/registry": "^3.0.1-rc.1"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^3.0.
|
|
33
|
+
"@travetto/transformer": "^3.0.1-rc.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/bind-util.ts
CHANGED
|
@@ -306,4 +306,15 @@ export class BindUtil {
|
|
|
306
306
|
}
|
|
307
307
|
return params;
|
|
308
308
|
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Coerce method parameters when possible
|
|
312
|
+
* @param cls
|
|
313
|
+
* @param method
|
|
314
|
+
* @param params
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
static coerceMethodParams<T>(cls: Class<T>, method: string, params: unknown[], applyDefaults = false): unknown[] {
|
|
318
|
+
return this.coerceFields(SchemaRegistry.getMethodSchema(cls, method), params, applyDefaults);
|
|
319
|
+
}
|
|
309
320
|
}
|
package/src/service/registry.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { MetadataRegistry, RootRegistry, ChangeEvent } from '@travetto/registry'
|
|
|
3
3
|
|
|
4
4
|
import { ClassList, FieldConfig, ClassConfig, SchemaConfig, ViewFieldsConfig, ViewConfig } from './types';
|
|
5
5
|
import { SchemaChangeListener } from './changes';
|
|
6
|
-
import { BindUtil } from '../bind-util';
|
|
7
6
|
import { AllViewⲐ } from '../internal/types';
|
|
8
7
|
|
|
9
8
|
function hasType<T>(o: unknown): o is { type: Class<T> | string } {
|
|
@@ -244,17 +243,6 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
|
|
|
244
243
|
return cache.get(method)! ?? [];
|
|
245
244
|
}
|
|
246
245
|
|
|
247
|
-
/**
|
|
248
|
-
* Coerce method parameters when possible
|
|
249
|
-
* @param cls
|
|
250
|
-
* @param method
|
|
251
|
-
* @param params
|
|
252
|
-
* @returns
|
|
253
|
-
*/
|
|
254
|
-
coerceMethodParams<T>(cls: Class<T>, method: string, params: unknown[], applyDefaults = false): unknown[] {
|
|
255
|
-
return BindUtil.coerceFields(this.getMethodSchema(cls, method), params, applyDefaults);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
246
|
/**
|
|
259
247
|
* Register a view
|
|
260
248
|
* @param target The target class
|