exact-mirror 0.2.5 → 0.2.7

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.
@@ -10,6 +10,7 @@ export interface Instruction {
10
10
  unions: TypeCheck<any>[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
+ fromUnion?: boolean;
13
14
  /**
14
15
  * TypeCompiler is required when using Union
15
16
  *
package/dist/cjs/index.js CHANGED
@@ -68,11 +68,13 @@ var handleRecord = (schema, property, instruction) => {
68
68
  instruction.array++;
69
69
  let v = `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}`;
70
70
  const optionals = instruction.optionalsInArray[i + 1];
71
- if (optionals)
71
+ if (optionals) {
72
72
  for (let oi = 0; oi < optionals.length; oi++) {
73
73
  const target = `ar${i}v[ar${i}s[i]]${optionals[oi]}`;
74
74
  v += `;if(${target}===undefined)delete ${target}`;
75
75
  }
76
+ instruction.optionalsInArray[i + 1] = [];
77
+ }
76
78
  v += `}return ar${i}v})()`;
77
79
  return v;
78
80
  };
@@ -87,7 +89,11 @@ var handleTuple = (schema, property, instruction) => {
87
89
  if (i2 !== 0) v += ",";
88
90
  v += mirror(
89
91
  schema[i2],
90
- joinProperty(property, i2, instruction.parentIsOptional),
92
+ joinProperty(
93
+ property,
94
+ i2,
95
+ instruction.parentIsOptional || instruction.fromUnion
96
+ ),
91
97
  instruction
92
98
  );
93
99
  }
@@ -171,14 +177,16 @@ var handleUnion = (schemas, property, instruction) => {
171
177
  {
172
178
  ...instruction,
173
179
  recursion: instruction.recursion + 1,
174
- parentIsOptional: true
180
+ parentIsOptional: true,
181
+ fromUnion: true
175
182
  }
176
183
  )}}
177
184
  `;
178
185
  cleanThenCheck += (i ? "" : "let ") + "tmp=" + mirror(type, property, {
179
186
  ...instruction,
180
187
  recursion: instruction.recursion + 1,
181
- parentIsOptional: true
188
+ parentIsOptional: true,
189
+ fromUnion: true
182
190
  }) + `
183
191
  if(d.unions[${ui}][${i}].Check(tmp))return tmp
184
192
  `;
@@ -221,7 +229,8 @@ var mirror = (schema, property, instruction) => {
221
229
  const name = joinProperty(
222
230
  property,
223
231
  key,
224
- instruction.parentIsOptional
232
+ // If parent is a union, any property could be undefined
233
+ instruction.parentIsOptional || instruction.fromUnion
225
234
  );
226
235
  if (isOptional) {
227
236
  const index = instruction.array;
@@ -241,8 +250,11 @@ var mirror = (schema, property, instruction) => {
241
250
  }
242
251
  }
243
252
  const array = instruction.optionalsInArray;
244
- if (array[index]) array[index].push(refName);
245
- else array[index] = [refName];
253
+ if (array[index]) {
254
+ array[index].push(refName);
255
+ } else {
256
+ array[index] = [refName];
257
+ }
246
258
  } else {
247
259
  instruction.optionals.push(name);
248
260
  }
@@ -298,6 +310,7 @@ var mirror = (schema, property, instruction) => {
298
310
  const target = `ar${i}v[i]${optionals[oi]}`;
299
311
  v += `;if(${target}===undefined)delete ${target}`;
300
312
  }
313
+ instruction.optionalsInArray[i + 1] = [];
301
314
  }
302
315
  v += `}`;
303
316
  if (!isRoot) v += `return ar${i}v})(${property})`;
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export interface Instruction {
10
10
  unions: TypeCheck<any>[][];
11
11
  unionKeys: Record<string, 1>;
12
12
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
+ fromUnion?: boolean;
13
14
  /**
14
15
  * TypeCompiler is required when using Union
15
16
  *
package/dist/index.mjs CHANGED
@@ -41,11 +41,13 @@ var handleRecord = (schema, property, instruction) => {
41
41
  instruction.array++;
42
42
  let v = `(()=>{const ar${i}s=Object.keys(${property}),ar${i}v={};for(let i=0;i<ar${i}s.length;i++){const ar${i}p=${property}[ar${i}s[i]];ar${i}v[ar${i}s[i]]=${mirror(child, `ar${i}p`, instruction)}`;
43
43
  const optionals = instruction.optionalsInArray[i + 1];
44
- if (optionals)
44
+ if (optionals) {
45
45
  for (let oi = 0; oi < optionals.length; oi++) {
46
46
  const target = `ar${i}v[ar${i}s[i]]${optionals[oi]}`;
47
47
  v += `;if(${target}===undefined)delete ${target}`;
48
48
  }
49
+ instruction.optionalsInArray[i + 1] = [];
50
+ }
49
51
  v += `}return ar${i}v})()`;
50
52
  return v;
51
53
  };
@@ -60,7 +62,11 @@ var handleTuple = (schema, property, instruction) => {
60
62
  if (i2 !== 0) v += ",";
61
63
  v += mirror(
62
64
  schema[i2],
63
- joinProperty(property, i2, instruction.parentIsOptional),
65
+ joinProperty(
66
+ property,
67
+ i2,
68
+ instruction.parentIsOptional || instruction.fromUnion
69
+ ),
64
70
  instruction
65
71
  );
66
72
  }
@@ -144,14 +150,16 @@ var handleUnion = (schemas, property, instruction) => {
144
150
  {
145
151
  ...instruction,
146
152
  recursion: instruction.recursion + 1,
147
- parentIsOptional: true
153
+ parentIsOptional: true,
154
+ fromUnion: true
148
155
  }
149
156
  )}}
150
157
  `;
151
158
  cleanThenCheck += (i ? "" : "let ") + "tmp=" + mirror(type, property, {
152
159
  ...instruction,
153
160
  recursion: instruction.recursion + 1,
154
- parentIsOptional: true
161
+ parentIsOptional: true,
162
+ fromUnion: true
155
163
  }) + `
156
164
  if(d.unions[${ui}][${i}].Check(tmp))return tmp
157
165
  `;
@@ -194,7 +202,8 @@ var mirror = (schema, property, instruction) => {
194
202
  const name = joinProperty(
195
203
  property,
196
204
  key,
197
- instruction.parentIsOptional
205
+ // If parent is a union, any property could be undefined
206
+ instruction.parentIsOptional || instruction.fromUnion
198
207
  );
199
208
  if (isOptional) {
200
209
  const index = instruction.array;
@@ -214,8 +223,11 @@ var mirror = (schema, property, instruction) => {
214
223
  }
215
224
  }
216
225
  const array = instruction.optionalsInArray;
217
- if (array[index]) array[index].push(refName);
218
- else array[index] = [refName];
226
+ if (array[index]) {
227
+ array[index].push(refName);
228
+ } else {
229
+ array[index] = [refName];
230
+ }
219
231
  } else {
220
232
  instruction.optionals.push(name);
221
233
  }
@@ -271,6 +283,7 @@ var mirror = (schema, property, instruction) => {
271
283
  const target = `ar${i}v[i]${optionals[oi]}`;
272
284
  v += `;if(${target}===undefined)delete ${target}`;
273
285
  }
286
+ instruction.optionalsInArray[i + 1] = [];
274
287
  }
275
288
  v += `}`;
276
289
  if (!isRoot) v += `return ar${i}v})(${property})`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {