/
minItems.ts
60 lines (58 loc) · 1.22 KB
/
minItems.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
import {JsonSchema} from "../class/JsonSchema";
import {decoratorSchemaFactory} from "../utils/decoratorSchemaFactory";
/**
* An array instance is valid against `minItems` if its size is greater than, or equal to, the value of this keyword.
*
* ::: warning
* The value `minItems` 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 @@MinItems@@ from @tsed/schema instead of @tsed/common.
* :::
*
* ## Example
*
* ```typescript
* class Model {
* @CollectionOf(String)
* @MinItems(10)
* property: string[];
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "string",
* "minItems": 10
* }
* }
* }
* ```
*
* @param {number} minItems
* @returns {Function}
* @decorator
* @validation
* @swagger
* @schema
* @collections
*/
export function MinItems(minItems: number) {
if (minItems < 0) {
throw new Error("The value of minItems MUST be a non-negative integer.");
}
return decoratorSchemaFactory((schema: JsonSchema) => {
schema.minItems = minItems;
});
}