Parameters validation
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 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

RequestSchemaPolicy - decleration
1
/**
2
* paramSchema - req.params
3
* queryParamSchema - req.query
4
* bodySchema - req.body
5
* headersSchema - req.headers
6
**/
7
export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi);
8
export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema);
9
export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema);
10
export function RequestSchemaPolicy(paramSchema: TComparableSchema | AJV | Joi, queryParamSchema: TComparableSchema, bodySchema: TComparableSchema, headersSchema: TComparableSchema);
Copied!
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.

Example:

1
@Controller('/dashboard')
2
export class DashboardController {
3
constructor() {
4
}
5
6
@HttpPost("/:id")
7
@RequestSchemaPolicy(
8
{"id": ComparableSchema.ofType(SchemaTypes.NUMBER)},// Path parameters schema
9
null,// Query parameters schema
10
{
11
"role": ComparableSchema.ofType(
12
{
13
text: ComparableSchema
14
.ofType(SchemaTypes.STRING)
15
.setRegex("([A-Z])+","i")
16
}
17
)
18
},// body schema is {role:{text:string//with regex - /([A-Z])+/i}}
19
new Ajv().compile(ajvHeaderSchema) // header schema
20
)
21
getUser(@RequestParam("id") id:number, @RequestBody() body:{role:{text:string}}) {
22
return User.findOne({id,role:body.role.text})
23
}
24
25
}
Copied!

Validation per parameter type

RequestParamsSchemaPolicy

For validating only the request parameters you can use RequestParamsSchemaPolicy
1
function RequestParamsSchemaPolicy(schema: TComparableSchema)
Copied!

RequestQueryParamsSchemaPolicy

For validating only the request query parameters you can useRequestQueryParamsSchemaPolicy
1
function RequestQueryParamsSchemaPolicy(schema: TComparableSchema)
Copied!

RequestBodySchemaPolicy

For validating only the request body you can useRequestBodySchemaPolicy
1
function RequestBodySchemaPolicy(schema: TComparableSchema)
Copied!

RequestHeadersSchemaPolicy

For validating only the request headers you can use RequestHeadersSchemaPolicy
1
function RequestHeadersSchemaPolicy(schema: TComparableSchema)
Copied!

Full example

1
@HttpPost("/:id")
2
@RequestParamsSchemaPolicy(RequestWithIDSchema)
3
@RequestQueryParamsSchemaPolicy({
4
check: ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(false)
5
})
6
@RequestBodySchemaPolicy({
7
data: ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(true)
8
})
9
@RequestHeadersSchemaPolicy({
10
"x-user-language":ComparableSchema.ofType(SchemaTypes.STRING).setMandatory(true)
11
})
12
public setData(@RequestParam("id") id: string) {
13
// finally the logic
14
}
Copied!
Last modified 2yr ago