@atomiqlabs/lp-lib 17.6.0 → 17.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,11 @@
1
- import {FieldTypeEnum, parseBigInt, RequestSchema, RequestSchemaResult, verifySchema} from "./SchemaVerifier";
1
+ import {
2
+ FieldTypeEnum,
3
+ parseBigInt,
4
+ RequestSchema,
5
+ RequestSchemaResult,
6
+ verifySchema,
7
+ verifySchemaField
8
+ } from "./SchemaVerifier";
2
9
  import {IParamReader} from "./IParamReader";
3
10
 
4
11
 
@@ -133,41 +140,7 @@ export class ParamDecoder implements IParamReader {
133
140
  const resultSchema: any = {};
134
141
  for(let fieldName in schema) {
135
142
  const val: any = await this.getParam(fieldName);
136
- const type: FieldTypeEnum | RequestSchema | ((val: any) => boolean) = schema[fieldName];
137
- if(typeof(type)==="function") {
138
- const result = type(val);
139
- if(result==null) return null;
140
- resultSchema[fieldName] = result;
141
- continue;
142
- }
143
-
144
- if(val==null && (type as number)>=100) {
145
- resultSchema[fieldName] = null;
146
- continue;
147
- }
148
-
149
- if(type===FieldTypeEnum.Any || type===FieldTypeEnum.AnyOptional) {
150
- resultSchema[fieldName] = val;
151
- } else if(type===FieldTypeEnum.Boolean || type===FieldTypeEnum.BooleanOptional) {
152
- if(typeof(val)!=="boolean") return null;
153
- resultSchema[fieldName] = val;
154
- } else if(type===FieldTypeEnum.Number || type===FieldTypeEnum.NumberOptional) {
155
- if(typeof(val)!=="number") return null;
156
- if(isNaN(val as number)) return null;
157
- resultSchema[fieldName] = val;
158
- } else if(type===FieldTypeEnum.BigInt || type===FieldTypeEnum.BigIntOptional) {
159
- const result = parseBigInt(val);
160
- if(result==null) return null;
161
- resultSchema[fieldName] = result;
162
- } else if(type===FieldTypeEnum.String || type===FieldTypeEnum.StringOptional) {
163
- if(typeof(val)!=="string") return null;
164
- resultSchema[fieldName] = val;
165
- } else {
166
- //Probably another request schema
167
- const result = verifySchema(val, type as RequestSchema);
168
- if(result==null) return null;
169
- resultSchema[fieldName] = result;
170
- }
143
+ if(!verifySchemaField(val, schema[fieldName], fieldName, resultSchema)) return null;
171
144
  }
172
145
  return resultSchema;
173
146
  }
@@ -182,36 +155,7 @@ export class ParamDecoder implements IParamReader {
182
155
  continue;
183
156
  }
184
157
 
185
- const type: FieldTypeEnum | RequestSchema | ((val: any) => boolean) = schema[fieldName];
186
- if(typeof(type)==="function") {
187
- const result = type(val);
188
- if(result==null) return null;
189
- resultSchema[fieldName] = result;
190
- continue;
191
- }
192
-
193
- if(type===FieldTypeEnum.Any || type===FieldTypeEnum.AnyOptional) {
194
- resultSchema[fieldName] = val;
195
- } else if(type===FieldTypeEnum.Boolean || type===FieldTypeEnum.BooleanOptional) {
196
- if(typeof(val)!=="boolean") return null;
197
- resultSchema[fieldName] = val;
198
- } else if(type===FieldTypeEnum.Number || type===FieldTypeEnum.NumberOptional) {
199
- if(typeof(val)!=="number") return null;
200
- if(isNaN(val as number)) return null;
201
- resultSchema[fieldName] = val;
202
- } else if(type===FieldTypeEnum.BigInt || type===FieldTypeEnum.BigIntOptional) {
203
- const result = parseBigInt(val);
204
- if(result==null) return null;
205
- resultSchema[fieldName] = result;
206
- } else if(type===FieldTypeEnum.String || type===FieldTypeEnum.StringOptional) {
207
- if(typeof(val)!=="string") return null;
208
- resultSchema[fieldName] = val;
209
- } else {
210
- //Probably another request schema
211
- const result = verifySchema(val, type as RequestSchema);
212
- if(result==null) return null;
213
- resultSchema[fieldName] = result;
214
- }
158
+ if(!verifySchemaField(val, schema[fieldName], fieldName, resultSchema)) return null;
215
159
  }
