exact-mirror 0.2.6 → 1.0.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.
@@ -1,15 +1,49 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
- import type { TAnySchema, TModule } from '@sinclair/typebox';
3
- export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
1
+ import type { TSchema, TModule, Static } from 'typebox';
2
+ import type { Compile, Validator } from 'typebox/compile';
3
+ interface BaseSchema {
4
+ '~kind': string;
5
+ id?: string;
6
+ $id?: string;
7
+ type?: string;
8
+ $schema?: string;
9
+ const?: unknown[];
10
+ pattern?: string;
11
+ additionalItems?: boolean | AnySchema;
12
+ items?: AnySchema | AnySchema[];
13
+ required?: string[];
14
+ additionalProperties?: boolean | AnySchema;
15
+ definitions?: {
16
+ [name: string]: AnySchema;
17
+ };
18
+ properties?: {
19
+ [name: string]: AnySchema;
20
+ };
21
+ patternProperties?: {
22
+ [name: string]: AnySchema;
23
+ };
24
+ dependencies?: {
25
+ [name: string]: AnySchema | string[];
26
+ };
27
+ enum?: any[];
28
+ allOf?: AnySchema[];
29
+ anyOf?: AnySchema[];
30
+ oneOf?: AnySchema[];
31
+ not?: AnySchema;
32
+ $ref?: string;
33
+ $defs?: Record<string, AnySchema>;
34
+ }
35
+ type AnySchema = TSchema & BaseSchema;
36
+ export declare const mergeObjectIntersection: (schema: AnySchema) => AnySchema;
4
37
  type MaybeArray<T> = T | T[];
5
38
  export interface Instruction {
6
39
  optionals: string[];
7
40
  optionalsInArray: string[][];
8
41
  parentIsOptional: boolean;
9
42
  array: number;
10
- unions: TypeCheck<any>[][];
43
+ unions: Validator<any>[][];
11
44
  unionKeys: Record<string, 1>;
12
45
  sanitize: MaybeArray<(v: string) => string> | undefined;
46
+ fromUnion?: boolean;
13
47
  /**
14
48
  * TypeCompiler is required when using Union
15
49
  *
@@ -18,10 +52,10 @@ export interface Instruction {
18
52
  *
19
53
  * @default undefined
20
54
  */
21
- TypeCompiler?: typeof TypeCompiler;
55
+ Compile?: typeof Compile;
22
56
  typeCompilerWanred?: boolean;
23
- modules?: TModule<any, any>;
24
- definitions: Record<string, TAnySchema>;
57
+ modules?: TModule<{}>;
58
+ definitions: Record<string, AnySchema>;
25
59
  recursion: number;
26
60
  /**
27
61
  * @default 8
@@ -38,5 +72,5 @@ export interface Instruction {
38
72
  removeUnknownUnionType: boolean;
39
73
  }
40
74
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
41
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
75
+ export declare const createMirror: <T extends TSchema>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: Static<T>) => Static<T>);
42
76
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -26,9 +26,8 @@ __export(index_exports, {
26
26
  mergeObjectIntersection: () => mergeObjectIntersection
27
27
  });
28
28
  module.exports = __toCommonJS(index_exports);
29
- var import_compiler = require("@sinclair/typebox/compiler");
30
- var Kind = Symbol.for("TypeBox.Kind");
31
- var Hint = Symbol.for("TypeBox.Hint");
29
+ var Kind = "~kind";
30
+ var Hint = "~hint";
32
31
  var isSpecialProperty = (name) => /(\ |-|\t|\n|\.|\[|\]|\{|\})/.test(name) || !isNaN(+name[0]);
33
32
  var joinProperty = (v1, v2, isOptional = false) => {
34
33
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -89,7 +88,11 @@ var handleTuple = (schema, property, instruction) => {
89
88
  if (i2 !== 0) v += ",";
90
89
  v += mirror(
91
90
  schema[i2],
92
- joinProperty(property, i2, instruction.parentIsOptional),
91
+ joinProperty(
92
+ property,
93
+ i2,
94
+ instruction.parentIsOptional || instruction.fromUnion
95
+ ),
93
96
  instruction
94
97
  );
95
98
  }
@@ -120,7 +123,7 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
120
123
  return source;
121
124
  }
122
125
  var handleUnion = (schemas, property, instruction) => {
123
- if (instruction.TypeCompiler === void 0) {
126
+ if (instruction.Compile === void 0) {
124
127
  if (!instruction.typeCompilerWanred) {
125
128
  console.warn(
126
129
  new Error(
@@ -138,19 +141,16 @@ var handleUnion = (schemas, property, instruction) => {
138
141
  `;
139
142
  const unwrapRef = (type) => {
140
143
  if (!(Kind in type) || !type.$ref) return type;
141
- if (type[Kind] === "This") {
144
+ if (type[Kind] === "This")
142
145
  return deepClone(instruction.definitions[type.$ref]);
143
- } else if (type[Kind] === "Ref") {
146
+ else if (type[Kind] === "Cyclic") {
144
147
  if (!instruction.modules)
145
148
  console.warn(
146
149
  new Error(
147
150
  "[exact-mirror] modules is required when using nested cyclic reference"
148
151
  )
149
152
  );
150
- else
151
- return instruction.modules.Import(
152
- type.$ref
153
- );
153
+ else return instruction.modules.$defs[type.$ref];
154
154
  }
155
155
  return type;
156
156
  };
@@ -166,21 +166,23 @@ var handleUnion = (schemas, property, instruction) => {
166
166
  type.items[i2] = unwrapRef(type.items[i2]);
167
167
  else type.items = unwrapRef(type.items);
168
168
  }
169
- typeChecks.push(import_compiler.TypeCompiler.Compile(type));
169
+ typeChecks.push(instruction.Compile(type));
170
170
  v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
171
171
  type,
172
172
  property,
173
173
  {
174
174
  ...instruction,
175
175
  recursion: instruction.recursion + 1,
176
- parentIsOptional: true
176
+ parentIsOptional: true,
177
+ fromUnion: true
177
178
  }
178
179
  )}}
179
180
  `;
180
181
  cleanThenCheck += (i ? "" : "let ") + "tmp=" + mirror(type, property, {
181
182
  ...instruction,
182
183
  recursion: instruction.recursion + 1,
183
- parentIsOptional: true
184
+ parentIsOptional: true,
185
+ fromUnion: true
184
186
  }) + `
185
187
  if(d.unions[${ui}][${i}].Check(tmp))return tmp
186
188
  `;
@@ -192,7 +194,7 @@ if(d.unions[${ui}][${i}].Check(tmp))return tmp
192
194
  var mirror = (schema, property, instruction) => {
193
195
  if (!schema) return "";
194
196
  const isRoot = property === "v" && !instruction.unions.length;
195
- if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
197
+ if (Kind in schema && schema[Kind] === "~Cyclic" && schema.$ref in schema.$defs)
196
198
  return mirror(schema.$defs[schema.$ref], property, {
197
199
  ...instruction,
198
200
  definitions: Object.assign(instruction.definitions, schema.$defs)
@@ -223,7 +225,8 @@ var mirror = (schema, property, instruction) => {
223
225
  const name = joinProperty(
224
226
  property,
225
227
  key,
226
- instruction.parentIsOptional
228
+ // If parent is a union, any property could be undefined
229
+ instruction.parentIsOptional || instruction.fromUnion
227
230
  );
228
231
  if (isOptional) {
229
232
  const index = instruction.array;
@@ -267,7 +270,11 @@ var mirror = (schema, property, instruction) => {
267
270
  v += "}";
268
271
  break;
269
272
  case "array":
270
- if (schema.items.type !== "object" && schema.items.type !== "array") {
273
+ if (
274
+ // @ts-expect-error
275
+ schema.items.type !== "object" && // @ts-expect-error
276
+ schema.items.type !== "array"
277
+ ) {
271
278
  if (Array.isArray(schema.items)) {
272
279
  v = handleTuple(schema.items, property, instruction);
273
280
  break;
@@ -341,7 +348,7 @@ var mirror = (schema, property, instruction) => {
341
348
  return `${v}return x`;
342
349
  };
343
350
  var createMirror = (schema, {
344
- TypeCompiler: TypeCompiler2,
351
+ Compile,
345
352
  modules,
346
353
  definitions,
347
354
  sanitize: sanitize2,
@@ -357,7 +364,7 @@ var createMirror = (schema, {
357
364
  parentIsOptional: false,
358
365
  unions,
359
366
  unionKeys: {},
360
- TypeCompiler: TypeCompiler2,
367
+ Compile,
361
368
  modules,
362
369
  // @ts-ignore private property
363
370
  definitions: definitions ?? modules?.$defs ?? {},
package/dist/index.d.ts CHANGED
@@ -1,15 +1,49 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
- import type { TAnySchema, TModule } from '@sinclair/typebox';
3
- export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
1
+ import type { TSchema, TModule, Static } from 'typebox';
2
+ import type { Compile, Validator } from 'typebox/compile';
3
+ interface BaseSchema {
4
+ '~kind': string;
5
+ id?: string;
6
+ $id?: string;
7
+ type?: string;
8
+ $schema?: string;
9
+ const?: unknown[];
10
+ pattern?: string;
11
+ additionalItems?: boolean | AnySchema;
12
+ items?: AnySchema | AnySchema[];
13
+ required?: string[];
14
+ additionalProperties?: boolean | AnySchema;
15
+ definitions?: {
16
+ [name: string]: AnySchema;
17
+ };
18
+ properties?: {
19
+ [name: string]: AnySchema;
20
+ };
21
+ patternProperties?: {
22
+ [name: string]: AnySchema;
23
+ };
24
+ dependencies?: {
25
+ [name: string]: AnySchema | string[];
26
+ };
27
+ enum?: any[];
28
+ allOf?: AnySchema[];
29
+ anyOf?: AnySchema[];
30
+ oneOf?: AnySchema[];
31
+ not?: AnySchema;
32
+ $ref?: string;
33
+ $defs?: Record<string, AnySchema>;
34
+ }
35
+ type AnySchema = TSchema & BaseSchema;
36
+ export declare const mergeObjectIntersection: (schema: AnySchema) => AnySchema;
4
37
  type MaybeArray<T> = T | T[];
5
38
  export interface Instruction {
6
39
  optionals: string[];
7
40
  optionalsInArray: string[][];
8
41
  parentIsOptional: boolean;
9
42
  array: number;
10
- unions: TypeCheck<any>[][];
43
+ unions: Validator<any>[][];
11
44
  unionKeys: Record<string, 1>;
12
45
  sanitize: MaybeArray<(v: string) => string> | undefined;
46
+ fromUnion?: boolean;
13
47
  /**
14
48
  * TypeCompiler is required when using Union
15
49
  *
@@ -18,10 +52,10 @@ export interface Instruction {
18
52
  *
19
53
  * @default undefined
20
54
  */
21
- TypeCompiler?: typeof TypeCompiler;
55
+ Compile?: typeof Compile;
22
56
  typeCompilerWanred?: boolean;
23
- modules?: TModule<any, any>;
24
- definitions: Record<string, TAnySchema>;
57
+ modules?: TModule<{}>;
58
+ definitions: Record<string, AnySchema>;
25
59
  recursion: number;
26
60
  /**
27
61
  * @default 8
@@ -38,5 +72,5 @@ export interface Instruction {
38
72
  removeUnknownUnionType: boolean;
39
73
  }
40
74
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
41
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
75
+ export declare const createMirror: <T extends TSchema>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: Static<T>) => Static<T>);
42
76
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  // src/index.ts
2
- import { TypeCompiler } from "@sinclair/typebox/compiler";
3
- var Kind = Symbol.for("TypeBox.Kind");
4
- var Hint = Symbol.for("TypeBox.Hint");
2
+ var Kind = "~kind";
3
+ var Hint = "~hint";
5
4
  var isSpecialProperty = (name) => /(\ |-|\t|\n|\.|\[|\]|\{|\})/.test(name) || !isNaN(+name[0]);
6
5
  var joinProperty = (v1, v2, isOptional = false) => {
7
6
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -62,7 +61,11 @@ var handleTuple = (schema, property, instruction) => {
62
61
  if (i2 !== 0) v += ",";
63
62
  v += mirror(
64
63
  schema[i2],
65
- joinProperty(property, i2, instruction.parentIsOptional),
64
+ joinProperty(
65
+ property,
66
+ i2,
67
+ instruction.parentIsOptional || instruction.fromUnion
68
+ ),
66
69
  instruction
67
70
  );
68
71
  }
@@ -93,7 +96,7 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
93
96
  return source;
94
97
  }
95
98
  var handleUnion = (schemas, property, instruction) => {
96
- if (instruction.TypeCompiler === void 0) {
99
+ if (instruction.Compile === void 0) {
97
100
  if (!instruction.typeCompilerWanred) {
98
101
  console.warn(
99
102
  new Error(
@@ -111,19 +114,16 @@ var handleUnion = (schemas, property, instruction) => {
111
114
  `;
112
115
  const unwrapRef = (type) => {
113
116
  if (!(Kind in type) || !type.$ref) return type;
114
- if (type[Kind] === "This") {
117
+ if (type[Kind] === "This")
115
118
  return deepClone(instruction.definitions[type.$ref]);
116
- } else if (type[Kind] === "Ref") {
119
+ else if (type[Kind] === "Cyclic") {
117
120
  if (!instruction.modules)
118
121
  console.warn(
119
122
  new Error(
120
123
  "[exact-mirror] modules is required when using nested cyclic reference"
121
124
  )
122
125
  );
123
- else
124
- return instruction.modules.Import(
125
- type.$ref
126
- );
126
+ else return instruction.modules.$defs[type.$ref];
127
127
  }
128
128
  return type;
129
129
  };
@@ -139,21 +139,23 @@ var handleUnion = (schemas, property, instruction) => {
139
139
  type.items[i2] = unwrapRef(type.items[i2]);
140
140
  else type.items = unwrapRef(type.items);
141
141
  }
142
- typeChecks.push(TypeCompiler.Compile(type));
142
+ typeChecks.push(instruction.Compile(type));
143
143
  v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
144
144
  type,
145
145
  property,
146
146
  {
147
147
  ...instruction,
148
148
  recursion: instruction.recursion + 1,
149
- parentIsOptional: true
149
+ parentIsOptional: true,
150
+ fromUnion: true
150
151
  }
151
152
  )}}
152
153
  `;
153
154
  cleanThenCheck += (i ? "" : "let ") + "tmp=" + mirror(type, property, {
154
155
  ...instruction,
155
156
  recursion: instruction.recursion + 1,
156
- parentIsOptional: true
157
+ parentIsOptional: true,
158
+ fromUnion: true
157
159
  }) + `
158
160
  if(d.unions[${ui}][${i}].Check(tmp))return tmp
159
161
  `;
@@ -165,7 +167,7 @@ if(d.unions[${ui}][${i}].Check(tmp))return tmp
165
167
  var mirror = (schema, property, instruction) => {
166
168
  if (!schema) return "";
167
169
  const isRoot = property === "v" && !instruction.unions.length;
168
- if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
170
+ if (Kind in schema && schema[Kind] === "~Cyclic" && schema.$ref in schema.$defs)
169
171
  return mirror(schema.$defs[schema.$ref], property, {
170
172
  ...instruction,
171
173
  definitions: Object.assign(instruction.definitions, schema.$defs)
@@ -196,7 +198,8 @@ var mirror = (schema, property, instruction) => {
196
198
  const name = joinProperty(
197
199
  property,
198
200
  key,
199
- instruction.parentIsOptional
201
+ // If parent is a union, any property could be undefined
202
+ instruction.parentIsOptional || instruction.fromUnion
200
203
  );
201
204
  if (isOptional) {
202
205
  const index = instruction.array;
@@ -240,7 +243,11 @@ var mirror = (schema, property, instruction) => {
240
243
  v += "}";
241
244
  break;
242
245
  case "array":
243
- if (schema.items.type !== "object" && schema.items.type !== "array") {
246
+ if (
247
+ // @ts-expect-error
248
+ schema.items.type !== "object" && // @ts-expect-error
249
+ schema.items.type !== "array"
250
+ ) {
244
251
  if (Array.isArray(schema.items)) {
245
252
  v = handleTuple(schema.items, property, instruction);
246
253
  break;
@@ -314,7 +321,7 @@ var mirror = (schema, property, instruction) => {
314
321
  return `${v}return x`;
315
322
  };
316
323
  var createMirror = (schema, {
317
- TypeCompiler: TypeCompiler2,
324
+ Compile,
318
325
  modules,
319
326
  definitions,
320
327
  sanitize: sanitize2,
@@ -330,7 +337,7 @@ var createMirror = (schema, {
330
337
  parentIsOptional: false,
331
338
  unions,
332
339
  unionKeys: {},
333
- TypeCompiler: TypeCompiler2,
340
+ Compile,
334
341
  modules,
335
342
  // @ts-ignore private property
336
343
  definitions: definitions ?? modules?.$defs ?? {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.2.6",
3
+ "version": "1.0.0",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -11,21 +11,22 @@
11
11
  "release": "npm run build && npm run test && npm publish --access public"
12
12
  },
13
13
  "peerDependencies": {
14
- "@sinclair/typebox": "^0.34.15"
14
+ "typebox": ">= 1.1.0"
15
15
  },
16
16
  "peerDependenciesMeta": {
17
- "@sinclair/typebox": {
17
+ "typebox": {
18
18
  "optional": true
19
19
  }
20
20
  },
21
21
  "devDependencies": {
22
- "@types/bun": "1.2.2",
23
- "elysia": "^1.2.11",
22
+ "@types/bun": "1.3.8",
23
+ "elysia": "^1.4.28",
24
24
  "eslint": "9.6.0",
25
25
  "mitata": "^1.0.33",
26
26
  "tsup": "^8.1.0",
27
27
  "tsx": "^4.19.2",
28
- "typescript": "^5.5.3"
28
+ "typebox": "^1.1.15",
29
+ "typescript": "^6.0.0"
29
30
  },
30
31
  "main": "./dist/cjs/index.js",
31
32
  "module": "./dist/index.mjs",