Skip to content

Instantly share code, notes, and snippets.

@saman-ghm
Created January 30, 2020 20:56
Show Gist options
  • Save saman-ghm/fe8e27bb5d8dd130e9a350c375bfb7db to your computer and use it in GitHub Desktop.
Save saman-ghm/fe8e27bb5d8dd130e9a350c375bfb7db to your computer and use it in GitHub Desktop.
import { Router, Response, Request, NextFunction } from "express";
import express from "express";
import bodyParser from "body-parser";
import { validate, Matches, IsDefined } from "class-validator";
import { plainToClass, Expose } from "class-transformer";
class UserDto {
@IsDefined()
@Expose()
username: String;
@IsDefined()
@Expose()
@Matches(RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/))
password: String;
@IsDefined()
@Expose()
@Matches(RegExp(/^(([^<>()\[\]\\.,;:\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,}))$/))
email: String;
}
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const router = Router();
const validationMw = (dtoClass: any) => {
return function (req: Request, res: Response, next: NextFunction) {
const output: any = plainToClass(dtoClass, req.body);
validate(output, { skipMissingProperties: true }).then(errors => {
// errors is an array of validation errors
if (errors.length > 0) {
console.log(errors);
let errorTexts = Array();
for (const errorItem of errors) {
errorTexts = errorTexts.concat(errorItem.constraints);
}
res.status(400).send(errorTexts);
return;
} else {
res.locals.input = output;
next();
}
});
};
};
router.post("/user/submit", validationMw(UserDto), async (req: Request, res: Response) => {
res.json(res.locals.input);
});
app.use(router);
app.listen(3000, () => {
console.log('Server is running in http://localhost:3000');
});
@kathawala
Copy link

Loved your article where this snippet was used!

I have a different way of writing the part of the middleware where you collect any validation errors and send them to the user. If others want to try it out they can feel free. I swapped these lines:

                let errorTexts = Array();
                for (const errorItem of errors) {
                    errorTexts = errorTexts.concat(errorItem.constraints);
                }

for this one

                const errorTexts = errors.map(obj => Object.values(obj.constraints)).flat().join('. ');

This gives all the errors as sentences, one after another, and no brackets or other syntax, so the response is human readable.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment