Parameters validation

With SugoiJS request parameters validation become easier

SugoiJS server module uses @sugoi/core policies and supply predefined policies, and re-export SchemaTypes, TPolicy, TComparableSchema, Policy, UsePolicy, ComparableSchema from "@sugoi/core";

Further information on the @sugoi/core package documentation


 *  paramSchema         - req.params
 *  queryParamSchema    - req.query
 *  bodySchema          - req.body
 *  headersSchema       - req.headers
RequestSchemaPolicy(paramSchema?: TComparableSchema,queryParamSchema?: TComparableSchema,bodySchema?: TComparableSchema,headersSchema?: TComparableSchema)

The RequestSchemaPolicy decorator use for validate the request is using a valid schema for params\queryParams\body\headers.

In case null will pass the value won't check.


export class DashboardController {
    constructor() {

    {"id": ComparableSchema.ofType(SchemaTypes.NUMBER)},
     "role": ComparableSchema.ofType(
                                         text: ComparableSchema
     )//body schema is {role:{text:string//with regex - /([A-Z])+/i}}
    getUser(@RequestParam("id") id:number, @RequestBody() body:{role:{text:string}}) {
        return User.findOne({id,role:body.role.text})


Last updated