216
160
  return resultSchema;
217
161
  }
@@ -15,25 +15,41 @@ export enum FieldTypeEnum {
15
15
  Number=2,
16
16
  BigInt=3,
17
17
  Any=4,
18
+ BigIntPositive=5,
19
+ BigIntNotNegative=6,
20
+ NumberPositive=7,
21
+ NumberNotNegative=8,
18
22
 
19
23
  StringOptional=100,
20
24
  BooleanOptional=101,
21
25
  NumberOptional=102,
22
26
  BigIntOptional=103,
23
27
  AnyOptional=104,
28
+ BigIntPositiveOptional=105,
29
+ BigIntNotNegativeOptional=106,
30
+ NumberPositiveOptional=107,
31
+ NumberNotNegativeOptional=108,
24
32
  }
25
33
 
26
34
  export type FieldType<T extends FieldTypeEnum | RequestSchema | ((val: any) => (string | boolean | number | bigint | any))> =
27
35
  T extends FieldTypeEnum.String ? string :
28
36
  T extends FieldTypeEnum.Boolean ? boolean :
29
37
  T extends FieldTypeEnum.Number ? number :
38
+ T extends FieldTypeEnum.NumberPositive ? number :
39
+ T extends FieldTypeEnum.NumberNotNegative ? number :
30
40
  T extends FieldTypeEnum.BigInt ? bigint :
31
41
  T extends FieldTypeEnum.Any ? any :
42
+ T extends FieldTypeEnum.BigIntPositive ? bigint :
43
+ T extends FieldTypeEnum.BigIntNotNegative ? bigint :
32
44
  T extends FieldTypeEnum.StringOptional ? string :
33
45
  T extends FieldTypeEnum.BooleanOptional ? boolean :
34
46
  T extends FieldTypeEnum.NumberOptional ? number :
47
+ T extends FieldTypeEnum.NumberPositiveOptional ? number :
48
+ T extends FieldTypeEnum.NumberNotNegativeOptional ? number :
35
49
  T extends FieldTypeEnum.BigIntOptional ? bigint :
36
50
  T extends FieldTypeEnum.AnyOptional ? any :
51
+ T extends FieldTypeEnum.BigIntPositiveOptional ? bigint :
52
+ T extends FieldTypeEnum.BigIntNotNegativeOptional ? bigint :
37
53
  T extends RequestSchema ? RequestSchemaResult<T> :
38
54
  T extends ((val: any) => string) ? string :
39
55
  T extends ((val: any) => boolean) ? boolean :
@@ -50,47 +66,74 @@ export type RequestSchema = {
50
66
  [fieldName: string]: FieldTypeEnum | RequestSchema | ((val: any) => any)
51
67
  }
52
68
 
