@anyhowstep/ts-route-server
Version:
25 lines (23 loc) • 840 B
text/typescript
import {Schema, SchemaUtil} from "@anyhowstep/schema";
import * as express from "express";
export class SchemaMiddleware {
public static Create<T> (name : string, getMixed : (req : express.Request) => any, schema? : Schema<T>) {
if (schema == null) {
return (_req : express.Request, _res : express.Response, next : express.NextFunction) => {
next();
};
} else {
return (req : express.Request, res : express.Response, next : express.NextFunction) => {
SchemaUtil.Fill(
name,
getMixed(req),
schema
).then(() => {
next();
}).catch((err) => {
res.status(400).json(err);
});
};
}
}
}