SugoiJS provides easy, one line solution for validating the request, this by using the @RequestSchemaPolicy
decorator.
The RequestSchemaPolicy
is similar to socket module SocketSchemaPolicy
method.
SugoiJS server module uses @sugoi/core policies ability for supplying pre-defined policies
SugoiJS re-export SchemaTypes, TPolicy, TComparableSchema, Policy, UsePolicy, ComparableSchema from "@sugoi/core";
Further information can be found on the @sugoi/core package documentation​
RequestSchemaPolicy - decleration/*** paramSchema - req.params* queryParamSchema - req.query* bodySchema - req.body* headersSchema - req.headers**/export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi);export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema);export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema);export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema, headersSchema: TComparableSchema);
The RequestSchemaPolicy
decorator use for validating the request, this while using a valid schema for Request params, Query params, body, headers.
In case null will pass the value won't be check.
@Controller('/dashboard')export class DashboardController {constructor() {}​@HttpPost("/:id")@RequestSchemaPolicy({"id": ComparableSchema.ofType(SchemaTypes.NUMBER)},// Path parameters schemanull,// Query parameters schema{"role": ComparableSchema.ofType({text: ComparableSchema.ofType(SchemaTypes.STRING).setRegex("([A-Z])+","i")})},// body schema is {role:{text:string//with regex - /([A-Z])+/i}}new Ajv().compile(ajvHeaderSchema) // header schema)getUser(@RequestParam("id") id:number, @RequestBody() body:{role:{text:string}}) {return User.findOne({id,role:body.role.text})}​}
For validating only the request parameters you can use RequestParamsSchemaPolicy
function RequestParamsSchemaPolicy(schema: TComparableSchema)
For validating only the request query parameters you can useRequestQueryParamsSchemaPolicy
function RequestQueryParamsSchemaPolicy(schema: TComparableSchema)
For validating only the request body you can useRequestBodySchemaPolicy
function RequestBodySchemaPolicy(schema: TComparableSchema)
For validating only the request headers you can use RequestHeadersSchemaPolicy
function RequestHeadersSchemaPolicy(schema: TComparableSchema)
@HttpPost("/:id")@RequestParamsSchemaPolicy(RequestWithIDSchema)@RequestQueryParamsSchemaPolicy({check: ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(false)})@RequestBodySchemaPolicy({data: ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(true)})@RequestHeadersSchemaPolicy({"x-user-language":ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(true)})public setData(@RequestParam("id") id: string) {// finally the logic}