/
collectionOf.ts
180 lines (152 loc) · 4.49 KB
/
collectionOf.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {Type} from "@tsed/core";
import {JsonEntityStore} from "../../domain/JsonEntityStore";
export interface ArrayOfChainedDecorators {
(...args: any): any;
/**
* 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.
* :::
*/
MinItems(minItems: number): this;
/**
* The value `maxItems` MUST be a non-negative integer.
*
* An array instance is valid against `maxItems` if its size is less than, or equal to, the value of this keyword.
*
* :: warning
* The value `maxItems` MUST be a non-negative integer.
* :::
*/
MaxItems(maxItems: number): this;
/**
* Set the type of the item collection. The possible value is String, Boolean, Number, Date, Object, Class, etc...
*
* The array instance will be valid against "contains" if at least one of its elements is valid against the given schema.
*/
Contains(): this;
/**
* If this keyword has boolean value false, the instance validates successfully. If it has boolean value true, the instance validates successfully if all of its elements are unique.
*/
UniqueItems(uniqueItems: boolean): this;
}
export interface MapOfChainedDecorators {
(...args: any): any;
/**
* An object instance is valid against `minProperties` if its number of properties is less than, or equal to, the value of this keyword.
*
* ::: warning
* The value of this keyword MUST be a non-negative integer.
* :::
*/
MinProperties(minProperties: number): this;
/**
* An object instance is valid against `maxProperties` if its number of properties is less than, or equal to, the value of this keyword.
*
* ::: warning
* The value of this keyword MUST be a non-negative integer.
* :::
*/
MaxProperties(maxProperties: number): this;
}
export interface CollectionOfChainedDecorators extends MapOfChainedDecorators, ArrayOfChainedDecorators {}
/**
* Set the type of the item collection. The possible value is String, Boolean, Number, Date, Object, Class, etc...
*
* ```typescript
* class Model {
* @CollectionOf(String).MinLength(0).MaxLength(0)
* property: string[];
* }
* ```
* ::: warning
* You mustn't use the `type Type = string | number` as parameters Type.
*
* This example doesn't work:
*
* ```typescript
* type Type = "string" | "number"
* class Model {
* @CollectionOf(Type)
* property: Type[];
* }
* ```
* :::
*
* @param {Type<any>} type
* @param collectionType
* @decorator
* @validation
* @swagger
* @schema
* @input
* @collections
*/
export function CollectionOf(type: any, collectionType?: any): CollectionOfChainedDecorators {
const schema: any = {};
let contains: boolean = false;
const decorator = (...args: any) => {
const store = JsonEntityStore.from(...args);
// const itemSchema = store.itemSchema.toJSON();
if (collectionType) {
store.collectionType = collectionType;
store.schema.type(collectionType);
}
store.type = type;
// console.log(type);
store.itemSchema.type(type);
// console.log(store.itemSchema.getComputedType(), schema);
// store.itemSchema.assign({...itemSchema, type});
store.schema.assign(schema);
if (store.isArray && contains) {
store.schema.set("contains", store.schema.get("items"));
store.schema.delete("items");
}
};
decorator.MinItems = (minItems: number) => {
schema.minItems = minItems;
return decorator;
};
decorator.MaxItems = (maxItems: number) => {
schema.maxItems = maxItems;
return decorator;
};
decorator.MinProperties = (minProperties: number) => {
schema.minProperties = minProperties;
return decorator;
};
decorator.MaxProperties = (maxProperties: number) => {
schema.maxProperties = maxProperties;
return decorator;
};
decorator.Contains = () => {
contains = true;
return decorator;
};
decorator.UniqueItems = (uniqueItems = true) => {
schema.uniqueItems = uniqueItems;
return decorator;
};
return decorator;
}
/**
* Alias of @@GenericOf@@ decorator.
* @param type
* @decorator
*/
export function ArrayOf(type: any): ArrayOfChainedDecorators {
return CollectionOf(type, Array);
}
/**
* Alias of @@GenericOf@@ decorator.
* @param type
* @decorator
*/
export function MapOf(type: any): MapOfChainedDecorators {
return CollectionOf(type, Map);
}