@tremho/mist-lift 2.2.9 → 2.5.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.
Files changed (173) hide show
  1. package/README.md +14 -1
  2. package/build/QSTest/functions/IntegrationTest/IntegrationTest.test.js +47 -0
  3. package/build/QSTest/functions/IntegrationTest/IntegrationTest.test.js.map +1 -0
  4. package/build/QSTest/functions/IntegrationTest/src/local.js +33 -0
  5. package/build/QSTest/functions/IntegrationTest/src/local.js.map +1 -0
  6. package/build/QSTest/functions/IntegrationTest/src/main.js +28 -0
  7. package/build/QSTest/functions/IntegrationTest/src/main.js.map +1 -0
  8. package/build/{commands → src/commands}/actions/initQuestions.js +18 -9
  9. package/build/src/commands/actions/initQuestions.js.map +1 -0
  10. package/build/src/commands/actions/makePackageJson.js.map +1 -0
  11. package/build/{commands → src/commands}/actions/setupPackageJson.js +20 -11
  12. package/build/src/commands/actions/setupPackageJson.js.map +1 -0
  13. package/build/src/commands/actions/updateDeployedPermissions.js +118 -0
  14. package/build/src/commands/actions/updateDeployedPermissions.js.map +1 -0
  15. package/build/{commands → src/commands}/build.js +18 -9
  16. package/build/src/commands/build.js.map +1 -0
  17. package/build/{commands → src/commands}/builtin/ApiDocMaker.js +21 -14
  18. package/build/src/commands/builtin/ApiDocMaker.js.map +1 -0
  19. package/build/{commands → src/commands}/builtin/BuiltInHandler.js +26 -14
  20. package/build/src/commands/builtin/BuiltInHandler.js.map +1 -0
  21. package/build/{commands → src/commands}/builtin/DeployBuiltInZip.js +1 -2
  22. package/build/src/commands/builtin/DeployBuiltInZip.js.map +1 -0
  23. package/build/src/commands/builtin/ExportWebroot.js +253 -0
  24. package/build/src/commands/builtin/ExportWebroot.js.map +1 -0
  25. package/build/{commands → src/commands}/builtin/StageWebrootZip.js +28 -15
  26. package/build/src/commands/builtin/StageWebrootZip.js.map +1 -0
  27. package/build/src/commands/builtin/webroot-export/s3webroot.js +128 -0
  28. package/build/src/commands/builtin/webroot-export/s3webroot.js.map +1 -0
  29. package/build/{commands → src/commands}/create.js +18 -9
  30. package/build/src/commands/create.js.map +1 -0
  31. package/build/{commands → src/commands}/deploy.js +25 -14
  32. package/build/src/commands/deploy.js.map +1 -0
  33. package/build/{commands → src/commands}/doctor.js +19 -10
  34. package/build/src/commands/doctor.js.map +1 -0
  35. package/build/{commands → src/commands}/help.js +34 -24
  36. package/build/src/commands/help.js.map +1 -0
  37. package/build/{commands → src/commands}/info.js +18 -9
  38. package/build/src/commands/info.js.map +1 -0
  39. package/build/{commands → src/commands}/init.js +67 -18
  40. package/build/src/commands/init.js.map +1 -0
  41. package/build/{commands → src/commands}/package.js +51 -10
  42. package/build/src/commands/package.js.map +1 -0
  43. package/build/{commands → src/commands}/publish.js +58 -22
  44. package/build/src/commands/publish.js.map +1 -0
  45. package/build/{commands → src/commands}/settings.js +18 -9
  46. package/build/src/commands/settings.js.map +1 -0
  47. package/build/{commands → src/commands}/start.js +20 -10
  48. package/build/src/commands/start.js.map +1 -0
  49. package/build/{commands → src/commands}/test.js +18 -9
  50. package/build/src/commands/test.js.map +1 -0
  51. package/build/{commands → src/commands}/update.js +19 -9
  52. package/build/src/commands/update.js.map +1 -0
  53. package/build/src/commands/user.js.map +1 -0
  54. package/build/{expressRoutes → src/expressRoutes}/all.js +19 -9
  55. package/build/src/expressRoutes/all.js.map +1 -0
  56. package/build/src/expressRoutes/api.js.map +1 -0
  57. package/build/{expressRoutes → src/expressRoutes}/functionBinder.js +181 -26
  58. package/build/src/expressRoutes/functionBinder.js.map +1 -0
  59. package/build/{integration-tests → src/integration-tests}/quickstart-scenario.test.js +1 -1
  60. package/build/src/integration-tests/quickstart-scenario.test.js.map +1 -0
  61. package/build/{lib → src/lib}/CaseUtils.js +5 -6
  62. package/build/src/lib/CaseUtils.js.map +1 -0
  63. package/build/{lib → src/lib}/DirectoryUtils.js +4 -4
  64. package/build/src/lib/DirectoryUtils.js.map +1 -0
  65. package/build/{lib → src/lib}/ESBuild.js +19 -10
  66. package/build/src/lib/ESBuild.js.map +1 -0
  67. package/build/src/lib/IdSrc.js +67 -0
  68. package/build/src/lib/IdSrc.js.map +1 -0
  69. package/build/{lib → src/lib}/LiftConfig.js +7 -7
  70. package/build/src/lib/LiftConfig.js.map +1 -0
  71. package/build/{lib → src/lib}/LiftVersion.js +21 -11
  72. package/build/src/lib/LiftVersion.js.map +1 -0
  73. package/build/{lib → src/lib}/Tests/fileCompare.test.js +17 -7
  74. package/build/src/lib/Tests/fileCompare.test.js.map +1 -0
  75. package/build/src/lib/TypeCheck.js +1206 -0
  76. package/build/src/lib/TypeCheck.js.map +1 -0
  77. package/build/{lib → src/lib}/askQuestion.js +18 -9
  78. package/build/src/lib/askQuestion.js.map +1 -0
  79. package/build/{lib → src/lib}/executeCommand.js +2 -3
  80. package/build/src/lib/executeCommand.js.map +1 -0
  81. package/build/{lib → src/lib}/fileCompare.js +2 -3
  82. package/build/src/lib/fileCompare.js.map +1 -0
  83. package/build/{lib → src/lib}/openAPI/ApiBuildCollector.js +1 -2
  84. package/build/src/lib/openAPI/ApiBuildCollector.js.map +1 -0
  85. package/build/{lib → src/lib}/openAPI/WebrootFileSupport.js +18 -9
  86. package/build/src/lib/openAPI/WebrootFileSupport.js.map +1 -0
  87. package/build/src/lib/openAPI/openApiConstruction.js +435 -0
  88. package/build/src/lib/openAPI/openApiConstruction.js.map +1 -0
  89. package/build/{lib → src/lib}/pathResolve.js +1 -2
  90. package/build/src/lib/pathResolve.js.map +1 -0
  91. package/build/{lib → src/lib}/utils.js +20 -11
  92. package/build/src/lib/utils.js.map +1 -0
  93. package/build/{lift.js → src/lift.js} +19 -9
  94. package/build/src/lift.js.map +1 -0
  95. package/package.json +6 -4
  96. package/src/commands/actions/updateDeployedPermissions.ts +80 -0
  97. package/src/commands/builtin/ApiDocMaker.ts +17 -10
  98. package/src/commands/builtin/BuiltInHandler.ts +7 -2
  99. package/src/commands/builtin/ExportWebroot.ts +195 -0
  100. package/src/commands/builtin/StageWebrootZip.ts +13 -5
  101. package/src/commands/builtin/prebuilt-zips/API.zip +0 -0
  102. package/src/commands/builtin/prebuilt-zips/FileServe.zip +0 -0
  103. package/src/commands/builtin/prebuilt-zips/Webroot.zip +0 -0
  104. package/src/commands/builtin/webroot-export/s3webroot.ts +82 -0
  105. package/src/commands/deploy.ts +6 -4
  106. package/src/commands/help.ts +4 -2
  107. package/src/commands/init.ts +51 -8
  108. package/src/commands/package.ts +35 -2
  109. package/src/commands/publish.ts +38 -13
  110. package/src/commands/start.ts +2 -1
  111. package/src/commands/update.ts +1 -0
  112. package/src/expressRoutes/all.ts +1 -0
  113. package/src/expressRoutes/functionBinder.ts +155 -15
  114. package/src/integration-tests/quickstart-scenario.test.ts +1 -1
  115. package/src/lib/DirectoryUtils.ts +2 -1
  116. package/src/lib/IdSrc.ts +17 -4
  117. package/src/lib/TypeCheck.ts +1171 -0
  118. package/src/lib/executeCommand.ts +1 -1
  119. package/src/lib/openAPI/openApiConstruction.ts +230 -41
  120. package/src/lift.ts +2 -2
  121. package/templateData/app/MainPage.tsx +10 -0
  122. package/templateData/app/app.tsx +39 -0
  123. package/templateData/function-main-ts +8 -1
  124. package/templateData/webroot/app.js +7 -7
  125. package/build/commands/actions/initQuestions.js.map +0 -1
  126. package/build/commands/actions/makePackageJson.js.map +0 -1
  127. package/build/commands/actions/setupPackageJson.js.map +0 -1
  128. package/build/commands/build.js.map +0 -1
  129. package/build/commands/builtin/ApiDocMaker.js.map +0 -1
  130. package/build/commands/builtin/BuiltInHandler.js.map +0 -1
  131. package/build/commands/builtin/DeployBuiltInZip.js.map +0 -1
  132. package/build/commands/builtin/StageWebrootZip.js.map +0 -1
  133. package/build/commands/builtin/prebuilt-zips/API.zip +0 -0
  134. package/build/commands/builtin/prebuilt-zips/FileServe.zip +0 -0
  135. package/build/commands/builtin/prebuilt-zips/Webroot.zip +0 -0
  136. package/build/commands/create.js.map +0 -1
  137. package/build/commands/deploy.js.map +0 -1
  138. package/build/commands/doctor.js.map +0 -1
  139. package/build/commands/help.js.map +0 -1
  140. package/build/commands/info.js.map +0 -1
  141. package/build/commands/init.js.map +0 -1
  142. package/build/commands/package.js.map +0 -1
  143. package/build/commands/publish.js.map +0 -1
  144. package/build/commands/settings.js.map +0 -1
  145. package/build/commands/start.js.map +0 -1
  146. package/build/commands/test.js.map +0 -1
  147. package/build/commands/update.js.map +0 -1
  148. package/build/commands/user.js.map +0 -1
  149. package/build/expressRoutes/all.js.map +0 -1
  150. package/build/expressRoutes/api.js.map +0 -1
  151. package/build/expressRoutes/functionBinder.js.map +0 -1
  152. package/build/integration-tests/quickstart-scenario.test.js.map +0 -1
  153. package/build/lib/CaseUtils.js.map +0 -1
  154. package/build/lib/DirectoryUtils.js.map +0 -1
  155. package/build/lib/ESBuild.js.map +0 -1
  156. package/build/lib/IdSrc.js +0 -43
  157. package/build/lib/IdSrc.js.map +0 -1
  158. package/build/lib/LiftConfig.js.map +0 -1
  159. package/build/lib/LiftVersion.js.map +0 -1
  160. package/build/lib/Tests/fileCompare.test.js.map +0 -1
  161. package/build/lib/askQuestion.js.map +0 -1
  162. package/build/lib/executeCommand.js.map +0 -1
  163. package/build/lib/fileCompare.js.map +0 -1
  164. package/build/lib/openAPI/ApiBuildCollector.js.map +0 -1
  165. package/build/lib/openAPI/WebrootFileSupport.js.map +0 -1
  166. package/build/lib/openAPI/openApiConstruction.js +0 -237
  167. package/build/lib/openAPI/openApiConstruction.js.map +0 -1
  168. package/build/lib/pathResolve.js.map +0 -1
  169. package/build/lib/utils.js.map +0 -1
  170. package/build/lift.js.map +0 -1
  171. /package/build/{commands → src/commands}/actions/makePackageJson.js +0 -0
  172. /package/build/{commands → src/commands}/user.js +0 -0
  173. /package/build/{expressRoutes → src/expressRoutes}/api.js +0 -0
