Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Parse JSON body but whitelist properties in express or typescript

I have this endpoint in my express server:

app.post('/users', express.json(), async (request, response, next) => {
    try {
        console.log(`${request.method} ${request.url} was called.`);
        let user: User = request.body;
        let sessionId: string = request.query.sessionId as string;
        let captcha: string = request.query.captcha as string;
        let errors: string[] = await UserStore.CreateUser(elastic, smtp, user, sessionId, captcha);
        response.status(errors.length <= 0 ? 201 : 400).send(errors);
    }
    catch (error) {
        next(error);
    }
});

Additionally, this is my User class:

export class User {

    public Username: string;
    public Password: string;
    public Email: string;
    public EmailVerified: boolean;
    public Country: string;
    public State: string;
    public City: string;
    public Gender: string;
    public BirthdayTicks: number;

}

Is there a standard way in express or typescript to only parse and generate a whitelisted set of properties for a new User class from the JSON body, or do I have to new one up and set properties on it manually?

The problem right now is that request.body may have additional properties that don't exist in the model of a User or properties such as EmailVerified, which I don't want the user to be able to set themselves. If these go to the elastic server, they will be indexed.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!