Javascript 如何确保用户向firebase callabe函数发送正确的数据?
我一直在努力确保用户向firebase HttpScalable云函数发送正确的数据。 以下是我到目前为止得到的信息:Javascript 如何确保用户向firebase callabe函数发送正确的数据?,javascript,typescript,firebase,google-cloud-functions,Javascript,Typescript,Firebase,Google Cloud Functions,我一直在努力确保用户向firebase HttpScalable云函数发送正确的数据。 以下是我到目前为止得到的信息: import * as functions from "firebase-functions"; class Person { name: string; } class ExpectingData { a: string; b: number; c: Person; d: Person[]; } export const exam
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.是否有一个assert函数可以检查客户端是否准确发送ExpectingData形状(或任何其他形状)数据?
2.是否可以在该函数中检查a是字符串的类型,c是Person的实例,而不仅仅是检查键(如示例中的“assertKey”)
typeof foo==“string”
来检查JavaScript字符串
typeof foo==“string”
来检查JavaScript字符串
我是否可以递归地映射对象属性来检查数据,比如(dataFromClient:any,expectingDataClass:function)=>checkResult。我试过了,这很令人沮丧。我需要学习一些基本的东西。我可以递归地映射对象属性来检查数据吗,比如(dataFromClient:any,expectingDataClass:function)=>checkResult。我试过了,这很令人沮丧。我需要学习一些基本的东西。