@@ -0,0 +1,1206 @@
1
+ "use strict";
2
+ /*
3
+ Module for Constraint definitions and TypeCheck support
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ElementCheckType = exports.TypeConstraint = exports.ValueType = void 0;
7
+ exports.valueTypeFromString = valueTypeFromString;
8
+ exports.stringFromValueType = stringFromValueType;
9
+ exports.parseConstraintsToMap = parseConstraintsToMap;
10
+ exports.parseConstraints = parseConstraints;
11
+ exports.validate = validate;
12
+ /* eslint-disable */ // too screwed up. This file originated in an old JS project and was converted to TS quickly. TS-Standard is too restrictive for the style.
13
+ /**
14
+ * Enumeration of basic types
15
+ *
16
+ * - see [stringFromValueType](#module_TypeCheck..stringFromValueType)
17
+ * - see [valueTypeFromString](#module_TypeCheck..valueTypeFromString)
18
+ */
19
+ var ValueType;
20
+ (function (ValueType) {
21
+ ValueType[ValueType["none"] = 0] = "none";
22
+ ValueType[ValueType["number"] = 1] = "number";
23
+ ValueType[ValueType["string"] = 2] = "string";
24
+ ValueType[ValueType["boolean"] = 3] = "boolean";
25
+ ValueType[ValueType["object"] = 4] = "object";
26
+ ValueType[ValueType["array"] = 5] = "array";
27
+ ValueType[ValueType["regex"] = 6] = "regex";
28
+ })(ValueType || (exports.ValueType = ValueType = {}));
29
+ /**
30
+ * Base for all Constraint errors.
31
+ * Defines the identifying class archetype and consistent error message prefix
32
+ */
33
+ class ConstraintError extends Error {
34
+ constructor() {
35
+ super();
36
+ this.message = 'Constraint Error: ';
37
+ }
38
+ }
39
+ /**
40
+ * An error message for when a value fails validation.
41
+ */
42
+ class ConstraintFail extends ConstraintError {
43
+ constructor(failType, value) {
44
+ super();
45
+ this.message += `Failed ${failType}: ${value}`;
46
+ }
47
+ }
48
+ /**
49
+ * An error for when the basic type is wrong
50
+ */
51
+ class ConstraintBasicTypeError extends ConstraintError {
52
+ constructor(value, expType) {
53
+ super();
54
+ this.message += `Incorrect type ${typeof value}, (${expType} expected) ${value}`;
55
+ }
56
+ }
57
+ /**
58
+ * An error for when we expected null or undefined
59
+ */
60
+ // class NullConstraintError extends ConstraintError {
61
+ // constructor() {
62
+ // super();
63
+ // this.message += 'Expected NULL or undefined'
64
+ // }
65
+ // }
66
+ /**
67
+ * An error for when a min/max range has been violated, including what type of range.
68
+ */
69
+ class RangeConstraintError extends ConstraintError {
70
+ constructor(value, comp, rangeType = 'Number') {
71
+ super();
72
+ // we don't need to test both range ends, because we know we are here because of an error one way
73
+ // or the other.
74
+ if (value < comp) {
75
+ this.message += `${rangeType} ${value} is less than range minimum of ${comp}`;
76
+ }
77
+ else {
78
+ this.message += `${rangeType} ${value} exceeds range maximum of ${comp}`;
79
+ }
80
+ }
81
+ }
82
+ /**
83
+ * An error for when an integer was expected
84
+ */
85
+ class IntegerConstraintError extends ConstraintError {
86
+ constructor(value) {
87
+ super();
88
+ if (value === undefined) {
89
+ this.message += 'Integer expected';
90
+ }
91
+ else {
92
+ this.message += `Value ${value} is not an integer`;
93
+ }
94
+ }
95
+ }
96
+ /**
97
+ * An error for when a positive value was expected
98
+ */
99
+ class PositiveConstraintError extends ConstraintError {
100
+ constructor(value) {
101
+ super();
102
+ if (value === undefined) {
103
+ this.message += 'Positive value expected';
104
+ }
105
+ else {
106
+ this.message += `Value ${value} is not positive`;
107
+ }
108
+ }
109
+ }
110
+ /**
111
+ * An error for when a negative value was expected
112
+ */
113
+ class NegativeConstraintError extends ConstraintError {
114
+ constructor(value) {
115
+ super();
116
+ if (value === undefined) {
117
+ this.message += 'Positive value expected';
118
+ }
119
+ else {
120
+ this.message += `Value ${value} is not negative`;
121
+ }
122
+ }
123
+ }
124
+ /**
125
+ * An error for when zero was not expected.
126
+ */
127
+ class ZeroValueConstraintError extends ConstraintError {
128
+ constructor() {
129
+ super();
130
+ this.message += 'Zero is not an allowable value';
131
+ }
132
+ }
133
+ /**
134
+ * An error for declaring both ! and not ! variants of the same expression
135
+ */
136
+ class ConstraintConflictError extends ConstraintError {
137
+ constructor(conflictType) {
138
+ super();
139
+ this.message += `Both ${conflictType} and !${conflictType} declared`;
140
+ }
141
+ }
142
+ /**
143
+ * Base form of TypeConstraint.
144
+ * Defines the base type and the test method.
145
+ */
146
+ class TypeConstraint {
147
+ constructor(typeString = '') {
148
+ this.type = typeString.trim().toLowerCase();
149
+ }
150
+ /**
151
+ * Perform a runtime test of the value
152
+
153
+ * returns without throw if test was okay, otherwise throws a ConstraintError explaining the violation.
154
+ *
155
+ * @param value - value to test against this constraint
156
+ *
157
+ * @throws {ConstraintError} Error is thrown if test fails its constraints
158
+ */
159
+ test(value) {
160
+ if (typeof value !== this.type) {
161
+ throw new ConstraintBasicTypeError(value, this.type);
162
+ }
163
+ }
164
+ // Describes the constraint in printable terms (not really used, a bit redundant to describe)
165
+ toString() {
166
+ if (this.badName)
167
+ return `"${this.badName}" is not a recognized constraint for ${this.type}`;
168
+ if (this.note)
169
+ return this.note;
170
+ return '- No Constraint';
171
+ }
172
+ // describe the constraints in human terms.
173
+ describe() {
174
+ if (this.badName)
175
+ return `"${this.badName}" is not a recognized constraint for ${this.type}`;
176
+ if (this.note)
177
+ return this.note;
178
+ return 'No Constraint';
179
+ }
180
+ }
181
+ exports.TypeConstraint = TypeConstraint;
182
+ // /**
183
+ // * Enumeration of recognized status for a parameter or return constraint
184
+ // */
185
+ // enum ConstraintStatus {
186
+ // None = "", // not parsed
187
+ // NotConstraint = "NotConstraint", // doesn't start with '-', treat as description
188
+ // Error = "Error", // parsing error
189
+ // NotProvided = "NotProvided", // no constraint block
190
+ //
191
+ //
192
+ // }
193
+ /**
194
+ * Null only applies to objects.
195
+ */
196
+ // class NullConstraint extends TypeConstraint {
197
+ // test(value) {
198
+ // if(value || typeof value !== 'object') {
199
+ // throw new NullConstraintError()
200
+ // }
201
+ // }
202
+ // }
203
+ /**
204
+ * Constraints recorded on a number
205
+ * Integer, Positive, Negative, NotZero, min, max
206
+ */
207
+ class NumberConstraint extends TypeConstraint {
208
+ constructor() {
209
+ super('number');
210
+ this.isInteger = false; // number must be an integer
211
+ this.isPositive = false; // number must be positive
212
+ this.isNegative = false; // number must be negative
213
+ this.notZero = false; // number must not be zero
214
+ }
215
+ test(value) {
216
+ super.test(value);
217
+ if (this.isInteger) {
218
+ if (Math.floor(value) !== value) {
219
+ throw new IntegerConstraintError(value);
220
+ }
221
+ }
222
+ if (this.notZero) {
223
+ if (value === 0) {
224
+ throw new ZeroValueConstraintError();
225
+ }
226
+ }
227
+ if (this.isPositive && this.isNegative) {
228
+ throw new ConstraintConflictError('positive');
229
+ }
230
+ if (this.isPositive) {
231
+ if (value < 0) {
232
+ throw new PositiveConstraintError(value);
233
+ }
234
+ }
235
+ if (this.isNegative) {
236
+ if (value < 0) {
237
+ throw new NegativeConstraintError(value);
238
+ }
239
+ }
240
+ if (this.min !== undefined) {
241
+ if (value < this.min) {
242
+ throw new RangeConstraintError(value, this.min);
243
+ }
244
+ }
245
+ if (this.max !== undefined) {
246
+ if (value > this.max) {
247
+ throw new RangeConstraintError(value, this.max);
248
+ }
249
+ }
250
+ if (this.maxx !== undefined) {
251
+ if (value >= this.maxx) {
252
+ throw new RangeConstraintError(value, this.maxx);
253
+ }
254
+ }
255
+ }
256
+ toString() {
257
+ const keys = [];
258
+ if (this.isInteger)
259
+ keys.push('Integer');
260
+ if (this.notZero)
261
+ keys.push('Not Zero');
262
+ if (this.isPositive)
263
+ keys.push('Positive');
264
+ if (this.isNegative)
265
+ keys.push('Negative');
266
+ if (this.min !== undefined)
267
+ keys.push(`Min = ${this.min}`);
268
+ if (this.max !== undefined)
269
+ keys.push(`Max = ${this.max}`);
270
+ if (this.maxx !== undefined)
271
+ keys.push(`Maxx = ${this.maxx}`);
272
+ if (this.note)
273
+ keys.push(this.note);
274
+ return (keys.length > 0) ? '- ' + keys.join(',') : super.toString();
275
+ }
276
+ describe() {
277
+ const keys = [];
278
+ if (this.isInteger)
279
+ keys.push('number must be an integer');
280
+ if (this.notZero)
281
+ keys.push('number must not be zero');
282
+ if (this.isPositive)
283
+ keys.push('number must be positive');
284
+ if (this.isNegative)
285
+ keys.push('number must be negative');
286
+ if (this.min !== undefined)
287
+ keys.push(`Minimum value is ${this.min}`);
288
+ if (this.max !== undefined)
289
+ keys.push(`Maximum value is ${this.max}`);
290
+ if (this.maxx !== undefined)
291
+ keys.push(`Maximum value is less than ${this.maxx}`);
292
+ if (this.note || this.badName)
293
+ keys.push(super.describe());
294
+ return (keys.length > 0) ? keys.join('\n') : super.describe();
295
+ }
296
+ }
297
+ /**
298
+ * Constraints recorded on a string
299
+ * minLength, maxLength, (!)startsWith, (!)endsWith, (!)contains, (!)match
300
+ */
301
+ class StringConstraint extends TypeConstraint {
302
+ constructor() {
303
+ super('string');
304
+ }
305
+ test(value) {
306
+ super.test(value);
307
+ if (this.minLength) {
308
+ if (value.length < this.minLength) {
309
+ throw new RangeConstraintError(value.length, this.minLength, 'String Length');
310
+ }
311
+ }
312
+ if (this.maxLength) {
313
+ if (value.length > this.maxLength) {
314
+ throw new RangeConstraintError(value.length, this.maxLength, 'String Length');
315
+ }
316
+ }
317
+ if (this.startsWith && this.notStartsWith) {
318
+ throw new ConstraintConflictError('startsWith');
319
+ }
320
+ if (this.startsWith || this.notStartsWith) {
321
+ const comp = this.startsWith || this.notStartsWith || '';
322
+ const not = !!this.notStartsWith;
323
+ if (value.substring(0, comp.length) === comp) {
324
+ if (not)
325
+ throw new ConstraintFail('!startsWith', value);
326
+ }
327
+ else {
328
+ if (!not)
329
+ throw new ConstraintFail('startsWith', value);
330
+ }
331
+ }
332
+ if (this.endsWith && this.notEndsWith) {
333
+ throw new ConstraintConflictError('endsWith');
334
+ }
335
+ if (this.endsWith || this.notEndsWith) {
336
+ const comp = this.endsWith || this.notEndsWith || '';
337
+ const not = !!this.notEndsWith;
338
+ if (value.substring(value.length - comp.length) === comp) {
339
+ if (not)
340
+ throw new ConstraintFail('!endsWith', value);
341
+ }
342
+ else {
343
+ if (!not)
344
+ throw new ConstraintFail('endsWith', value);
345
+ }
346
+ }
347
+ if (this.contains && this.notContains) {
348
+ throw new ConstraintConflictError('contains');
349
+ }
350
+ if (this.contains || this.notContains) {
351
+ const comp = this.contains || this.notContains;
352
+ const not = !!this.notContains;
353
+ if (value.indexOf(comp) !== -1) {
354
+ if (not)
355
+ throw new ConstraintFail('!contains', value);
356
+ }
357
+ else {
358
+ if (!not)
359
+ throw new ConstraintFail('contains', value);
360
+ }
361
+ }
362
+ if (this.match && this.notMatch) {
363
+ throw new ConstraintConflictError('match');
364
+ }
365
+ if (this.match || this.notMatch) {
366
+ const comp = this.match || this.notMatch;
367
+ const not = !!this.notMatch;
368
+ const re = new RegExp(comp || '');
369
+ if (re.test(value)) {
370
+ if (not)
371
+ throw new ConstraintFail('!match', value);
372
+ }
373
+ else {
374
+ if (!not)
375
+ throw new ConstraintFail('match', value);
376
+ }
377
+ }
378
+ }
379
+ toString() {
380
+ const keys = [];
381
+ if (this.minLength)
382
+ keys.push(`Min Length = ${this.minLength}`);
383
+ if (this.maxLength)
384
+ keys.push(`Max Length = ${this.maxLength}`);
385
+ if (this.startsWith)
386
+ keys.push(`Starts With = ${this.startsWith}`);
387
+ if (this.notStartsWith)
388
+ keys.push(`!StartsWith = ${this.startsWith}`);
389
+ if (this.endsWith)
390
+ keys.push(`Ends With = ${this.endsWith}`);
391
+ if (this.notEndsWith)
392
+ keys.push(`!EndsWith = ${this.endsWith}`);
393
+ if (this.contains)
394
+ keys.push(`Contains = ${this.contains}`);
395
+ if (this.notContains)
396
+ keys.push(`!Contains = ${this.notContains}`);
397
+ if (this.match)
398
+ keys.push(`Match = ${this.match}`);
399
+ if (this.notMatch)
400
+ keys.push(`!Match = ${this.notMatch}`);
401
+ if (this.note)
402
+ keys.push(this.note);
403
+ return (keys.length > 0) ? '- ' + keys.join(',') : super.toString();
404
+ }
405
+ describe() {
406
+ const keys = [];
407
+ if (this.minLength)
408
+ keys.push(`string must be at least ${this.minLength} characters long`);
409
+ if (this.maxLength)
410
+ keys.push(`string must consist of less than ${this.maxLength} characters`);
411
+ if (this.startsWith)
412
+ keys.push(`string must start with "${this.startsWith}"`);
413
+ if (this.notStartsWith)
414
+ keys.push(`string must NOT start with "${this.startsWith}"`);
415
+ if (this.endsWith)
416
+ keys.push(`string must end with "${this.endsWith}"`);
417
+ if (this.notEndsWith)
418
+ keys.push(`string must NOT end with "${this.endsWith}"`);
419
+ if (this.contains)
420
+ keys.push(`must contain substring "${this.contains}"`);
421
+ if (this.notContains)
422
+ keys.push(`must NOT contain substring "${this.notContains}"`);
423
+ if (this.match)
424
+ keys.push(`must match Regular Expression "${this.match}"`);
425
+ if (this.notMatch)
426
+ keys.push(`must NOT match RegExp "${this.notMatch}"`);
427
+ if (this.note || this.badName)
428
+ keys.push(super.describe());
429
+ return (keys.length > 0) ? keys.join('\n') : super.describe();
430
+ }
431
+ }
432
+ /**
433
+ * Constraints recorded on an object
434
+ * (!)empty, (!)hasProperties, notNested, noPrototype, canSerialize, noUndefinedProps
435
+ */
436
+ class ObjectConstraint extends TypeConstraint {
437
+ constructor() {
438
+ super('object');
439
+ }
440
+ test(value) {
441
+ super.test(value);
442
+ if (this.empty && this.notEmpty) {
443
+ throw new ConstraintConflictError('empty');
444
+ }
445
+ if ((this.hasProperties != null) && (this.notHasProperties != null)) {
446
+ const collisions = [];
447
+ for (const has of this.hasProperties) {
448
+ if (this.notHasProperties.includes(has)) {
449
+ collisions.push(has);
450
+ }
451
+ }
452
+ if (collisions.length > 0) {
453
+ throw new ConstraintConflictError('hasProperties "' + collisions.join(',') + '"');
454
+ }
455
+ }
456
+ if (this.empty) {
457
+ if (Object.getOwnPropertyNames(value).length > 0) {
458
+ throw new ConstraintFail('empty', 'object contains ' + Object.getOwnPropertyNames(value).length + ' props');
459
+ }
460
+ }
461
+ if (this.notEmpty) {
462
+ if (Object.getOwnPropertyNames(value).length === 0) {
463
+ throw new ConstraintFail('!empty', value);
464
+ }
465
+ }
466
+ if (this.hasProperties != null) {
467
+ for (const has of this.hasProperties) {
468
+ if (!value.hasOwnProperty(has)) {
469
+ throw new ConstraintFail('hasProperties', has);
470
+ }
471
+ }
472
+ }
473
+ if (this.notHasProperties != null) {
474
+ for (const hasnot of this.notHasProperties) {
475
+ if (value.hasOwnProperty(hasnot)) {
476
+ throw new ConstraintFail('!hasProperties', hasnot);
477
+ }
478
+ }
479
+ }
480
+ if (this.notNested) {
481
+ for (const p of Object.getOwnPropertyNames(value)) {
482
+ const v = value[p];
483
+ if (typeof v === 'object') {
484
+ if (!Array.isArray(v)) {
485
+ throw new ConstraintFail('notNested', p);
486
+ }
487
+ }
488
+ }
489
+ }
490
+ if (this.noPrototype) {
491
+ const prot = Object.getPrototypeOf(value);
492
+ const name = prot && prot.constructor.name;
493
+ if (name && name !== 'Object') {
494
+ throw new ConstraintFail('noPrototype', value);
495
+ }
496
+ }
497
+ if (this.canSerialize) {
498
+ let json;
499
+ try {
500
+ json = JSON.stringify(value);
501
+ }
502
+ catch (e) {
503
+ }
504
+ if (!json) {
505
+ throw new ConstraintFail('canSerialize', value);
506
+ }
507
+ }
508
+ if (this.noFalseyProps) {
509
+ for (const p of Object.getOwnPropertyNames(value)) {
510
+ const v = value[p];
511
+ if (!v) {
512
+ throw new ConstraintFail('noFalseyProps', p);
513
+ }
514
+ }
515
+ }
516
+ if (this.noTruthyProps) {
517
+ for (const p of Object.getOwnPropertyNames(value)) {
518
+ const v = value[p];
519
+ if (v) {
520
+ throw new ConstraintFail('noTruthyProps', p);
521
+ }
522
+ }
523
+ }
524
+ if (this.instanceOf) {
525
+ if (value.constructor.name !== this.instanceOf) {
526
+ throw new ConstraintFail('instanceOf (' + this.instanceOf + ')', value.constructor.name);
527
+ }
528
+ }
529
+ if (this.notInstanceOf) {
530
+ if (value.constructor.name === this.notInstanceOf) {
531
+ throw new ConstraintFail('!instanceOf', this.notInstanceOf);
532
+ }
533
+ }
534
+ }
535
+ toString() {
536
+ const keys = [];
537
+ if (this.empty)
538
+ keys.push('Empty');
539
+ if (this.notEmpty)
540
+ keys.push('!Empty');
541
+ if (this.hasProperties != null)
542
+ keys.push(`Has Properties =${this.hasProperties.join(',')}`);
543
+ if (this.notHasProperties != null)
544
+ keys.push(`!Has Properties =${this.notHasProperties}`);
545
+ if (this.notNested)
546
+ keys.push('Not Nested');
547
+ if (this.noPrototype)
548
+ keys.push('No Prototype');
549
+ if (this.canSerialize)
550
+ keys.push('Can Serialize');
551
+ if (this.noFalseyProps)
552
+ keys.push('No Falsey Props');
553
+ if (this.noTruthyProps)
554
+ keys.push('No Truthy Props');
555
+ if (this.instanceOf)
556
+ keys.push(`Instance Of = ${this.instanceOf}`);
557
+ if (this.notInstanceOf)
558
+ keys.push(`Not an instance of ${this.notInstanceOf}`);
559
+ if (this.note)
560
+ keys.push(this.note);
561
+ return (keys.length > 0) ? '- ' + keys.join(',') : super.toString();
562
+ }
563
+ describe() {
564
+ const keys = [];
565
+ if (this.empty)
566
+ keys.push('object must be empty');
567
+ if (this.notEmpty)
568
+ keys.push('object must not be empty');
569
+ if (this.hasProperties != null)
570
+ keys.push(`object must contain properties "${this.hasProperties.join(',')}"`);
571
+ if (this.notHasProperties != null)
572
+ keys.push(`object must not contain properties "${this.notHasProperties.join(',')}"`);
573
+ if (this.notNested)
574
+ keys.push('object must not contain nested objects');
575
+ if (this.noPrototype)
576
+ keys.push('object must not derive from a prototype');
577
+ if (this.canSerialize)
578
+ keys.push('object can be serialized');
579
+ if (this.noFalseyProps)
580
+ keys.push('object can contain no properties that evaluate as false');
581
+ if (this.noTruthyProps)
582
+ keys.push('object can contain no properties that evaluate as true');
583
+ if (this.instanceOf)
584
+ keys.push(`object must be an instance of "${this.instanceOf}"`);
585
+ if (this.notInstanceOf)
586
+ keys.push(`object must not be an instance of "${this.notInstanceOf}"`);
587
+ if (this.note || this.badName)
588
+ keys.push(super.describe());
589
+ return (keys.length > 0) ? keys.join('\n') : super.describe();
590
+ }
591
+ }
592
+ /**
593
+ * Enumeration of checkType parsed results.
594
+ *
595
+ * parameters (p1, p2) are parsed at same time, and meaning does vary per checkType.
596
+ */
597
+ var ElementCheckType;
598
+ (function (ElementCheckType) {
599
+ ElementCheckType[ElementCheckType["none"] = 0] = "none";
600
+ ElementCheckType[ElementCheckType["all"] = 1] = "all";
601
+ ElementCheckType[ElementCheckType["random"] = 2] = "random";
602
+ ElementCheckType[ElementCheckType["step"] = 3] = "step";
603
+ ElementCheckType[ElementCheckType["first"] = 4] = "first";
604
+ ElementCheckType[ElementCheckType["last"] = 5] = "last";
605
+ ElementCheckType[ElementCheckType["firstThenLast"] = 6] = "firstThenLast";
606
+ ElementCheckType[ElementCheckType["firstThenStep"] = 7] = "firstThenStep";
607
+ ElementCheckType[ElementCheckType["firstThenRandom"] = 8] = "firstThenRandom"; // test all up to (p1) elements, then up to (p2) of the remaining, chosen at random
608
+ })(ElementCheckType || (exports.ElementCheckType = ElementCheckType = {}));
609
+ /**
610
+ * Constraints recorded on an array
611
+ * minLength, maxLength, (!)contains, checkType, each
612
+ */
613
+ class ArrayConstraint extends TypeConstraint {
614
+ constructor() {
615
+ super('array');
616
+ this.elementConstraints = []; // elements are tested for compliance under these rules
617
+ this.elementCheckType = ElementCheckType.none; // defines the extent of runtime coverage on elements
618
+ this.elementCheckParameter = 0; // defined by elementCheckType
619
+ this.elementCheckParameter2 = 0; // defined by elementCheckType
620
+ }
621
+ test(value) {
622
+ if (!Array.isArray(value)) {
623
+ throw new ConstraintBasicTypeError(value, 'array');
624
+ }
625
+ const length = value.length;
626
+ if (this.minLength) {
627
+ if (length < this.minLength) {
628
+ throw new RangeConstraintError(length, this.minLength, 'Array Length');
629
+ }
630
+ }
631
+ if (this.maxLength) {
632
+ if (length > this.maxLength) {
633
+ throw new RangeConstraintError(length, this.maxLength, 'Array Length');
634
+ }
635
+ }
636
+ if (this.contains && this.notContains) {
637
+ throw new ConstraintConflictError('contains');
638
+ }
639
+ if (this.contains || this.notContains) {
640
+ const comp = this.contains || this.notContains;
641
+ const not = !!this.notContains;
642
+ if (value.includes(comp)) {
643
+ if (not)
644
+ throw new ConstraintFail('!contains', this.notContains);
645
+ }
646
+ else {
647
+ if (!not)
648
+ throw new ConstraintFail('contains', this.contains);
649
+ }
650
+ }
651
+ if (this.elementConstraints || this.elementCheckType) {
652
+ const checkType = this.elementCheckType === undefined ? ElementCheckType.all : this.elementCheckType;
653
+ let i = 0;
654
+ let count = 0;
655
+ let step = 1;
656
+ let firstCount = 0;
657
+ let thenCount = 0;
658
+ let counting = false;
659
+ const tested = {};
660
+ switch (checkType) {
661
+ case ElementCheckType.none:
662
+ firstCount = 0;
663
+ thenCount = 0;
664
+ counting = false;
665
+ break;
666
+ case ElementCheckType.all:
667
+ firstCount = length;
668
+ step = 1;
669
+ thenCount = 0;
670
+ counting = true;
671
+ break;
672
+ case ElementCheckType.first:
673
+ firstCount = parseInt('' + this.elementCheckParameter);
674
+ step = 1;
675
+ thenCount = 0;
676
+ counting = true;
677
+ break;
678
+ case ElementCheckType.last:
679
+ firstCount = 0;
680
+ step = 1;
681
+ thenCount = length - this.elementCheckParameter;
682
+ counting = false;
683
+ break;
684
+ case ElementCheckType.firstThenLast:
685
+ firstCount = parseInt('' + this.elementCheckParameter);
686
+ thenCount = length - this.elementCheckParameter;
687
+ if (thenCount < 0)
688
+ thenCount = length;
689
+ step = 1;
690
+ counting = true;
691
+ break;
692
+ case ElementCheckType.step:
693
+ firstCount = length;
694
+ thenCount = 0;
695
+ counting = true;
696
+ step = parseInt('' + this.elementCheckParameter);
697
+ break;
698
+ case ElementCheckType.random:
699
+ firstCount = 0;
700
+ thenCount = parseInt('' + this.elementCheckParameter);
701
+ step = 0;
702
+ counting = true;
703
+ break;
704
+ case ElementCheckType.firstThenStep:
705
+ firstCount = parseInt('' + this.elementCheckParameter);
706
+ thenCount = length - firstCount;
707
+ step = parseInt('' + this.elementCheckParameter2);
708
+ counting = true;
709
+ break;
710
+ case ElementCheckType.firstThenRandom:
711
+ firstCount = parseInt('' + this.elementCheckParameter);
712
+ thenCount = parseInt('' + this.elementCheckParameter2);
713
+ step = 0;
714
+ counting = true;
715
+ break;
716
+ }
717
+ while (i < length) {
718
+ if (counting) {
719
+ const ev = value[i];
720
+ let t = typeof ev;
721
+ if (Array.isArray(ev))
722
+ t = 'array';
723
+ const m = this.elementConstraints;
724
+ const c = m && m.get(t);
725
+ const tc = (c != null) || parseConstraints(t, '');
726
+ if (tc !== true && (tc === null || tc === void 0 ? void 0 : tc.test) != undefined)
727
+ tc.test(ev);
728
+ count++;
729
+ }
730
+ if ((checkType === ElementCheckType.last || checkType === ElementCheckType.firstThenLast) && i === thenCount) {
731
+ counting = true;
732
+ }
733
+ if (checkType === ElementCheckType.firstThenLast && i === firstCount) {
734
+ counting = false;
735
+ }
736
+ if (count >= firstCount) {
737
+ if (count >= firstCount + thenCount) {
738
+ break;
739
+ }
740
+ }
741
+ if (step) {
742
+ i += step;
743
+ }
744
+ else {
745
+ while (true) {
746
+ const rr = Math.floor(Math.random() * (length - count));
747
+ i = count + rr;
748
+ if (i < length && !tested[i]) {
749
+ tested[i] = true;
750
+ break;
751
+ }
752
+ }
753
+ }
754
+ }
755
+ }
756
+ }
757
+ toString() {
758
+ const keys = [];
759
+ if (this.minLength)
760
+ keys.push(`Min Length = ${this.minLength}`);
761
+ if (this.maxLength)
762
+ keys.push(`Max Length = ${this.maxLength}`);
763
+ if (this.contains)
764
+ keys.push(`Contains = ${this.contains}`);
765
+ if (this.notContains)
766
+ keys.push(`!Contains = ${this.notContains}`);
767
+ if (this.elementConstraints)
768
+ keys.push(`each element of the array has the following constraints by type ${listEachConstraints(this.elementConstraints)}`);
769
+ if (this.elementCheckType)
770
+ keys.push(`(elements will be tested using the ${checkTypeToString(this.elementCheckType, this.elementCheckParameter, this.elementCheckParameter2)} method)`);
771
+ if (this.note)
772
+ keys.push(this.note);
773
+ return (keys.length > 0) ? '- ' + keys.join(',') : super.toString();
774
+ }
775
+ describe() {
776
+ const keys = [];
777
+ if (this.minLength)
778
+ keys.push(`array must contain at least ${this.minLength} elements`);
779
+ if (this.maxLength)
780
+ keys.push(`array must contain no more than ${this.maxLength} elements`);
781
+ if (this.contains)
782
+ keys.push(`array must contain element value "${this.contains}"`);
783
+ if (this.notContains)
784
+ keys.push(`array must not contain an element value "${this.notContains}"`);
785
+ if (this.elementConstraints)
786
+ keys.push(`each element of the array has the following constraints by type ${listEachConstraints(this.elementConstraints)}`);
787
+ if (this.elementCheckType)
788
+ keys.push(`(elements will be tested using the ${checkTypeToString(this.elementCheckType, this.elementCheckParameter, this.elementCheckParameter2)} method)`);
789
+ if (this.note || this.badName)
790
+ keys.push(super.describe());
791
+ return (keys.length > 0) ? keys.join('\n') : super.describe();
792
+ }
793
+ }
794
+ function listEachConstraints(cmap) {
795
+ let out = '';
796
+ const types = cmap.keys();
797
+ const entries = cmap.entries();
798
+ let entry;
799
+ while ((entry = entries.next().value)) {
800
+ out += '<br/><b>' + entry[0] + ' elements:</b><br/>&nbsp;&nbsp; -';
801
+ out += entry[1].describe().replace(/\n/g, '<br/>&nbsp;&nbsp; - ');
802
+ }
803
+ return out;
804
+ }
805
+ /**
806
+ * Translates a type string (number, string, boolean, object, array, regex) into the corresponding ValueType enum
807
+ * Note that strings beside none, array, and regex are synonymous with the `typeof` operator value
808
+ * @param str
809
+ */
810
+ function valueTypeFromString(str) {
811
+ switch (str.trim().toLowerCase()) {
812
+ default: return str.trim().length ? str.includes('[]') ? ValueType.array : ValueType.object : ValueType.none;
813
+ case 'number': return ValueType.number;
814
+ case 'string': return ValueType.string;
815
+ case 'boolean': return ValueType.boolean;
816
+ case 'object': return ValueType.object;
817
+ case 'array': return ValueType.array;
818
+ case 'regex': return ValueType.regex;
819
+ case 'regexp': return ValueType.regex;
820
+ }
821
+ }
822
+ /**
823
+ * Translates a ValueType enum value into the corresponding string.
824
+ * Note that strings beside none, array, and regex are synonymous with the `typeof` operator value
825
+ * @param vt
826
+ */
827
+ function stringFromValueType(vt) {
828
+ switch (vt) {
829
+ case ValueType.none: return '';
830
+ case ValueType.number: return 'number';
831
+ case ValueType.string: return 'string';
832
+ case ValueType.boolean: return 'boolean';
833
+ case ValueType.object: return 'object';
834
+ case ValueType.array: return 'array';
835
+ case ValueType.regex: return 'regex';
836
+ }
837
+ }
838
+ /**
839
+ * Read either a value or a list from an expression value
840
+ * @param str
841
+ */
842
+ function constraintListParse(str = '') {
843
+ str.trim();
844
+ if (str.charAt(0) === '"' || str.charAt(0) === "'") {
845
+ str = str.substring(1, str.length - 1);
846
+ }
847
+ if (str.includes(',')) {
848
+ return str.split(','); // return the split array
849
+ }
850
+ if (isFinite(Number(str))) {
851
+ return Number(str);
852
+ }
853
+ return str; // return the unquoted string value
854
+ }
855
+ /**
856
+ * Used to parse the type+constraints blocks from an "each" directive list
857
+ * @param str
858
+ */
859
+ function eachListParse(str = '') {
860
+ const map = new Map();
861
+ const esplit = str.split('|');
862
+ for (const tblock of esplit) {
863
+ const ci = tblock.indexOf(',');
864
+ if (ci !== -1) {
865
+ const type = tblock.substring(0, ci).trim();
866
+ const cdef = tblock.substring(ci + 1);
867
+ const constraint = (parseConstraints(type, cdef) != null) || new TypeConstraint();
868
+ if (constraint !== undefined && constraint !== true) {
869
+ map.set(type, constraint);
870
+ }
871
+ }
872
+ }
873
+ return map;
874
+ }
875
+ /**
876
+ * Parse out the checkType and return the resulting type name and the parsed parameters in a structure.
877
+ * @param ctStr
878
+ * @return {{string}name,{number}[p1],{number}[p2]}
879
+ */
880
+ function parseCheckType(ctStr = '') {
881
+ let opi = ctStr.indexOf('(');
882
+ if (opi === -1)
883
+ opi = ctStr.length;
884
+ const name = ctStr.substring(0, opi);
885
+ let cpi = ctStr.indexOf(')', opi);
886
+ if (cpi === -1)
887
+ cpi = ctStr.length;
888
+ const p = ctStr.substring(opi + 1, cpi).split(',');
889
+ let p1, p2;
890
+ try {
891
+ p1 = p[0] && parseInt(p[0]);
892
+ p2 = p[1] && parseInt(p[1]);
893
+ }
894
+ catch (e) { }
895
+ return { name, p1, p2 };
896
+ }
897
+ function checkTypeToString(ct, p1, p2) {
898
+ switch (ct) {
899
+ case ElementCheckType.random:
900
+ return `random(${p1})`;
901
+ case ElementCheckType.step:
902
+ return `step(${p1})`;
903
+ case ElementCheckType.first:
904
+ return `first(${p1})`;
905
+ case ElementCheckType.last:
906
+ return `last(${p1})`;
907
+ case ElementCheckType.firstThenLast:
908
+ return `firstThenLast(${p1},${p2})`;
909
+ case ElementCheckType.firstThenStep:
910
+ return `firstThenStep(${p1},${p2})`;
911
+ case ElementCheckType.firstThenRandom:
912
+ return `firstThenRandom(${p1},${p2})`;
913
+ case ElementCheckType.none:
914
+ return 'none';
915
+ default:
916
+ case ElementCheckType.all:
917
+ return 'all';
918
+ }
919
+ }
920
+ function checkTypeFromString(ctstr) {
921
+ switch (ctstr.trim().toLowerCase()) {
922
+ case 'random': return ElementCheckType.random;
923
+ case 'step': return ElementCheckType.step;
924
+ case 'first': return ElementCheckType.first;
925
+ case 'last': return ElementCheckType.last;
926
+ case 'firstthenlast': return ElementCheckType.firstThenLast;
927
+ case 'firstthenstep': return ElementCheckType.firstThenStep;
928
+ case 'firstthenrandom': return ElementCheckType.firstThenRandom;
929
+ case 'none': return ElementCheckType.none;
930
+ default:
931
+ case 'all': return ElementCheckType.all;
932
+ }
933
+ }
934
+ // parse constraints from what may be more than one type (e.g. string|number)
935
+ function parseConstraintsToMap(typeString, blockSet = '') {
936
+ const map = new Map();
937
+ const types = typeString.split('|');
938
+ const blocks = blockSet.split(',');
939
+ for (let type of types) {
940
+ type = (type || '').trim();
941
+ const constraint = (parseConstraints(type, blockSet) != null) || new TypeConstraint();
942
+ if (constraint !== undefined && constraint !== true) {
943
+ map.set(type, constraint);
944
+ }
945
+ }
946
+ return map;
947
+ }
948
+ /**
949
+ * Given a block of text, parse as constraints and return the set if this is a constraint declaration
950
+ * otherwise, return ConstraintStatus.NotConstraint to signify this is a description block and not a constraint declaration
951
+ * @param type - the type parsed from the param or return declaration
952
+ * @param block - the block of text to evaluate
953
+ * @param delim - the split delimiter (defaults to ',')
954
+ */
955
+ function parseConstraints(type, block, delim = ',') {
956
+ let constraint;
957
+ if (!block || !type)
958
+ return;
959
+ const valueType = valueTypeFromString(type);
960
+ let cblock = block.trim();
961
+ // get any constraint parameters
962
+ let fpi = cblock.indexOf('(');
963
+ while (fpi !== -1) {
964
+ let cpi = cblock.indexOf(')', fpi);
965
+ if (cpi === -1)
966
+ cpi = cblock.length;
967
+ const swap = cblock.substring(fpi, cpi).replace(/,/g, ';;');
968
+ cblock = cblock.substring(0, fpi) + swap + cblock.substring(cpi);
969
+ fpi = cblock.indexOf('(', cpi);
970
+ }
971
+ const expressions = cblock.split(delim);
972
+ for (let expr of expressions) {
973
+ let expVal;
974
+ let params;
975
+ let not = false;
976
+ expr = expr.trim();
977
+ if (!expr.startsWith('match') && !expr.startsWith('!match')) {
978
+ const cpi = expr.indexOf('(');
979
+ if (cpi !== -1) {
980
+ params = expr.substring(cpi).replace(/;;/g, ',').trim();
981
+ if (params.charAt(0) === '(')
982
+ params = params.substring(1);
983
+ if (params.charAt(params.length - 1) === ')')
984
+ params = params.substring(0, params.length - 1);
985
+ expr = expr.substring(0, cpi).trim();
986
+ if (expr === 'each') {
987
+ expVal = eachListParse(params);
988
+ }
989
+ else {
990
+ expVal = constraintListParse(params);
991
+ }
992
+ }
993
+ }
994
+ if (expr.charAt(0) === '!') {
995
+ not = true;
996
+ expr = expr.substring(1);
997
+ }
998
+ if (expr.includes('=')) {
999
+ const p = expr.split('=');
1000
+ if (p.length > 2) {
1001
+ p[1] = p.slice(1).join('=');
1002
+ }
1003
+ expr = p[0].trim();
1004
+ if (expr === 'each') {
1005
+ expVal = eachListParse(p[1]);
1006
+ }
1007
+ else {
1008
+ expVal = constraintListParse(p[1]);
1009
+ }
1010
+ }
1011
+ expr = expr.trim().toLowerCase();
1012
+ switch (valueType) {
1013
+ case ValueType.number:
1014
+ constraint = constraint || new NumberConstraint();
1015
+ switch (expr) {
1016
+ case 'noconstraint':
1017
+ case 'no constraint':
1018
+ return constraint; // early exit if we encounter "- No Constraint"
1019
+ /* Integer, Positive, Negative, NotZero, min, max */
1020
+ case 'integer':
1021
+ constraint.isInteger = true;
1022
+ break;
1023
+ case 'positive':
1024
+ constraint.isPositive = true;
1025
+ break;
1026
+ case 'negative':
1027
+ constraint.isNegative = true;
1028
+ break;
1029
+ case 'notzero':
1030
+ case 'not zero':
1031
+ case 'nonzero':
1032
+ constraint.notZero = true;
1033
+ break;
1034
+ case 'min':
1035
+ constraint.min = expVal;
1036
+ break;
1037
+ case 'max':
1038
+ constraint.max = expVal;
1039
+ break;
1040
+ case 'maxx':
1041
+ constraint.maxx = expVal;
1042
+ break;
1043
+ case 'note':
1044
+ constraint.note = expVal;
1045
+ break;
1046
+ default:
1047
+ constraint.badName = expr;
1048
+ break;
1049
+ }
1050
+ break;
1051
+ case ValueType.string:
1052
+ // minLength, maxLength, (!)startsWith, (!)endsWith, (!)contains, (!)match
1053
+ constraint = constraint || new StringConstraint();
1054
+ switch (expr) {
1055
+ case 'noconstraint':
1056
+ case 'no constraint':
1057
+ return constraint; // early exit if we encounter "- No Constraint"
1058
+ case 'minlength':
1059
+ constraint.minLength = expVal;
1060
+ break;
1061
+ case 'maxlength':
1062
+ constraint.maxLength = expVal;
1063
+ break;
1064
+ case 'startswith':
1065
+ not ? constraint.notStartsWith = expVal : constraint.startsWith = expVal;
1066
+ break;
1067
+ case 'endswith':
1068
+ not ? constraint.notEndsWith = expVal : constraint.endsWith = expVal;
1069
+ break;
1070
+ case 'contains':
1071
+ not ? constraint.notContains = expVal : constraint.contains = expVal;
1072
+ break;
1073
+ case 'match':
1074
+ not ? constraint.notMatch = expVal : constraint.match = expVal;
1075
+ break;
1076
+ case 'note':
1077
+ constraint.note = expVal;
1078
+ break;
1079
+ default:
1080
+ constraint.badName = expr;
1081
+ break;
1082
+ }
1083
+ break;
1084
+ case ValueType.object:
1085
+ // (!)empty, (!)hasProperties, notNested, noPrototype, canSerialize, noUndefinedProps
1086
+ constraint = constraint || new ObjectConstraint();
1087
+ switch (expr) {
1088
+ case 'noconstraint':
1089
+ case 'no constraint':
1090
+ return constraint; // early exit if we encounter "- No Constraint"
1091
+ case 'empty':
1092
+ constraint.empty = !not;
1093
+ constraint.notEmpty = not;
1094
+ break;
1095
+ case 'hasproperties':
1096
+ case 'has properties':
1097
+ if (typeof expVal === 'string')
1098
+ expVal = [expVal];
1099
+ not ? constraint.notHasProperties = expVal : constraint.hasProperties = expVal;
1100
+ break;
1101
+ case 'notnested':
1102
+ case 'not nested':
1103
+ constraint.notNested = true;
1104
+ break;
1105
+ case 'noprototype':
1106
+ case 'no prototype':
1107
+ constraint.noPrototype = true;
1108
+ break;
1109
+ case 'canserialize':
1110
+ case 'can serialize':
1111
+ constraint.canSerialize = true;
1112
+ break;
1113
+ case 'notruthyprops':
1114
+ case 'no truthy props':
1115
+ constraint.noTruthyProps = true;
1116
+ break;
1117
+ case 'nofalseyprops':
1118
+ case 'no falsey props':
1119
+ constraint.noFalseyProps = true;
1120
+ break;
1121
+ case 'instanceof':
1122
+ case 'instance of':
1123
+ if (not)
1124
+ constraint.notInstanceOf = expVal;
1125
+ else
1126
+ constraint.instanceOf = expVal;
1127
+ break;
1128
+ case 'note':
1129
+ constraint.note = expVal;
1130
+ break;
1131
+ default:
1132
+ constraint.badName = expr;
1133
+ break;
1134
+ }
1135
+ break;
1136
+ case ValueType.array:
1137
+ // minLength, maxLength, (!)contains, each:
1138
+ constraint = constraint || new ArrayConstraint();
1139
+ switch (expr) {
1140
+ case 'noconstraint':
1141
+ case 'no constraint':
1142
+ return constraint; // early exit if we encounter "- No Constraint"
1143
+ case 'minlength':
1144
+ case 'min length':
1145
+ constraint.minLength = expVal;
1146
+ break;
1147
+ case 'maxlength':
1148
+ case 'max length':
1149
+ constraint.maxLength = expVal;
1150
+ break;
1151
+ case 'contains':
1152
+ not ? constraint.notContains = expVal : constraint.contains = expVal;
1153
+ break;
1154
+ case 'checktype':
1155
+ case 'check type':
1156
+ const psplit = (params || '').split(',');
1157
+ const pct = parseCheckType('' + expVal);
1158
+ constraint.elementCheckType = checkTypeFromString(pct.name);
1159
+ constraint.elementCheckParameter = (psplit[0] || pct.p1);
1160
+ constraint.elementCheckParameter2 = (psplit[1] || pct.p2);
1161
+ break;
1162
+ case 'each':
1163
+ const type = 'any';
1164
+ constraint.elementConstraints = expVal;
1165
+ break;
1166
+ case 'note':
1167
+ constraint.note = expVal;
1168
+ break;
1169
+ default:
1170
+ constraint.badName = expr;
1171
+ break;
1172
+ }
1173
+ break;
1174
+ default: // none, boolean, regex
1175
+ if (expr === 'no constraint')
1176
+ return;
1177
+ constraint = new TypeConstraint(stringFromValueType(valueType));
1178
+ break;
1179
+ }
1180
+ }
1181
+ return constraint;
1182
+ }
1183
+ /**
1184
+ * Simple test to see if a value adheres to a set of constraints
1185
+ */
1186
+ function validate(value, // The value to test for constraints. Must be one of the basic types supported by contraints
1187
+ constraintString // the constraints to test it against. Constraints listed must match the type being tested. Do not include < > brackets.
1188
+ ) {
1189
+ let type = typeof value;
1190
+ if (type === 'object') {
1191
+ if (Array.isArray(value))
1192
+ type = 'array';
1193
+ }
1194
+ const tc = parseConstraints(type, constraintString || '');
1195
+ let ok = '';
1196
+ try {
1197
+ if (tc != null)
1198
+ tc.test(value);
1199
+ }
1200
+ catch (e) {
1201
+ ok = e.message || e.toString();
1202
+ }
1203
+ return ok;
1204
+ }
1205
+ /* eslint-enable */
1206
+ //# sourceMappingURL=TypeCheck.js.map