3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 00:05:01 +00:00

fix: missing data in template safe objects

This commit is contained in:
Dragory 2023-04-01 21:33:40 +03:00
parent d4292205b9
commit ab54dc215f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 55 additions and 30 deletions

View file

@ -59,29 +59,36 @@ function isTemplateSafeValue(value: unknown): value is TemplateSafeValue {
export class TemplateSafeValueContainer {
// Fake property used for stricter type checks since TypeScript uses structural typing
_isTemplateSafeValueContainer: true;
[key: string]: TemplateSafeValue;
constructor(data: Record<string, TemplateSafeValue> = {}) {
for (const [key, value] of Object.entries(data)) {
if (!isTemplateSafeValue(value)) {
// tslint:disable:no-console
console.error("=== CONTEXT FOR UNSAFE VALUE ===");
console.error("stringified:", JSON.stringify(value));
console.error("typeof:", typeof value);
console.error("constructor name:", (value as any)?.constructor?.name);
console.error("=== /CONTEXT FOR UNSAFE VALUE ===");
// tslint:enable:no-console
throw new Error(`Unsafe value for key "${key}" in SafeTemplateValueContainer`);
}
this[key] = value;
constructor(data?: Record<string, TemplateSafeValue>) {
if (data) {
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
}
export type TypedTemplateSafeValueContainer<T> = TemplateSafeValueContainer & T;
export function ingestDataIntoTemplateSafeValueContainer(
target: TemplateSafeValueContainer,
data: Record<string, TemplateSafeValue> = {},
) {
for (const [key, value] of Object.entries(data)) {
if (!isTemplateSafeValue(value)) {
// tslint:disable:no-console
console.error("=== CONTEXT FOR UNSAFE VALUE ===");
console.error("stringified:", JSON.stringify(value));
console.error("typeof:", typeof value);
console.error("constructor name:", (value as any)?.constructor?.name);
console.error("=== /CONTEXT FOR UNSAFE VALUE ===");
// tslint:enable:no-console
throw new Error(`Unsafe value for key "${key}" in SafeTemplateValueContainer`);
}
target[key] = value;
}
}
export function createTypedTemplateSafeValueContainer<T extends Record<string, TemplateSafeValue>>(
data: T,
): TypedTemplateSafeValueContainer<T> {