@travetto/schema 6.0.0-rc.2 → 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 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 api for Web Applications with support for the dependency injection."), [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.
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
 
@@ -25,6 +25,7 @@ The module utilizes AST transformations to collect schema information, and facil
25
25
  * `title` - definition of the schema
26
26
  * `description` - detailed description of the schema
27
27
  * `examples` - A set of examples as [JSON](https://www.json.org) or [YAML](https://en.wikipedia.org/wiki/YAML)
28
+
28
29
  The `title` 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.
29
30
 
30
31
  **Code: Sample User Schema**
@@ -85,15 +86,17 @@ This schema provides a powerful base for data binding and validation at runtime.
85
86
  * [@Secret](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L56) marks a field as being sensitive. This is used by certain logging activities to ensure sensitive information is not logged out.
86
87
  * [@Specifier](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L198) attributes additional specifiers to a field, allowing for more specification beyond just the field's type.
87
88
  * [@SubTypeField](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L204) allows for promoting a given field as the owner of the sub type discriminator (defaults to `type`).
89
+
88
90
  Additionally, schemas can be nested to form more complex data structures that are able to bound and validated.
89
91
 
90
92
  Just like the class, all fields can be defined with
91
93
  * `description` - detailed description of the schema
92
94
  * `examples` - A set of examples as [JSON](https://www.json.org) or [YAML](https://en.wikipedia.org/wiki/YAML)
95
+
93
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.
94
97
 
95
98
  ### Parameters
96
- Parameters are available in certain scenarios (e.g. [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative api for Web Applications with support for the dependency injection.") 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.
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.
97
100
 
98
101
  **Code: Sample Parameter Usage**
99
102
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema",
3
- "version": "6.0.0-rc.2",
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.0-rc.2"
30
+ "@travetto/registry": "^6.0.1"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^6.0.0-rc.3"
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
- node.members
87
+ members
61
88
  );
62
89
  }
63
90