@travetto/schema 3.0.3 → 3.1.0-rc.0

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"), [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.
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"), [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.
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
 
@@ -203,12 +203,13 @@ Validation Failed {
203
203
  "errors": [
204
204
  {
205
205
  "kind": "type",
206
+ "type": "number",
206
207
  "message": "age is not a valid number",
207
- "path": "age",
208
- "type": "number"
208
+ "path": "age"
209
209
  },
210
210
  {
211
211
  "kind": "required",
212
+ "active": true,
212
213
  "message": "address.street2 is required",
213
214
  "path": "address.street2"
214
215
  }
@@ -269,6 +270,10 @@ export interface ValidationError {
269
270
  * Regular expression to match
270
271
  */
271
272
  re?: string;
273
+ /**
274
+ * Number to compare against
275
+ */
276
+ n?: number | Date;
272
277
  /**
273
278
  * The type of the field
274
279
  */
@@ -341,9 +346,9 @@ Validation Failed {
341
346
  "errors": [
342
347
  {
343
348
  "kind": "type",
349
+ "type": "PointImpl",
344
350
  "message": "point is not a valid PointImpl",
345
- "path": "point",
346
- "type": "PointImpl"
351
+ "path": "point"
347
352
  }
348
353
  ]
349
354
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema",
3
- "version": "3.0.3",
3
+ "version": "3.1.0-rc.0",
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.3"
30
+ "@travetto/registry": "^3.1.0-rc.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^3.0.3"
33
+ "@travetto/transformer": "^3.1.0-rc.0"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
@@ -233,7 +233,7 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
233
233
  const { fields, schema } = this.getViewSchema(cls);
234
234
  const out = [];
235
235
  for (const el of fields) {
236
- if (el.startsWith(`${method}.`)) {
236
+ if (el.startsWith(`${method}.`) && schema[el].forMethod) {
237
237
  out.push(schema[el]);
238
238
  }
239
239
  }
@@ -302,6 +302,7 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
302
302
  registerPendingParamConfig(target: Class, method: string, idx: number, type: ClassList, conf?: Partial<FieldConfig>): Class {
303
303
  conf ??= {};
304
304
  conf.index = idx;
305
+ conf.forMethod = true;
305
306
  return this.registerPendingFieldConfig(target, `${method}.${idx}`, type, conf);
306
307
  }
307
308
 
@@ -160,6 +160,10 @@ export interface FieldConfig extends DescribableConfig {
160
160
  * Is this field a getter or setter
161
161
  */
162
162
  accessor?: string;
163
+ /**
164
+ * Is the field for a method
165
+ */
166
+ forMethod?: boolean;
163
167
  }
164
168
 
165
169
  export type ViewFieldsConfig<T> = { with: Extract<(keyof T), string>[] } | { without: Extract<(keyof T), string>[] };
@@ -25,6 +25,10 @@ export interface ValidationError {
25
25
  * Regular expression to match
26
26
  */
27
27
  re?: string;
28
+ /**
29
+ * Number to compare against
30
+ */
31
+ n?: number | Date;
28
32
  /**
29
33
  * The type of the field
30
34
  */
@@ -55,6 +59,10 @@ export interface ValidationResult {
55
59
  * Potential regular expression for the result
56
60
  */
57
61
  re?: RegExp;
62
+ /**
63
+ * Number to compare against
64
+ */
65
+ n?: number | Date;
58
66
  }
59
67
 
60
68
  /**
@@ -40,7 +40,7 @@ export class SchemaValidator {
40
40
 
41
41
  const fields = TypedObject.keys<SchemaConfig>(schema);
42
42
  for (const field of fields) {
43
- if (schema[field].access !== 'readonly') { // Do not validate readonly fields
43
+ if (schema[field].access !== 'readonly' && !schema[field].forMethod) { // Do not validate readonly fields
44
44
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
45
45
  errors = errors.concat(this.#validateFieldSchema(schema[field], o[field as keyof T], relative));
46
46
  }
@@ -202,6 +202,7 @@ export class SchemaValidator {
202
202
  const out: ValidationError[] = [];
203
203
  for (const res of results) {
204
204
  const err: ValidationError = {
205
+ ...res,
205
206
  kind: res.kind,
206
207
  value: res.value,
207
208
  message: '',
@@ -311,7 +312,7 @@ export class SchemaValidator {
311
312
  * @param method The method being invoked
312
313
  * @param params The params to validate
313
314
  */
314
- static validateMethod<T>(cls: Class<T>, method: string, params: unknown[]): void {
315
+ static async validateMethod<T>(cls: Class<T>, method: string, params: unknown[]): Promise<void> {
315
316
  const errors: ValidationError[] = [];
316
317
  for (const field of SchemaRegistry.getMethodSchema(cls, method)) {
317
318
  errors.push(...this.#validateFieldSchema(field, params[field.index!]));
@@ -128,6 +128,12 @@ export class SchemaTransformUtil {
128
128
  }
129
129
  }
130
130
 
131
+ const tags = ts.getJSDocTags(node);
132
+ const aliases = tags.filter(x => x.tagName.getText() === 'alias');
133
+ if (aliases.length) {
134
+ attrs.push(state.factory.createPropertyAssignment('aliases', state.fromLiteral(aliases.map(x => x.comment).filter(x => !!x))));
135
+ }
136
+
131
137
  const params: ts.Expression[] = [];
132
138
 
133
139
  const existing = state.findDecorator('@travetto/schema', node, 'Field', FIELD_MOD);