/
required.ts
49 lines (48 loc) · 1.11 KB
/
required.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {Allow} from "./allow";
import {Optional} from "./optional";
/**
* Add required annotation on Property or Parameter.
*
* The @@Required@@ decorator can be used on two cases.
*
* To decorate a parameters:
*
* ```typescript
* @Post("/")
* async method(@Required() @BodyParams("field") field: string) {}
* ```
*
* To decorate a model:
*
* ```typescript
* class Model {
* @Required()
* field: string;
* }
* ```
*
* ::: tip
* Required will throw a BadRequest when the given value is `null`, an empty string or `undefined`.
* :::
*
* ### Allow values
*
* In some case, you didn't want trigger a BadRequest when the value is an empty string for example.
* The decorator `@Allow()`, allow you to configure a value list for which there will be no exception.
*
* ```typescript
* class Model {
* @Allow("") // add automatically required flag
* field: string;
* }
* ```
*
* @decorator
* @validation
* @swagger
* @schema
* @input
*/
export function Required(required: boolean = true, ...allowedRequiredValues: any[]) {
return required ? Allow(...allowedRequiredValues) : Optional();
}