Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确保用户向firebase callabe函数发送正确的数据?_Javascript_Typescript_Firebase_Google Cloud Functions - Fatal编程技术网

Javascript 如何确保用户向firebase callabe函数发送正确的数据?

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

我一直在努力确保用户向firebase HttpScalable云函数发送正确的数据。 以下是我到目前为止得到的信息:

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”)

  • 不,您必须手动检查属性和类型,以确保您拥有所需的内容。归根结底,TypeScript必须编译成JavaScript,这在运行时不会给您任何保证,除非您检查它们

  • 任何普通JavaScript对象(包括交付给可调用对象的对象)都不会有可以在TypeScript中检查类型安全性的构造函数类型。如果您想知道每个对象是否包含您所需的所有内容,您必须深入探究每个对象及其属性。您当然可以通过使用
    typeof foo==“string”
    来检查JavaScript字符串

  • 不,您必须手动检查属性和类型,以确保您拥有所需的内容。归根结底,TypeScript必须编译成JavaScript,这在运行时不会给您任何保证,除非您检查它们

  • 任何普通JavaScript对象(包括交付给可调用对象的对象)都不会有可以在TypeScript中检查类型安全性的构造函数类型。如果您想知道每个对象是否包含您所需的所有内容,您必须深入探究每个对象及其属性。您当然可以通过使用
    typeof foo==“string”
    来检查JavaScript字符串


  • 我是否可以递归地映射对象属性来检查数据,比如(dataFromClient:any,expectingDataClass:function)=>checkResult。我试过了,这很令人沮丧。我需要学习一些基本的东西。我可以递归地映射对象属性来检查数据吗,比如(dataFromClient:any,expectingDataClass:function)=>checkResult。我试过了,这很令人沮丧。我需要学习一些基本的东西。