@travetto/schema 6.0.0 → 6.0.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 +2 -2
- package/package.json +3 -3
- package/support/transformer.schema.ts +29 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @travetto/schema
|
|
|
13
13
|
yarn add @travetto/schema
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
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 leveraged in the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support"), [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework"), [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative
|
|
16
|
+
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 leveraged in the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support"), [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework"), [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications"), [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 provide validation on correctness of input, whether it is a web request, command line inputs, or a configuration file.
|
|
17
17
|
|
|
18
18
|
This module provides a mechanism for registering classes and field level information as well the ability to apply that information at runtime.
|
|
19
19
|
|
|
@@ -96,7 +96,7 @@ Just like the class, all fields can be defined with
|
|
|
96
96
|
And similarly, the `description` will be picked up from the [JSDoc](http://usejsdoc.org/about-getting-started.html) comments, and additionally all fields can be set using the [@Describe](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/common.ts#L15) decorator.
|
|
97
97
|
|
|
98
98
|
### Parameters
|
|
99
|
-
Parameters are available in certain scenarios (e.g. [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative
|
|
99
|
+
Parameters are available in certain scenarios (e.g. [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") endpoints and [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework") main methods). In these scenarios, all of the field decorators are valid, but need to be called slightly differently to pass the typechecker. The simple solution is to use the `Arg` field of the decorator to convince Typescript its the correct type.
|
|
100
100
|
|
|
101
101
|
**Code: Sample Parameter Usage**
|
|
102
102
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.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": "^6.0.
|
|
30
|
+
"@travetto/registry": "^6.0.1"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^6.0.
|
|
33
|
+
"@travetto/transformer": "^6.0.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
TransformerState, OnProperty, OnClass, AfterClass, DocUtil, DeclarationUtil, OnGetter, OnSetter
|
|
4
|
+
TransformerState, OnProperty, OnClass, AfterClass, DocUtil, DeclarationUtil, OnGetter, OnSetter,
|
|
5
|
+
DecoratorUtil
|
|
5
6
|
} from '@travetto/transformer';
|
|
6
7
|
|
|
7
8
|
import { SchemaTransformUtil } from './transformer/util.ts';
|
|
@@ -50,6 +51,32 @@ export class SchemaTransformer {
|
|
|
50
51
|
|
|
51
52
|
delete state[InSchemaSymbol];
|
|
52
53
|
delete state[AccessorsSymbol];
|
|
54
|
+
let members = node.members;
|
|
55
|
+
|
|
56
|
+
const schemaMethods = [
|
|
57
|
+
...node.modifiers?.filter(x => ts.isDecorator(x))
|
|
58
|
+
.flatMap(x => state.getDeclarations(DecoratorUtil.getDecoratorIdent(x))) ?? [],
|
|
59
|
+
node
|
|
60
|
+
]
|
|
61
|
+
.flatMap(v => state.readDocTagList(v, 'schemaMethods'));
|
|
62
|
+
|
|
63
|
+
if (schemaMethods.length) {
|
|
64
|
+
const methodSet = new Set(schemaMethods.flatMap(x => x.split(/\s*,\s*/g)));
|
|
65
|
+
members = state.factory.createNodeArray(
|
|
66
|
+
node.members.map(x => ts.isMethodDeclaration(x) && methodSet.has(x.name.getText()) ?
|
|
67
|
+
state.factory.updateMethodDeclaration(
|
|
68
|
+
x,
|
|
69
|
+
x.modifiers,
|
|
70
|
+
x.asteriskToken,
|
|
71
|
+
x.name,
|
|
72
|
+
x.questionToken,
|
|
73
|
+
x.typeParameters,
|
|
74
|
+
x.parameters.map(y => SchemaTransformUtil.computeField(state, y)),
|
|
75
|
+
x.type,
|
|
76
|
+
x.body
|
|
77
|
+
) : x)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
53
80
|
|
|
54
81
|
return state.factory.updateClassDeclaration(
|
|
55
82
|
node,
|
|
@@ -57,7 +84,7 @@ export class SchemaTransformer {
|
|
|
57
84
|
node.name,
|
|
58
85
|
node.typeParameters,
|
|
59
86
|
node.heritageClauses,
|
|
60
|
-
|
|
87
|
+
members
|
|
61
88
|
);
|
|
62
89
|
}
|
|
63
90
|
|