Generated-output differences

quicktype output changed between the PR base and tested PR merge revisions.
← Back to the pull request
1test cases
1files differ
0modified
1new
0deleted
219changed lines
+219 −0insertions / deletions
Base a49b94e8f6f1b319a4da9fb15d57ef13ba316ddc · PR merge 30e7aa7019af5ee6b58ad939b5bc4b114dd5e4af · Head 63dae59b06e188520579fdb1b26e11b49b7f1f52 · raw patch
Test case

test/inputs/schema/multi-source-1543

1 generated file · +219 −0
Aschema-typescriptdefault / TopLevel.ts+219 −0
@@ -0,0 +1,219 @@
1+// To parse this data:
2+//
3+// import { Convert, OrderCustomer, TopLevel } from "./TopLevel";
4+//
5+// const orderCustomer = Convert.toOrderCustomer(json);
6+// const topLevel = Convert.toTopLevel(json);
7+//
8+// These functions will throw an error if the JSON doesn't
9+// match the expected interface, even if the JSON is valid.
10+
11+export interface TopLevel {
12+ external_order_ref?: string;
13+ info: OrderCustomer;
14+ [property: string]: unknown;
15+}
16+
17+export interface OrderCustomer {
18+ country?: string;
19+ email?: string;
20+ first_name?: string;
21+ last_name?: string;
22+ newsletter?: string;
23+ order_number: string;
24+ phone?: string;
25+ post_code?: string;
26+ type?: string;
27+ [property: string]: unknown;
28+}
29+
30+// Converts JSON strings to/from your types
31+// and asserts the results of JSON.parse at runtime
32+export class Convert {
33+ public static toOrderCustomer(json: string): OrderCustomer {
34+ return cast(JSON.parse(json), r("OrderCustomer"));
35+ }
36+
37+ public static orderCustomerToJson(value: OrderCustomer): string {
38+ return JSON.stringify(uncast(value, r("OrderCustomer")), null, 2);
39+ }
40+
41+ public static toTopLevel(json: string): TopLevel {
42+ return cast(JSON.parse(json), r("TopLevel"));
43+ }
44+
45+ public static topLevelToJson(value: TopLevel): string {
46+ return JSON.stringify(uncast(value, r("TopLevel")), null, 2);
47+ }
48+}
49+
50+function invalidValue(typ: any, val: any, key: any, parent: any = ''): never {
51+ const prettyTyp = prettyTypeName(typ);
52+ const parentText = parent ? ` on ${parent}` : '';
53+ const keyText = key ? ` for key "${key}"` : '';
54+ throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
55+}
56+
57+function prettyTypeName(typ: any): string {
58+ if (Array.isArray(typ)) {
59+ if (typ.length === 2 && typ[0] === undefined) {
60+ return `an optional ${prettyTypeName(typ[1])}`;
61+ } else {
62+ return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`;
63+ }
64+ } else if (typeof typ === "object" && typ.literal !== undefined) {
65+ return typ.literal;
66+ } else {
67+ return typeof typ;
68+ }
69+}
70+
71+function jsonToJSProps(typ: any): any {
72+ if (typ.jsonToJS === undefined) {
73+ const map: any = {};
74+ typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ });
75+ typ.jsonToJS = map;
76+ }
77+ return typ.jsonToJS;
78+}
79+
80+function jsToJSONProps(typ: any): any {
81+ if (typ.jsToJSON === undefined) {
82+ const map: any = {};
83+ typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ });
84+ typ.jsToJSON = map;
85+ }
86+ return typ.jsToJSON;
87+}
88+
89+function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any {
90+ function transformPrimitive(typ: string, val: any): any {
91+ if (typeof typ === typeof val) return val;
92+ return invalidValue(typ, val, key, parent);
93+ }
94+
95+ function transformUnion(typs: any[], val: any): any {
96+ // val must validate against one typ in typs
97+ const l = typs.length;
98+ for (let i = 0; i < l; i++) {
99+ const typ = typs[i];
100+ try {
101+ return transform(val, typ, getProps);
102+ } catch (_) {}
103+ }
104+ return invalidValue(typs, val, key, parent);
105+ }
106+
107+ function transformEnum(cases: string[], val: any): any {
108+ if (cases.indexOf(val) !== -1) return val;
109+ return invalidValue(cases.map(a => { return l(a); }), val, key, parent);
110+ }
111+
112+ function transformArray(typ: any, val: any): any {
113+ // val must be an array with no invalid elements
114+ if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent);
115+ return val.map(el => transform(el, typ, getProps));
116+ }
117+
118+ function transformDate(val: any): any {
119+ if (val === null) {
120+ return null;
121+ }
122+ const d = new Date(val);
123+ if (isNaN(d.valueOf())) {
124+ return invalidValue(l("Date"), val, key, parent);
125+ }
126+ return d;
127+ }
128+
129+ function transformObject(props: { [k: string]: any }, additional: any, val: any): any {
130+ if (val === null || typeof val !== "object" || Array.isArray(val)) {
131+ return invalidValue(l(ref || "object"), val, key, parent);
132+ }
133+ const result: any = {};
134+ Object.getOwnPropertyNames(props).forEach(key => {
135+ const prop = props[key];
136+ const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
137+ result[prop.key] = transform(v, prop.typ, getProps, key, ref);
138+ });
139+ Object.getOwnPropertyNames(val).forEach(key => {
140+ if (!Object.prototype.hasOwnProperty.call(props, key)) {
141+ result[key] = transform(val[key], additional, getProps, key, ref);
142+ }
143+ });
144+ return result;
145+ }
146+
147+ if (typ === "any") return val;
148+ if (typ === null) {
149+ if (val === null) return val;
150+ return invalidValue(typ, val, key, parent);
151+ }
152+ if (typ === false) return invalidValue(typ, val, key, parent);
153+ let ref: any = undefined;
154+ while (typeof typ === "object" && typ.ref !== undefined) {
155+ ref = typ.ref;
156+ typ = typeMap[typ.ref];
157+ }
158+ if (Array.isArray(typ)) return transformEnum(typ, val);
159+ if (typeof typ === "object") {
160+ return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
161+ : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
162+ : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
163+ : invalidValue(typ, val, key, parent);
164+ }
165+ // Numbers can be parsed by Date but shouldn't be.
166+ if (typ === Date && typeof val !== "number") return transformDate(val);
167+ return transformPrimitive(typ, val);
168+}
169+
170+function cast<T>(val: any, typ: any): T {
171+ return transform(val, typ, jsonToJSProps);
172+}
173+
174+function uncast<T>(val: T, typ: any): any {
175+ return transform(val, typ, jsToJSONProps);
176+}
177+
178+function l(typ: any) {
179+ return { literal: typ };
180+}
181+
182+function a(typ: any) {
183+ return { arrayItems: typ };
184+}
185+
186+function u(...typs: any[]) {
187+ return { unionMembers: typs };
188+}
189+
190+function o(props: any[], additional: any) {
191+ return { props, additional };
192+}
193+
194+function m(additional: any) {
195+ const props: any[] = [];
196+ return { props, additional };
197+}
198+
199+function r(name: string) {
200+ return { ref: name };
201+}
202+
203+const typeMap: any = {
204+ "TopLevel": o([
205+ { json: "external_order_ref", js: "external_order_ref", typ: u(undefined, "") },
206+ { json: "info", js: "info", typ: r("OrderCustomer") },
207+ ], "any"),
208+ "OrderCustomer": o([
209+ { json: "country", js: "country", typ: u(undefined, "") },
210+ { json: "email", js: "email", typ: u(undefined, "") },
211+ { json: "first_name", js: "first_name", typ: u(undefined, "") },
212+ { json: "last_name", js: "last_name", typ: u(undefined, "") },
213+ { json: "newsletter", js: "newsletter", typ: u(undefined, "") },
214+ { json: "order_number", js: "order_number", typ: "" },
215+ { json: "phone", js: "phone", typ: u(undefined, "") },
216+ { json: "post_code", js: "post_code", typ: u(undefined, "") },
217+ { json: "type", js: "type", typ: u(undefined, "") },
218+ ], "any"),
219+};
No generated files match these filters.