@travetto/schema 3.0.2 → 3.0.3
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 +6 -20
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
|
-
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/schema/DOC.
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/schema/DOC.tsx and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Schema
|
|
4
|
+
|
|
4
5
|
## Data type registry for runtime validation, reflection and binding.
|
|
5
6
|
|
|
6
7
|
**Install: @travetto/schema**
|
|
@@ -12,23 +13,18 @@ npm install @travetto/schema
|
|
|
12
13
|
yarn add @travetto/schema
|
|
13
14
|
```
|
|
14
15
|
|
|
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
|
|
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
|
|
17
|
-
provide validation on correctness of input, whether it is a rest 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"), [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 provide validation on correctness of input, whether it is a rest request, command line inputs, or a configuration file.
|
|
18
17
|
|
|
19
18
|
This module provides a mechanism for registering classes and field level information as well the ability to apply that information at runtime.
|
|
20
19
|
|
|
21
20
|
## Registration
|
|
22
|
-
The registry's schema information is defined by [Typescript](https://typescriptlang.org) AST and only applies to classes registered with the [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14) decoration.
|
|
21
|
+
The registry's schema information is defined by [Typescript](https://typescriptlang.org) AST and only applies to classes registered with the [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14) decoration.
|
|
23
22
|
|
|
24
23
|
### Classes
|
|
25
24
|
The module utilizes AST transformations to collect schema information, and facilitate the registration process without user intervention. The class can also be described using providing a:
|
|
26
|
-
|
|
27
|
-
|
|
28
25
|
* `title` - definition of the schema
|
|
29
26
|
* `description` - detailed description of the schema
|
|
30
27
|
* `examples` - A set of examples as [JSON](https://www.json.org) or [YAML](https://en.wikipedia.org/wiki/YAML)
|
|
31
|
-
|
|
32
28
|
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.
|
|
33
29
|
|
|
34
30
|
**Code: Sample User Schema**
|
|
@@ -66,8 +62,6 @@ User:
|
|
|
66
62
|
|
|
67
63
|
### Fields
|
|
68
64
|
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:
|
|
69
|
-
|
|
70
|
-
|
|
71
65
|
* [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L38) defines a field that will be serialized.
|
|
72
66
|
* [@Required](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L78) defines a that field should be required
|
|
73
67
|
* [@Enum](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L85) defines the allowable values that a field can have
|
|
@@ -87,15 +81,11 @@ This schema provides a powerful base for data binding and validation at runtime.
|
|
|
87
81
|
* [@LongText](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L98) same as text, but expects longer form content
|
|
88
82
|
* [@Readonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L65) defines a that field should not be bindable external to the class
|
|
89
83
|
* [@Writeonly](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L59) defines a that field should not be exported in serialization, but that it can be bound to
|
|
90
|
-
|
|
91
|
-
Additionally, schemas can be nested to form more complex data structures that are able to bound and validated.
|
|
84
|
+
Additionally, schemas can be nested to form more complex data structures that are able to bound and validated.
|
|
92
85
|
|
|
93
86
|
Just like the class, all fields can be defined with
|
|
94
|
-
|
|
95
|
-
|
|
96
87
|
* `description` - detailed description of the schema
|
|
97
88
|
* `examples` - A set of examples as [JSON](https://www.json.org) or [YAML](https://en.wikipedia.org/wiki/YAML)
|
|
98
|
-
|
|
99
89
|
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.
|
|
100
90
|
|
|
101
91
|
## Binding/Validation
|
|
@@ -156,7 +146,6 @@ Person {
|
|
|
156
146
|
**Note**: Binding will attempt to convert/coerce types as much as possible to honor the pattern of Javascript and it's dynamic nature.
|
|
157
147
|
|
|
158
148
|
### Validation
|
|
159
|
-
|
|
160
149
|
Validation is very similar to binding, but instead of attempting to assign values, any mismatch or violation of the schema will result in errors. All errors will be collected and returned. Given the same schema as above,
|
|
161
150
|
|
|
162
151
|
**Code: Sub Schemas via Address**
|
|
@@ -228,7 +217,6 @@ Validation Failed {
|
|
|
228
217
|
```
|
|
229
218
|
|
|
230
219
|
### Custom Validators
|
|
231
|
-
|
|
232
220
|
Within the schema framework, it is possible to add custom validators class level. This allows for more flexibility when dealing with specific situations (e.g. password requirements or ensuring two fields match)
|
|
233
221
|
|
|
234
222
|
**Code: Password Validator**
|
|
@@ -289,7 +277,7 @@ export interface ValidationError {
|
|
|
289
277
|
```
|
|
290
278
|
|
|
291
279
|
## Custom Types
|
|
292
|
-
When working with the schema, the basic types are easily understood, but some of [Typescript](https://typescriptlang.org)'s more complex constructs are too complex to automate cleanly.
|
|
280
|
+
When working with the schema, the basic types are easily understood, but some of [Typescript](https://typescriptlang.org)'s more complex constructs are too complex to automate cleanly.
|
|
293
281
|
|
|
294
282
|
To that end, the module supports two concepts:
|
|
295
283
|
|
|
@@ -324,8 +312,6 @@ export class PointImpl {
|
|
|
324
312
|
```
|
|
325
313
|
|
|
326
314
|
What you can see here is that the `Point` type is now backed by a class that supports:
|
|
327
|
-
|
|
328
|
-
|
|
329
315
|
* `validateSchema` - Will run during validation for this specific type.
|
|
330
316
|
* `bindSchema` - Will run during binding to ensure correct behavior.
|
|
331
317
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
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.3"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^3.0.
|
|
33
|
+
"@travetto/transformer": "^3.0.3"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|