Request Validation
The @Validation
decorator can be used to validate the request body or query parameters:
src/controllers/user.controller.ts
import {
Application,
ControllerBase,
KultController,
Post,
Validate,
} from '@kult/core';
@KultController('/users')
class UserController extends ControllerBase {
constructor(app: Application) {
super(app);
}
@Post('/create')
@Validate({
name: 'required|type:string|max:10',
age: 'required|type:number',
profile: {
avatar: 'type:string',
},
})
create() {
return 'User created.';
}
}
export default UserController;
Validation Options
Required
Ensures a propery exists
required
Type
Ensures a propery is of specified type
type:string
Max
Ensures a propery length does not exceed the specified length
max:5
Min
Ensures a propery is the minimun length of the specified length
min:2
Multiple options
Multiple validation options can be chained together just seperated all options with |
:
required|type:string|max:10