69
+ export function verifySchemaField(
70
+ val: any,
71
+ type: FieldTypeEnum | RequestSchema | ((val: any) => any),
72
+ fieldName: string,
73
+ resultSchema: any
74
+ ): boolean {
75
+ if(typeof(type)==="function") {
76
+ const result = type(val);
77
+ if(result==null) return false;
78
+ resultSchema[fieldName] = result;
79
+ return true;
80
+ }
81
+
82
+ if(val==null && (type as number)>=100) {
83
+ resultSchema[fieldName] = null;
84
+ return true;
85
+ }
86
+
87
+ if(type===FieldTypeEnum.Any || type===FieldTypeEnum.AnyOptional) {
88
+ resultSchema[fieldName] = val;
89
+ } else if(type===FieldTypeEnum.Boolean || type===FieldTypeEnum.BooleanOptional) {
90
+ if(typeof(val)!=="boolean") return false;
91
+ resultSchema[fieldName] = val;
92
+ } else if(type===FieldTypeEnum.Number || type===FieldTypeEnum.NumberOptional) {
93
+ if(typeof(val)!=="number") return false;
94
+ if(isNaN(val as number)) return false;
95
+ resultSchema[fieldName] = val;
96
+ } else if(type===FieldTypeEnum.NumberPositive || type===FieldTypeEnum.NumberPositiveOptional) {
97
+ if(typeof(val)!=="number") return false;
98
+ if(isNaN(val as number)) return false;
99
+ if(val<=0) return false;
100
+ resultSchema[fieldName] = val;
101
+ } else if(type===FieldTypeEnum.NumberNotNegative || type===FieldTypeEnum.NumberNotNegativeOptional) {
102
+ if(typeof(val)!=="number") return false;
103
+ if(isNaN(val as number)) return false;
104
+ if(val<0) return false;
105
+ resultSchema[fieldName] = val;
106
+ } else if(type===FieldTypeEnum.BigInt || type===FieldTypeEnum.BigIntOptional) {
107
+ const result = parseBigInt(val);
108
+ if(result==null) return false;
109
+ resultSchema[fieldName] = result;
110
+ } else if(type===FieldTypeEnum.BigIntPositive || type===FieldTypeEnum.BigIntPositiveOptional) {
111
+ const result = parseBigInt(val);
112
+ if(result==null) return false;
113
+ if(result<=0n) return false;
114
+ resultSchema[fieldName] = result;
115
+ } else if(type===FieldTypeEnum.BigIntNotNegative || type===FieldTypeEnum.BigIntNotNegativeOptional) {
116
+ const result = parseBigInt(val);
117
+ if(result==null) return false;
118
+ if(result<0n) return false;
119
+ resultSchema[fieldName] = result;
120
+ } else if(type===FieldTypeEnum.String || type===FieldTypeEnum.StringOptional) {
121
+ if(typeof(val)!=="string") return false;
122
+ resultSchema[fieldName] = val;
123
+ } else {
124
+ //Probably another request schema
125
+ const result = verifySchema(val, type as RequestSchema);
126
+ if(result==null) return false;
127
+ resultSchema[fieldName] = result;
128
+ }
129
+ return true;
130
+ }
131
+
53
132
  export function verifySchema<T extends RequestSchema>(req: any, schema: T): RequestSchemaResult<T> {
54
133
  if(req==null) return null;
55
134
  const resultSchema: any = {};
56
135
  for(let fieldName in schema) {
57
- const val: any = req[fieldName];
58
-
59
- const type: FieldTypeEnum | RequestSchema | ((val: any) => boolean) = schema[fieldName];
60
- if(typeof(type)==="function") {
61
- const result = type(val);
62
- if(result==null) return null;
63
- resultSchema[fieldName] = result;
64
- continue;
65
- }
66
-
67
- if(val==null && (type as number)>=100) {
68
- resultSchema[fieldName] = null;
69
- continue;
70
- }
71
-
72
- if(type===FieldTypeEnum.Any || type===FieldTypeEnum.AnyOptional) {
73
- resultSchema[fieldName] = val;
74
- } else if(type===FieldTypeEnum.Boolean || type===FieldTypeEnum.BooleanOptional) {
75
- if(typeof(val)!=="boolean") return null;
76
- resultSchema[fieldName] = val;
77
- } else if(type===FieldTypeEnum.Number || type===FieldTypeEnum.NumberOptional) {
78
- if(typeof(val)!=="number") return null;
79
- if(isNaN(val as number)) return null;
80
- resultSchema[fieldName] = val;
81
- } else if(type===FieldTypeEnum.BigInt || type===FieldTypeEnum.BigIntOptional) {
82
- const result = parseBigInt(val);
83
- if(result==null) return null;
84
- resultSchema[fieldName] = result;
85
- } else if(type===FieldTypeEnum.String || type===FieldTypeEnum.StringOptional) {
86
- if(typeof(val)!=="string") return null;
87
- resultSchema[fieldName] = val;
88
- } else {
89
- //Probably another request schema
90
- const result = verifySchema(val, type as RequestSchema);
91
- if(result==null) return null;
92
- resultSchema[fieldName] = result;
93
- }
136
+ if(!verifySchemaField(req[fieldName], schema[fieldName], fieldName, resultSchema)) return null;
94
137
  }
95
138
  return resultSchema;
96
139
  }