Parameters validation

Last updated last month

With SugoiJS request parameters validation become easier

Overview

SugoiJS provides easy, one line solution for validating the request, this by using the @RequestSchemaPolicy decorator.

The RequestSchemaPolicy is similar to server module socketSocketSchemaPolicy 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

RequestSchemaPolicy - decleration
/**
* paramSchema - req.params
* queryParamSchema - req.query
* bodySchema - req.body
* headersSchema - req.headers
**/
export function RequestSchemaPolicy(paramSchema: TComparableSchema);
export function RequestSchemaPolicy(paramSchema: TComparableSchema, queryParamSchema: TComparableSchema);
export function RequestSchemaPolicy(paramSchema: TComparableSchema, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema);
export function RequestSchemaPolicy(paramSchema: TComparableSchema, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema, headersSchema: TComparableSchema);

The RequestSchemaPolicy decorator use for validating the request, this whild using a valid schema for Request params, Query params, body, headers.

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

Example:

@Controller('/dashboard')
export class DashboardController {
constructor() {
}
@HttpPost("/:id")
@RequestSchemaPolicy(
{"id": ComparableSchema.ofType(SchemaTypes.NUMBER)},
null,
{
"role": ComparableSchema.ofType(
{
text: ComparableSchema
.ofType(SchemaTypes.STRING)
.setRegex("([A-Z])+","i")
}
)
}
)//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})
}
}

Validation per parameter type

RequestParamsSchemaPolicy

For validating only the request parameters you can use RequestParamsSchemaPolicy

function RequestParamsSchemaPolicy(schema: TComparableSchema)

RequestQueryParamsSchemaPolicy

For validating only the request query parameters you can useRequestQueryParamsSchemaPolicy

function RequestQueryParamsSchemaPolicy(schema: TComparableSchema)

RequestBodySchemaPolicy

For validating only the request body you can useRequestBodySchemaPolicy

function RequestBodySchemaPolicy(schema: TComparableSchema)

RequestHeadersSchemaPolicy

For validating only the request headers you can use RequestHeadersSchemaPolicy

function RequestHeadersSchemaPolicy(schema: TComparableSchema)

Full example

@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
}