@travetto/schema 3.4.1 → 3.4.2

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/__index__.ts CHANGED
@@ -12,4 +12,5 @@ export * from './src/validate/validator';
12
12
  export * from './src/validate/error';
13
13
  export * from './src/validate/types';
14
14
  export * from './src/bind-util';
15
+ export * from './src/name';
15
16
  export * from './src/types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema",
3
- "version": "3.4.1",
3
+ "version": "3.4.2",
4
4
  "description": "Data type registry for runtime validation, reflection and binding.",
5
5
  "keywords": [
6
6
  "schema",
@@ -30,7 +30,7 @@
30
30
  "@travetto/registry": "^3.4.1"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^3.4.0"
33
+ "@travetto/transformer": "^3.4.1"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/bind-util.ts CHANGED
@@ -272,7 +272,7 @@ export class BindUtil {
272
272
  */
273
273
  static coerceField(field: FieldConfig, val: unknown, applyDefaults = false): unknown {
274
274
  if ((val === undefined || val === null) && applyDefaults) {
275
- val = field.default;
275
+ val = Array.isArray(field.default) ? field.default.slice(0) : field.default;
276
276
  }
277
277
  if (!field.required && (val === undefined || val === null)) {
278
278
  return val;
@@ -301,7 +301,7 @@ export class BindUtil {
301
301
  * @param params
302
302
  * @returns
303
303
  */
304
- static coerceFields(fields: FieldConfig[], params: unknown[], applyDefaults = false): unknown[] {
304
+ static coerceFields(fields: FieldConfig[], params: unknown[], applyDefaults = true): unknown[] {
305
305
  params = [...params];
306
306
  // Coerce types
307
307
  for (const el of fields) {
@@ -317,7 +317,7 @@ export class BindUtil {
317
317
  * @param params
318
318
  * @returns
319
319
  */
320
- static coerceMethodParams<T>(cls: Class<T>, method: string, params: unknown[], applyDefaults = false): unknown[] {
320
+ static coerceMethodParams<T>(cls: Class<T>, method: string, params: unknown[], applyDefaults = true): unknown[] {
321
321
  return this.coerceFields(SchemaRegistry.getMethodSchema(cls, method), params, applyDefaults);
322
322
  }
323
323
  }
package/src/name.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { RootIndex } from '@travetto/manifest';
2
+ import { ClassConfig } from './service/types';
3
+
4
+ const SYN_RE = /(__)(\d+)[^0-9]*$/;
5
+
6
+ /**
7
+ * Name resolver, specifically for synthetic types
8
+ */
9
+ export class SchemaNameResolver {
10
+
11
+ #schemaIdToName = new Map<string, string>();
12
+ #digits: number;
13
+ #base: number;
14
+
15
+ constructor(digits = 5) {
16
+ this.#digits = digits;
17
+ this.#base = 10 ** this.#digits;
18
+ }
19
+
20
+ getName(schema: ClassConfig): string {
21
+ const id = schema.class.Ⲑid;
22
+ if (RootIndex.getFunctionMetadataFromClass(schema.class)?.synthetic && SYN_RE.test(schema.class.name)) {
23
+ if (!this.#schemaIdToName.has(id)) {
24
+ const name = schema.class.name.replace(SYN_RE, (_, pref, uid) => `__${(+uid % this.#base).toString().padStart(this.#digits, '0')}`);
25
+ this.#schemaIdToName.set(id, name);
26
+ }
27
+ return this.#schemaIdToName.get(id)!;
28
+ } else {
29
+ return schema.class.name;
30
+ }
31
+ }
32
+ }
@@ -189,7 +189,6 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
189
189
  baseType: RootIndex.getFunctionMetadata(cls)?.abstract,
190
190
  metadata: {},
191
191
  methods: {},
192
- externalName: cls.name.replace('Ⲑsyn', ''),
193
192
  views: {
194
193
  [AllViewⲐ]: {
195
194
  schema: {},
@@ -86,10 +86,6 @@ export interface ClassConfig extends DescribableConfig {
86
86
  * Method parameter configs
87
87
  */
88
88
  methods: Record<string, FieldConfig[]>;
89
- /**
90
- * Name for consuming clients
91
- */
92
- externalName: string;
93
89
  }
94
90
 
95
91
  /**
@@ -166,7 +162,7 @@ export interface FieldConfig extends DescribableConfig {
166
162
  /**
167
163
  * Default value
168
164
  */
169
- default?: Primitive;
165
+ default?: Primitive | [];
170
166
  /**
171
167
  * Is the field readonly, or write only?, defaults to no restrictions
172
168
  */
@@ -90,7 +90,10 @@ export class SchemaTransformUtil {
90
90
  } else if (!node.questionToken && !typeExpr.undefinable && !node.initializer) {
91
91
  attrs.push(state.factory.createPropertyAssignment('required', state.fromLiteral({ active: true })));
92
92
  }
93
- if (node.initializer && ts.isLiteralExpression(node.initializer)) {
93
+ if (node.initializer && (
94
+ ts.isLiteralExpression(node.initializer) ||
95
+ (ts.isArrayLiteralExpression(node.initializer) && node.initializer.elements.length === 0)
96
+ )) {
94
97
  attrs.push(state.factory.createPropertyAssignment('default', node.initializer));
95
98
  }
96
99
  } else {