I am using firebase cloud functions and I want to validate my api request call on postman. On wrong input it must send a error response back to my postman.
api call
const validator = require('./libs/validators');
export const createUser = functions.https.onRequest((req, res) => {
// @ts-ignore
// ts-lint:disable-next-line
corsHandler(req, res, async () => {
const validData = validateData(req.body);
res.send(validData);
try {
const user = await admin.auth().createUser({ email: validData.email, password: validData.password });
await admin.auth().setCustomUserClaims(user.uid, { isAdmin: false, isSubscribed: false });
await admin.firestore().collection('users').doc(user.uid).set(
{ validData }, { merge: true }
);
res.send(user);
} catch (e: any) {
throw (res.status(400).send(e.message));
}
});
});
function validateData(body: any) {
if (body === undefined) {
throw Error("Invalid data");
}
return validator.newStudentValidator(body);
}
validators.ts
export const newStudentValidator = function (body: any) {
try {
if (body.name === null) {
throw new Error('name is required');
}
if (!body.email.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) {
throw new Error("Invalid email");
}
if (body.password === undefined) {
throw new Error("Password is undefined");
}
if (body.university === undefined) {
throw new Error("University is undefined");
}
if (body.sem === undefined) {
throw new Error("Semister is undefined");
}
}
catch (e: any) {
throw new Error(e.message);
}
const allowed = ['name', 'email', 'password', 'university', 'sem'];
const filtered = Object.keys(body).filter(key => allowed.includes(key)).reduce((obj: any, key) => {
obj[key] = body[key];
return obj;
}, {});
return filtered;
}
i get this error everytime enter image description here