【发布时间】:2020-11-12 03:57:19
【问题描述】:
我一直在努力确保用户将正确的数据发送到 firebase HTTPSCallable 云函数。 这是我到目前为止得到的:
import * as functions from "firebase-functions";
class Person {
name: string;
}
class ExpectingData {
a: string;
b: number;
c: Person;
d: Person[];
}
export const example = functions.https.onCall((data, context) => {
const uid = assertUID(context);
// with "assertUID", I can make sure user is authenticated.
const a = assertKey(data, "a");
// With "assertKey", I can roughly make sure user data got the right property in the first nested layer.
const b = assertKey(data, "b");
const c = assertKey(data, "c");
const d = assertKey(data, "d");
});
export const assertUID = (context: any) => {
if (!context.auth) {
throw new functions.https.HttpsError(
"permission-denied",
"function called without context.auth"
);
} else {
return context.auth.uid as string;
}
};
export const assertKey = (data: any, key: string) => {
// data[key] should not be undefined or null;
const value = data[key];
if (typeof value === "undefined") {
throw new functions.https.HttpsError(
"invalid-argument",
`${key} is miisng`
);
}
return value;
};
这是我的问题: 1. 是否有一个断言函数可以检查客户端是否准确发送了 ExpectingData 形状(或任何其他形状)的数据? 2. 是否可以在此函数中检查 typeof a 是字符串,c 是 Person 的实例,而不仅仅是检查键(如示例中的“assertKey”)?
【问题讨论】:
标签: javascript typescript firebase google-cloud-functions