Skip to content

Commit 7049188

Browse files
committed
Stricter comparison checks
1 parent 0f3b785 commit 7049188

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/jinja/src/runtime.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,19 @@ export class ObjectValue extends RuntimeValue<Map<string, AnyRuntimeValue>> {
361361
bValue = bValue.toLowerCase();
362362
}
363363

364-
// Compare values
364+
// Ensure comparable types:
365+
// This is only an potential issue when `by='value'` and the dictionary has mixed value types
366+
const isPrimitive = (val: unknown) =>
367+
typeof val === "string" || typeof val === "number" || typeof val === "boolean";
368+
let firstNonPrimitive = isPrimitive(aValue) ? (isPrimitive(bValue) ? null : bValue) : aValue;
369+
if (firstNonPrimitive !== null) {
370+
throw new Error(
371+
`Cannot sort dictionary with non-primitive value types (found ${typeof firstNonPrimitive})`
372+
);
373+
} else if (typeof aValue !== typeof bValue) {
374+
throw new Error("Cannot sort dictionary with mixed value types");
375+
}
376+
365377
const a1 = aValue as string | number | boolean;
366378
const b1 = bValue as string | number | boolean;
367379

0 commit comments

Comments
 (0)