exact-mirror 0.1.3 → 0.1.5

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/dist/cjs/index.js CHANGED
@@ -66,7 +66,15 @@ var handleRecord = (schema, property, instruction) => {
66
66
  if (!child) return property;
67
67
  const i = instruction.array;
68
68
  instruction.array++;
69
- return `(()=>{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)}}return ar${i}v})()`;
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
+ const optionals = instruction.optionalsInArray[i + 1];
71
+ if (optionals)
72
+ for (let oi = 0; oi < optionals.length; oi++) {
73
+ const target = `ar${i}v[ar${i}s[i]].${optionals[oi]}`;
74
+ v += `;if(${target}===undefined)delete ${target}`;
75
+ }
76
+ v += `}return ar${i}v})()`;
77
+ return v;
70
78
  };
71
79
  var handleTuple = (schema, property, instruction) => {
72
80
  const i = instruction.array;
@@ -192,11 +200,15 @@ var mirror = (schema, property, instruction) => {
192
200
  }
193
201
  schema = mergeObjectIntersection(schema);
194
202
  v += "{";
195
- if (schema.additionalProperties) v += `...${property}`;
203
+ if (schema.additionalProperties) v += `...${property},`;
196
204
  const keys = Object.keys(schema.properties);
197
205
  for (let i2 = 0; i2 < keys.length; i2++) {
198
206
  const key = keys[i2];
199
- let isOptional = schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf);
207
+ let isOptional = (
208
+ // all fields are optional
209
+ !schema.required || // field is explicitly required
210
+ schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf)
211
+ );
200
212
  const name = joinProperty(
201
213
  property,
202
214
  key,
@@ -214,8 +226,6 @@ var mirror = (schema, property, instruction) => {
214
226
  }
215
227
  }
216
228
  const child = schema.properties[key];
217
- if (schema.additionalProperties && child.type !== "object")
218
- continue;
219
229
  if (i2 !== 0) v += ",";
220
230
  v += `${encodeProperty(key)}:${isOptional ? `${name}===undefined?undefined:` : ""}${mirror(
221
231
  child,
@@ -284,15 +294,19 @@ var mirror = (schema, property, instruction) => {
284
294
  break;
285
295
  }
286
296
  if (!isRoot) return v;
287
- if (schema.type === "array") return `${v}return ar0v`;
288
- v = `const x=${v}
297
+ if (schema.type === "array") {
298
+ v = `${v}const x=ar0v;`;
299
+ } else {
300
+ v = `const x=${v}
289
301
  `;
302
+ }
290
303
  for (let i = 0; i < instruction.optionals.length; i++) {
291
304
  const key = instruction.optionals[i];
292
305
  const prop = key.slice(1);
293
306
  v += `if(${key}===undefined`;
294
307
  if (instruction.unionKeys[key]) v += `||x${prop}===undefined`;
295
- v += `)delete x${prop.charCodeAt(0) !== 63 ? "?" : ""}${prop}
308
+ const shouldQuestion = prop.charCodeAt(0) !== 63 && schema.type !== "array";
309
+ v += `)delete x${shouldQuestion ? "?" : ""}${prop}
296
310
  `;
297
311
  }
298
312
  return `${v}return x`;
package/dist/index.mjs CHANGED
@@ -39,7 +39,15 @@ var handleRecord = (schema, property, instruction) => {
39
39
  if (!child) return property;
40
40
  const i = instruction.array;
41
41
  instruction.array++;
42
- return `(()=>{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)}}return ar${i}v})()`;
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
+ const optionals = instruction.optionalsInArray[i + 1];
44
+ if (optionals)
45
+ for (let oi = 0; oi < optionals.length; oi++) {
46
+ const target = `ar${i}v[ar${i}s[i]].${optionals[oi]}`;
47
+ v += `;if(${target}===undefined)delete ${target}`;
48
+ }
49
+ v += `}return ar${i}v})()`;
50
+ return v;
43
51
  };
44
52
  var handleTuple = (schema, property, instruction) => {
45
53
  const i = instruction.array;
@@ -165,11 +173,15 @@ var mirror = (schema, property, instruction) => {
165
173
  }
166
174
  schema = mergeObjectIntersection(schema);
167
175
  v += "{";
168
- if (schema.additionalProperties) v += `...${property}`;
176
+ if (schema.additionalProperties) v += `...${property},`;
169
177
  const keys = Object.keys(schema.properties);
170
178
  for (let i2 = 0; i2 < keys.length; i2++) {
171
179
  const key = keys[i2];
172
- let isOptional = schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf);
180
+ let isOptional = (
181
+ // all fields are optional
182
+ !schema.required || // field is explicitly required
183
+ schema.required && !schema.required.includes(key) || Array.isArray(schema.properties[key].anyOf)
184
+ );
173
185
  const name = joinProperty(
174
186
  property,
175
187
  key,
@@ -187,8 +199,6 @@ var mirror = (schema, property, instruction) => {
187
199
  }
188
200
  }
189
201
  const child = schema.properties[key];
190
- if (schema.additionalProperties && child.type !== "object")
191
- continue;
192
202
  if (i2 !== 0) v += ",";
193
203
  v += `${encodeProperty(key)}:${isOptional ? `${name}===undefined?undefined:` : ""}${mirror(
194
204
  child,
@@ -257,15 +267,19 @@ var mirror = (schema, property, instruction) => {
257
267
  break;
258
268
  }
259
269
  if (!isRoot) return v;
260
- if (schema.type === "array") return `${v}return ar0v`;
261
- v = `const x=${v}
270
+ if (schema.type === "array") {
271
+ v = `${v}const x=ar0v;`;
272
+ } else {
273
+ v = `const x=${v}
262
274
  `;
275
+ }
263
276
  for (let i = 0; i < instruction.optionals.length; i++) {
264
277
  const key = instruction.optionals[i];
265
278
  const prop = key.slice(1);
266
279
  v += `if(${key}===undefined`;
267
280
  if (instruction.unionKeys[key]) v += `||x${prop}===undefined`;
268
- v += `)delete x${prop.charCodeAt(0) !== 63 ? "?" : ""}${prop}
281
+ const shouldQuestion = prop.charCodeAt(0) !== 63 && schema.type !== "array";
282
+ v += `)delete x${shouldQuestion ? "?" : ""}${prop}
269
283
  `;
270
284
  }
271
285
  return `${v}return x`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {