Skip to content

Commit 8563102

Browse files
authored
Merge pull request #1155 from SrRickGrimes/main
added validation check to see if the node is an Array
2 parents 16a68d0 + da724a2 commit 8563102

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

packages/serialization/json/src/jsonParseNode.ts

+27-14
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ export class JsonParseNode implements ParseNode {
3939
typeof this._jsonNode === "object" &&
4040
(this._jsonNode as { [key: string]: any })[identifier] !== undefined
4141
? new JsonParseNode(
42-
(this._jsonNode as { [key: string]: any })[identifier],
42+
(this._jsonNode as { [key: string]: any })[identifier]
4343
)
4444
: undefined;
4545
public getBooleanValue = () => this._jsonNode as boolean;
4646
public getNumberValue = () => this._jsonNode as number;
4747
public getGuidValue = () => parseGuidString(this.getStringValue());
48-
public getDateValue = () => this._jsonNode ? new Date(this._jsonNode as string) : undefined;
48+
public getDateValue = () =>
49+
this._jsonNode ? new Date(this._jsonNode as string) : undefined;
4950
public getDateOnlyValue = () => DateOnly.parse(this.getStringValue());
5051
public getTimeOnlyValue = () => TimeOnly.parse(this.getStringValue());
5152
public getDurationValue = () => Duration.parse(this.getStringValue());
5253
public getCollectionOfPrimitiveValues = <T>(): T[] | undefined => {
54+
if (!Array.isArray(this._jsonNode)){
55+
return undefined;
56+
}
5357
return (this._jsonNode as unknown[]).map((x) => {
5458
const currentParseNode = new JsonParseNode(x);
5559
const typeOfX = typeof x;
@@ -69,7 +73,7 @@ export class JsonParseNode implements ParseNode {
6973
return currentParseNode.getDateValue() as unknown as T;
7074
} else {
7175
throw new Error(
72-
`encountered an unknown type during deserialization ${typeof x}`,
76+
`encountered an unknown type during deserialization ${typeof x}`
7377
);
7478
}
7579
});
@@ -82,15 +86,20 @@ export class JsonParseNode implements ParseNode {
8286
return undefined;
8387
}
8488
public getCollectionOfObjectValues = <T extends Parsable>(
85-
method: ParsableFactory<T>,
89+
method: ParsableFactory<T>
8690
): T[] | undefined => {
87-
return this._jsonNode ? (this._jsonNode as unknown[])
88-
.map((x) => new JsonParseNode(x))
89-
.map((x) => x.getObjectValue<T>(method)) : undefined;
91+
if (!Array.isArray(this._jsonNode)){
92+
return undefined;
93+
}
94+
return this._jsonNode
95+
? (this._jsonNode as unknown[])
96+
.map((x) => new JsonParseNode(x))
97+
.map((x) => x.getObjectValue<T>(method))
98+
: undefined;
9099
};
91100

92101
public getObjectValue = <T extends Parsable>(
93-
parsableFactory: ParsableFactory<T>,
102+
parsableFactory: ParsableFactory<T>
94103
): T => {
95104
const temp: T = {} as T;
96105
if (isUntypedNode(parsableFactory(this)(temp))) {
@@ -108,16 +117,16 @@ export class JsonParseNode implements ParseNode {
108117
(this._jsonNode as any[]).forEach((x) => {
109118
nodes.push(
110119
new JsonParseNode(x).getObjectValue(
111-
createUntypedNodeFromDiscriminatorValue,
112-
),
120+
createUntypedNodeFromDiscriminatorValue
121+
)
113122
);
114123
});
115124
value = createUntypedArray(nodes) as any as T;
116125
} else if (this._jsonNode && valueType === "object") {
117126
const properties: Record<string, UntypedNode> = {};
118127
Object.entries(this._jsonNode as any).forEach(([k, v]) => {
119128
properties[k] = new JsonParseNode(v).getObjectValue(
120-
createUntypedNodeFromDiscriminatorValue,
129+
createUntypedNodeFromDiscriminatorValue
121130
);
122131
});
123132
value = createUntypedObject(properties) as any as T;
@@ -126,8 +135,12 @@ export class JsonParseNode implements ParseNode {
126135
}
127136
return value;
128137
}
129-
const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp));
130-
const value: T = enableBackingStore ? new Proxy(temp, createBackedModelProxyHandler<T>()) : temp;
138+
const enableBackingStore = isBackingStoreEnabled(
139+
parsableFactory(this)(temp)
140+
);
141+
const value: T = enableBackingStore
142+
? new Proxy(temp, createBackedModelProxyHandler<T>())
143+
: temp;
131144
if (this.onBeforeAssignFieldValues) {
132145
this.onBeforeAssignFieldValues(value);
133146
}
@@ -140,7 +153,7 @@ export class JsonParseNode implements ParseNode {
140153

141154
private assignFieldValues = <T extends Parsable>(
142155
model: T,
143-
parsableFactory: ParsableFactory<T>,
156+
parsableFactory: ParsableFactory<T>
144157
): void => {
145158
const fields = parsableFactory(this)(model);
146159
if (!this._jsonNode) return;

0 commit comments

Comments
 (0)