-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
maxLength.ts
86 lines (84 loc) · 1.73 KB
/
maxLength.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {decoratorSchemaFactory} from "../utils/decoratorSchemaFactory";
/**
* A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
*
* The length of a string instance is defined as the number of its characters as defined by [RFC 7159](http://json-schema.org/latest/json-schema-validation.html#RFC7159).
*
* ::: warning
* The value of maxLength MUST be a non-negative integer.
* :::
*
* ::: tip
* Omitting this keyword has the same behavior as a value of 0.
* :::
*
* ::: warning
* This decorator will be removed in v7.
* For v6 user, use @@MaxLength@@ from @tsed/schema instead of @tsed/common.
* :::
*
* ## Example
* ### With primitive type
*
* ```typescript
* class Model {
* @MaxLength(10)
* property: number;
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "string",
* "maxLength": 10
* }
* }
* }
* ```
*
* ### With array type
*
* ```typescript
* class Model {
* @MaxLength(10)
* @CollectionOf(String)
* property: string[];
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "array",
* "items": {
* "type": "string",
* "maxLength": 10
* }
* }
* }
* }
* ```
*
* @param {number} maxLength The maximum length allowed
* @decorator
* @validation
* @swagger
* @schema
*/
export function MaxLength(maxLength: number) {
if (maxLength < 0) {
throw new Error("The value of maxLength MUST be a non-negative integer.");
}
return decoratorSchemaFactory((schema) => {
schema.mapper.maxLength = maxLength;
});
}