@travetto/schema 5.0.0-rc.10 → 5.0.0-rc.12

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
@@ -379,7 +379,7 @@ Validation Failed {
379
379
  ```
380
380
 
381
381
  ## Data Utilities
382
- Data utilities for binding values, and type conversion. Currently [DataUtil](https://github.com/travetto/travetto/tree/main/module/schema/src/data.ts#L10) includes:
382
+ Data utilities for binding values, and type conversion. Currently [DataUtil](https://github.com/travetto/travetto/tree/main/module/schema/src/data.ts#L11) includes:
383
383
  * `deepAssign(a, b, mode ?)` which allows for deep assignment of `b` onto `a`, the `mode` determines how aggressive the assignment is, and how flexible it is. `mode` can have any of the following values:
384
384
  * `loose`, which is the default is the most lenient. It will not error out, and overwrites will always happen
385
385
  * `coerce`, will attempt to force values from `b` to fit the types of `a`, and if it can't it will error out
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema",
3
- "version": "5.0.0-rc.10",
3
+ "version": "5.0.0-rc.12",
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": "^5.0.0-rc.9"
30
+ "@travetto/registry": "^5.0.0-rc.11"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^5.0.0-rc.6"
33
+ "@travetto/transformer": "^5.0.0-rc.7"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/data.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isNumberObject as isNum, isBooleanObject as isBool, isStringObject as isStr } from 'node:util/types';
2
2
 
3
3
  import { asConstructable, castTo, Class, asFull, TypedObject } from '@travetto/runtime';
4
+ import { UnknownType } from './internal/types';
4
5
 
5
6
  const REGEX_PAT = /[\/](.*)[\/](i|g|m|s)?/;
6
7
 
@@ -120,9 +121,11 @@ export class DataUtil {
120
121
  */
121
122
  static coerceType(input: unknown, type: typeof String, strict?: boolean): string;
122
123
  static coerceType(input: unknown, type: typeof Number, strict?: boolean): number;
124
+ static coerceType(input: unknown, type: typeof BigInt, strict?: boolean): bigint;
123
125
  static coerceType(input: unknown, type: typeof Boolean, strict?: boolean): boolean;
124
126
  static coerceType(input: unknown, type: typeof Date, strict?: boolean): Date;
125
127
  static coerceType(input: unknown, type: typeof RegExp, strict?: boolean): RegExp;
128
+ static coerceType(input: unknown, type: typeof UnknownType, strict?: boolean): unknown;
126
129
  static coerceType<T>(input: unknown, type: Class<T> | Function, strict?: boolean): T;
127
130
  static coerceType(input: unknown, type: Class<unknown> | Function, strict = true): unknown {
128
131
  // Do nothing
@@ -160,6 +163,20 @@ export class DataUtil {
160
163
  }
161
164
  return res;
162
165
  }
166
+ case BigInt: {
167
+ if (typeof input === 'bigint') {
168
+ return input;
169
+ }
170
+ try {
171
+ return BigInt((typeof input === 'boolean' || typeof input === 'number') ?
172
+ input : `${input}`.replace(/n$/i, ''));
173
+ } catch {
174
+ if (strict) {
175
+ throw new Error(`Invalid numeric value: ${input}`);
176
+ }
177
+ return;
178
+ }
179
+ }
163
180
  case Boolean: {
164
181
  const match = `${input}`.match(/^((?<TRUE>true|yes|1|on)|false|no|off|0)$/i);
165
182
  if (strict && !match) {
@@ -184,6 +201,9 @@ export class DataUtil {
184
201
  return;
185
202
  }
186
203
  }
204
+ case UnknownType: {
205
+ return input;
206
+ }
187
207
  case Object: {
188
208
  if (!strict || this.isPlainObject(input)) {
189
209
  return input;
@@ -1 +1,2 @@
1
- export const AllViewⲐ: unique symbol = Symbol.for('@travetto/schema:all');
1
+ export const AllViewⲐ: unique symbol = Symbol.for('@travetto/schema:all');
2
+ export class UnknownType { }
@@ -255,6 +255,9 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
255
255
  if (config.specifiers) {
256
256
  config.specifiers = [...params[idx]?.specifiers ?? [], ...config.specifiers];
257
257
  }
258
+ if (config.enum?.values) {
259
+ config.enum.values = config.enum.values.slice().sort();
260
+ }
258
261
 
259
262
  params[idx] = {
260
263
  // @ts-expect-error
@@ -287,6 +290,9 @@ class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> {
287
290
  if (config.specifiers) {
288
291
  config.specifiers = [...allViewConf.schema[prop].specifiers ?? [], ...config.specifiers];
289
292
  }
293
+ if (config.enum?.values) {
294
+ config.enum.values = config.enum.values.slice().sort();
295
+ }
290
296
 
291
297
  Object.assign(allViewConf.schema[prop], config);
292
298
 
@@ -7,6 +7,7 @@ import {
7
7
  const SCHEMA_MOD = '@travetto/schema/src/decorator/schema';
8
8
  const FIELD_MOD = '@travetto/schema/src/decorator/field';
9
9
  const COMMON_MOD = '@travetto/schema/src/decorator/common';
10
+ const TYPES_FILE = '@travetto/schema/src/internal/types';
10
11
 
11
12
  export class SchemaTransformUtil {
12
13
 
@@ -27,6 +28,10 @@ export class SchemaTransformUtil {
27
28
  }
28
29
  break;
29
30
  }
31
+ case 'unknown': {
32
+ const imp = state.importFile(TYPES_FILE);
33
+ return state.createAccess(imp.ident, 'UnknownType');
34
+ }
30
35
  case 'shape': {
31
36
  const uniqueId = state.generateUniqueIdentifier(node, type);
32
37
 
@@ -49,7 +54,7 @@ export class SchemaTransformUtil {
49
54
  this.computeField(state, state.factory.createPropertyDeclaration(
50
55
  [], k,
51
56
  v.undefinable || v.nullable ? state.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
52
- undefined, undefined
57
+ v.key === 'unknown' ? state.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword) : undefined, undefined
53
58
  ), { type: v, root })
54
59
  )
55
60
  );
@@ -70,7 +75,6 @@ export class SchemaTransformUtil {
70
75
  break;
71
76
  }
72
77
  case 'foreign':
73
- case 'unknown':
74
78
  default: {
75
79
  // Object
76
80
  }
@@ -126,8 +130,7 @@ export class SchemaTransformUtil {
126
130
  // If we have a union type
127
131
  if (primaryExpr.key === 'union') {
128
132
  const values = primaryExpr.subTypes.map(x => x.key === 'literal' ? x.value : undefined)
129
- .filter(x => x !== undefined && x !== null)
130
- .sort();
133
+ .filter(x => x !== undefined && x !== null);
131
134
 
132
135
  if (values.length === primaryExpr.subTypes.length) {
133
136
  attrs.push(state.factory.createPropertyAssignment('enum', state.fromLiteral({