GithubHelp home page GithubHelp logo

Comments (9)

magiskboy avatar magiskboy commented on June 10, 2024 1

sorry, I setup the project incorrectly, my problem was solved

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 10, 2024

I cannot reproduce this with your example. Here is the snippet I'm running:

import 'reflect-metadata';
import {Expose, plainToClass} from "class-transformer";
import {IsEmail} from "class-validator";
import {Body, createExpressServer, JsonController, Post, Res} from "routing-controllers";
import {Response} from 'express';

export class GenerateIdentificationByEmailCommand {
    @Expose()
    @IsEmail()
    email!: string;

    static setProperties(cmd: GenerateIdentificationByEmailCommand): GenerateIdentificationByEmailCommand {
        return plainToClass(GenerateIdentificationByEmailCommand, cmd, {excludeExtraneousValues: true});
    }
}

@JsonController('')
export class TestController {
    @Post('/email')
    async generateIdentificationByEmail(
        @Res() res: Response,
        @Body({validate: true}) cmd: GenerateIdentificationByEmailCommand
    ) {
        const body = GenerateIdentificationByEmailCommand.setProperties(cmd);
        return res.status(201).send('test');
    }
}

const server = createExpressServer({
    defaultErrorHandler: true,
    defaults: {
        nullResultCode: 400,
        undefinedResultCode: 204,
        paramOptions: {
            required: true,
        },
    },
    classTransformer: true,
    validation: true,
    routePrefix: "/oks",
    controllers: [
        TestController
    ],
});

server.listen(8080, () => console.log('started'));

Running POST request to /oks/email with body

{
    "email2": "[email protected]"
}

Getting back an error:

{
  "errors": [
        {
            "target": {
                "email2": "[email protected]"
            },
            "property": "email",
            "children": [],
            "constraints": {
                "isEmail": "email must be an email"
            }
        }
    ]
}

Dependencies installed:

"dependencies": {
    "@types/express": "^4.17.17",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "routing-controllers": "^0.10.4",
    "ts-node": "^10.9.1",
    "typedi": "^0.10.0",
    "typescript": "^4.8.4"
  }

Could you please include more details?

from routing-controllers.

magiskboy avatar magiskboy commented on June 10, 2024

I cannot get errors when passing an invalid request body. It seems like the validator not working

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 10, 2024

@magiskboy could you include an example that is not working?

from routing-controllers.

magiskboy avatar magiskboy commented on June 10, 2024

Yep
This is to create application and express
image

and then I setup a controller called User with a route /login
image

I use httpie to call to localhost:3000/api/users/login
image

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 10, 2024

@magiskboy Do you have reflect-metadata imported?

from routing-controllers.

magiskboy avatar magiskboy commented on June 10, 2024

yep, I imported reflect-metadata on top

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 10, 2024

@magiskboy how do you transpile ts? Using tsc or something else?

from routing-controllers.

joamart1nez avatar joamart1nez commented on June 10, 2024

I have the same problem, could you tell me where you configured the project wrong?

from routing-controllers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.