I have a specific requirement where I need to validate the structure of a request body to ensure it conforms to a predefined type. Is there a way or a package that can help achieve this validation?
type SampleRequestBody = {
id: string;
name: string;
age: number;
address: {
city: string;
street_num: number;
street_name: string;
};
balance?: number;
}
app.post("/", (req, res) => {
const isCorrectType: boolean = someMethod(req.body, SampleRequestBody);
if (isCorrectType) {
res.status(200).send("Correct Type!");
} else {
res.status(400).send("Incorrect Type!");
}
Essentially, the goal is to ensure that all mandatory fields are correctly typed according to the predefined structure!
Note: I understand that types cannot be directly passed to a function, I am simply illustrating what I intend to accomplish!