quicktype-core 7.0.0 → 7.0.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.
@@ -214,11 +214,25 @@ class JavaScriptRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
214
214
  const { any: anyAnnotation, anyArray: anyArrayAnnotation, anyMap: anyMapAnnotation, string: stringAnnotation, stringArray: stringArrayAnnotation, never: neverAnnotation } = this.typeAnnotations;
215
215
  this.ensureBlankLine();
216
216
  this
217
- .emitMultiline(`function invalidValue(typ${anyAnnotation}, val${anyAnnotation}, key${anyAnnotation} = '')${neverAnnotation} {
218
- if (key) {
219
- throw Error(\`Invalid value for key \"\${key}\". Expected type \${JSON.stringify(typ)} but got \${JSON.stringify(val)}\`);
217
+ .emitMultiline(`function invalidValue(typ${anyAnnotation}, val${anyAnnotation}, key${anyAnnotation}, parent${anyAnnotation} = '')${neverAnnotation} {
218
+ const prettyTyp = prettyTypeName(typ);
219
+ const parentText = parent ? \` on \${parent}\` : '';
220
+ const keyText = key ? \` for key "\${key}"\` : '';
221
+ throw Error(\`Invalid value\${keyText}\${parentText}. Expected \${prettyTyp} but got \${JSON.stringify(val)}\`);
222
+ }
223
+
224
+ function prettyTypeName(typ${anyAnnotation})${stringAnnotation} {
225
+ if (Array.isArray(typ)) {
226
+ if (typ.length === 2 && typ[0] === undefined) {
227
+ return \`an optional \${prettyTypeName(typ[1])}\`;
228
+ } else {
229
+ return \`one of [\${typ.map(a => { return prettyTypeName(a); }).join(", ")}]\`;
230
+ }
231
+ } else if (typeof typ === "object" && typ.literal !== undefined) {
232
+ return typ.literal;
233
+ } else {
234
+ return typeof typ;
220
235
  }
221
- throw Error(\`Invalid value \${JSON.stringify(val)} for type \${JSON.stringify(typ)}\`, );
222
236
  }
223
237
 
224
238
  function jsonToJSProps(typ${anyAnnotation})${anyAnnotation} {
@@ -239,10 +253,10 @@ function jsToJSONProps(typ${anyAnnotation})${anyAnnotation} {
239
253
  return typ.jsToJSON;
240
254
  }
241
255
 
242
- function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnotation}, key${anyAnnotation} = '')${anyAnnotation} {
256
+ function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnotation}, key${anyAnnotation} = '', parent${anyAnnotation} = '')${anyAnnotation} {
243
257
  function transformPrimitive(typ${stringAnnotation}, val${anyAnnotation})${anyAnnotation} {
244
258
  if (typeof typ === typeof val) return val;
245
- return invalidValue(typ, val, key);
259
+ return invalidValue(typ, val, key, parent);
246
260
  }
247
261
 
248
262
  function transformUnion(typs${anyArrayAnnotation}, val${anyAnnotation})${anyAnnotation} {
@@ -254,17 +268,17 @@ function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnota
254
268
  return transform(val, typ, getProps);
255
269
  } catch (_) {}
256
270
  }
257
- return invalidValue(typs, val);
271
+ return invalidValue(typs, val, key, parent);
258
272
  }
259
273
 
260
274
  function transformEnum(cases${stringArrayAnnotation}, val${anyAnnotation})${anyAnnotation} {
261
275
  if (cases.indexOf(val) !== -1) return val;
262
- return invalidValue(cases, val);
276
+ return invalidValue(cases.map(a => { return l(a); }), val, key, parent);
263
277
  }
264
278
 
265
279
  function transformArray(typ${anyAnnotation}, val${anyAnnotation})${anyAnnotation} {
266
280
  // val must be an array with no invalid elements
267
- if (!Array.isArray(val)) return invalidValue("array", val);
281
+ if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent);
268
282
  return val.map(el => transform(el, typ, getProps));
269
283
  }
270
284
 
@@ -274,26 +288,26 @@ function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnota
274
288
  }
275
289
  const d = new Date(val);
276
290
  if (isNaN(d.valueOf())) {
277
- return invalidValue("Date", val);
291
+ return invalidValue(l("Date"), val, key, parent);
278
292
  }
279
293
  return d;
280
294
  }
281
295
 
282
296
  function transformObject(props${anyMapAnnotation}, additional${anyAnnotation}, val${anyAnnotation})${anyAnnotation} {
283
297
  if (val === null || typeof val !== "object" || Array.isArray(val)) {
284
- return invalidValue("object", val);
298
+ return invalidValue(l(ref || "object"), val, key, parent);
285
299
  }
286
300
  const result${anyAnnotation} = {};
287
301
  Object.getOwnPropertyNames(props).forEach(key => {
288
302
  const prop = props[key];
289
303
  const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
290
- result[prop.key] = transform(v, prop.typ, getProps, prop.key);
304
+ result[prop.key] = transform(v, prop.typ, getProps, key, ref);
291
305
  });
292
306
  Object.getOwnPropertyNames(val).forEach(key => {
293
307
  if (!Object.prototype.hasOwnProperty.call(props, key)) {
294
308
  result[key] = ${this._jsOptions.runtimeTypecheckIgnoreUnknownProperties
295
309
  ? `val[key]`
296
- : `transform(val[key], additional, getProps, key)`};
310
+ : `transform(val[key], additional, getProps, key, ref)`};
297
311
  }
298
312
  });
299
313
  return result;
@@ -302,10 +316,12 @@ function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnota
302
316
  if (typ === "any") return val;
303
317
  if (typ === null) {
304
318
  if (val === null) return val;
305
- return invalidValue(typ, val);
319
+ return invalidValue(typ, val, key, parent);
306
320
  }
307
- if (typ === false) return invalidValue(typ, val);
321
+ if (typ === false) return invalidValue(typ, val, key, parent);
322
+ let ref = undefined;
308
323
  while (typeof typ === "object" && typ.ref !== undefined) {
324
+ ref = typ.ref;
309
325
  typ = typeMap[typ.ref];
310
326
  }
311
327
  if (Array.isArray(typ)) return transformEnum(typ, val);
@@ -313,7 +329,7 @@ function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnota
313
329
  return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
314
330
  : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
315
331
  : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
316
- : invalidValue(typ, val);
332
+ : invalidValue(typ, val, key, parent);
317
333
  }
318
334
  // Numbers can be parsed by Date but shouldn't be.
319
335
  if (typ === Date && typeof val !== "number") return transformDate(val);
@@ -328,6 +344,10 @@ ${this.castFunctionLines[1]} {
328
344
  return transform(val, typ, jsToJSONProps);
329
345
  }
330
346
 
347
+ function l(typ${anyAnnotation}) {
348
+ return { literal: typ };
349
+ }
350
+
331
351
  function a(typ${anyAnnotation}) {
332
352
  return { arrayItems: typ };
333
353
  }
@@ -134,7 +134,6 @@ const keywords = [
134
134
  "continue",
135
135
  "default",
136
136
  "defer",
137
- "description",
138
137
  "do",
139
138
  "else",
140
139
  "fallthrough",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "7.0.0",
3
+ "version": "7.0.2",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",
@@ -45,6 +45,6 @@
45
45
  "fs": false
46
46
  },
47
47
  "config": {
48
- "commit": "3118cbe3f215406f704558a1e5e07e5160b5fcff"
48
+ "commit": "5dbf11c85037e3f6a1ae842cf124c14546827a43"
49
49
  }
50
50
  }