-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcreteConditions.ts
910 lines (804 loc) · 36.6 KB
/
ConcreteConditions.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/**
* Concrete implementations of {@link Conditions/Types!ICondition | ICondition}, and their companion
* {@link Conditions/Types!ConditionConfig | ConditionConfig}.
*
* The conditions found here all use an ConditionConfig for supplying
* their configuration. Most Condition classes have a specific interface
* for their Config, such as {@link RangeConditionConfig} for {@link RangeCondition}.
*
* @module Conditions/ConcreteConditions
*/
import { LoggingCategory, LoggingLevel } from '../Interfaces/LoggerService';
import { CodingError, assertNotNull } from '../Utilities/ErrorHandling';
import { type IValueHost } from '../Interfaces/ValueHost';
import { IValueHostsManager } from '../Interfaces/ValueHostsManager';
import {
type ICondition,
ConditionCategory, ConditionEvaluateResult, SupportsDataTypeConverter, IEvaluateConditionDuringEdits
} from '../Interfaces/Conditions';
import { OneValueConditionBaseConfig, OneValueConditionBase } from './OneValueConditionBase';
import { StringConditionBaseConfig, StringConditionBase } from './StringConditionBase';
import { InputValueConditionBase, InputValueConditionBaseConfig } from './InputValueConditionBase';
import { EvaluateChildConditionResultsBase, EvaluateChildConditionResultsBaseConfig } from './EvaluateChildConditionResultsBase';
import { RegExpConditionBaseConfig, RegExpConditionBase } from './RegExpConditionBase';
import { ConditionType } from './ConditionTypes';
import { IValidationServices } from '../Interfaces/ValidationServices';
import { ComparersResult } from '../Interfaces/DataTypeComparerService';
import { TokenLabelAndValue } from '../Interfaces/MessageTokenSource';
import { IInputValueHost } from '../Interfaces/InputValueHost';
import { CompareToSecondValueHostConditionBase, CompareToSecondValueHostConditionBaseConfig } from './CompareToSecondValueHostConditionBase';
import { CompareToValueConditionBase, CompareToValueConditionBaseConfig } from './CompareToValueConditionBase';
import { IValidatorsValueHostBase } from '../Interfaces/ValidatorsValueHostBase';
import { toIInputValueHost } from '../ValueHosts/InputValueHost';
import { NumberConditionBase, NumberConditionBaseConfig } from './NumberConditionBase';
import { ConditionWithOneChildBase, ConditionWithOneChildBaseConfig } from './ConditionWithOneChildBase';
/**
* ConditionConfig for {@link DataTypeCheckCondition}
*/
export interface DataTypeCheckConditionConfig extends InputValueConditionBaseConfig {
}
/**
* Determines if the value of InputValue can be successfully converted to its native data type.
* Since the actual work of conversion occurs by the consuming system, this really just looks
* at both values. When InputValue is not undefined while Value is undefined, it reports an error
* as the converter could not get a valid value to store in the Value.
* Supports these tokens:
* {ConversionError} - Uses the value from IInputValueHost.getConversionErrorMessage()
*/
export class DataTypeCheckCondition extends InputValueConditionBase<DataTypeCheckConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.DataTypeCheck; }
protected evaluateInputValue(value: any, valueHost: IInputValueHost,
valueHostsManager: IValueHostsManager): ConditionEvaluateResult {
// value has already been proven to be something other than undefined...
return valueHost.getValue() !== undefined ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
public override getValuesForTokens(valueHost: IValidatorsValueHostBase, valueHostsManager: IValueHostsManager): Array<TokenLabelAndValue> {
let list: Array<TokenLabelAndValue> = [];
list = list.concat(super.getValuesForTokens(valueHost, valueHostsManager));
// same order of precidence as in Evaluate
let ivh = toIInputValueHost(valueHost);
if (ivh)
list.push({
tokenLabel: 'ConversionError',
associatedValue: ivh.getConversionErrorMessage() ?? null,
purpose: 'message'
});
return list;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.DataTypeCheck;
}
}
/**
* ConditionConfig for {@link RequireTextCondition}
*/
export interface RequireTextConditionConfig extends OneValueConditionBaseConfig {
/**
* Removes leading and trailing whitespace before evaluating the string.
* Only used with ValidateOption.DuringEdit = true as the string
* comes from the Input value, which is actively being edited.
* Your parser that moves data from Input to Native values is expected
* to do its own trimming, leaving the DuringEdit = false no need to trim.
*/
trim?: boolean;
/**
* Normally a value of null is considered NoMatch so both an empty string and null are NoMatch.
* When this is set, it determines the value.
* If you want to consider null as valid, supply Match. If you don't want to evaluate null
* at all, supply Undetermined.
*/
nullValueResult?: ConditionEvaluateResult;
}
/**
* For any input field/element whose native data is a string to determine if the required
* rule has been met or not, optionally require the absence of surrounding whitespace and optionally
* not null in native value.
* It has two evaluation features:
* - ICondition.evaluate() evaluates the native value. It ignores the trim property.
* - IEvaluateConditionDuringEdits.evaluateDuringEdit() evaluates the input value as the user is
* editing the input. It is invoked by InputValueHost.setInputValue(option.DuringEdit = true)
* and supports the trim property.
*/
export class RequireTextCondition extends OneValueConditionBase<RequireTextConditionConfig>
implements IEvaluateConditionDuringEdits
{
public static get DefaultConditionType(): ConditionType { return ConditionType.RequireText; }
public evaluate(valueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
valueHost = this.ensurePrimaryValueHost(valueHost, valueHostsManager);
let value = valueHost.getValue();
if (value === undefined)
return ConditionEvaluateResult.Undetermined;
if (value === null)
return this.config.nullValueResult ?? ConditionEvaluateResult.NoMatch;
if (typeof value !== 'string')
return ConditionEvaluateResult.Undetermined;
let text = value;
if (text == '')
return ConditionEvaluateResult.NoMatch;
return ConditionEvaluateResult.Match;
}
public evaluateDuringEdits(text: string, valueHost: IInputValueHost, services: IValidationServices): ConditionEvaluateResult {
if (this.config.trim ?? true)
text = text.trim();
if (text == '')
return ConditionEvaluateResult.NoMatch;
return ConditionEvaluateResult.Match;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.Require;
}
}
/**
* ConditionConfig for {@link NotNullCondition}
*/
export interface NotNullConditionConfig extends OneValueConditionBaseConfig {
}
/**
* To evaluate the Native Value when it may contain a null,
* and null is not valid in this case.
* Reports NoMatch when the value is null.
* See also RequireTextCondition which includes checking for null in addition to the empty string.
*/
export class NotNullCondition extends OneValueConditionBase<NotNullConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.NotNull; }
public evaluate(valueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
valueHost = this.ensurePrimaryValueHost(valueHost, valueHostsManager);
let value = valueHost.getValue();
if (value === undefined)
return ConditionEvaluateResult.Undetermined;
if (value === null)
return ConditionEvaluateResult.NoMatch;
return ConditionEvaluateResult.Match;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.Require;
}
}
/**
* ConditionConfig for {@link RegExpCondition}
*/
export interface RegExpConditionConfig extends RegExpConditionBaseConfig {
/**
* Used either expressionAsString or expression for the expression.
* When using expressionAsString, it is combined with IgnoreCase and Global to create
* the regular expression.
* Expressions must be compatible with JavaScript RegExp. Tests ignore captures and matches.
* Expect RegExp.test() to evaluate.
*/
expressionAsString?: string;
/**
* Used together with expressionAsString to set the case insensitive search option on the Regexp when true.
* If undefined, it is treated as false.
*/
ignoreCase?: boolean;
/**
* Used together with expressionAsString to set the multiline option on the Regexp when true.
* When used, ^ and $ match to newlines, not just start and end of the full string.
* If undefined, it is treated as false.
*/
multiline?: boolean;
/**
* Actual JavaScript Regular Expression object, complete with its flags.
* It is an alternative to expressionAsString. If both are supplied, this takes precedence.
*/
expression?: RegExp;
}
/**
* Evaluates the native value, which must be a string, against a regular expression.
* This implementation has the user supply the regular expression through
* RegExpConditionConfig.
*
* Supports validateOptions.duringEdit = true so long as Config.supportsDuringEdit
* is true or undefined.
*/
export class RegExpCondition extends RegExpConditionBase<RegExpConditionConfig>
{
/**
* Participates in releasing memory.
* While not required, the idea is to be a more friendly participant in the ecosystem.
* Note that once called, expect null reference errors to be thrown if any other functions
* try to use them.
*/
public dispose(): void
{
super.dispose();
this._savedRE = undefined!;
}
public static get DefaultConditionType(): ConditionType { return ConditionType.RegExp; }
private _savedRE: RegExp | null = null; // cache the results. By design, any change to the Config requires creating a new instance of the condition, discarding this
protected getRegExp(services: IValidationServices): RegExp {
if (!this._savedRE) {
let re: RegExp | null = this.config.expression ?? null;
if (!re) {
if (this.config.expressionAsString) {
// this may throw an exception due to bad expression pattern
re = new RegExp(this.config.expressionAsString,
(this.config.ignoreCase ? 'i' : '') +
(this.config.multiline ? 'm' : ''));
}
else
throw new CodingError('RegExpConditionConfig does not have a regular expression assigned to expression or ExpressionOrString properties.');
}
this._savedRE = re;
}
return this._savedRE;
}
protected evaluateString(text: string, valueHost: IValueHost, services: IValidationServices): ConditionEvaluateResult {
let found = this.getRegExp(services).test(text);
return found ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link RangeCondition}
*/
export interface RangeConditionConfig extends OneValueConditionBaseConfig, SupportsDataTypeConverter {
/**
* Native data type representing the minimum of the range.
* When undefined or null, no minimum, like LessThanOrEqualConditon.
*/
minimum: any;
/**
* Native data type representing the maximum of the range.
* When undefined or null, no maximum, like GreaterThanOrEqualConditon.
*/
maximum: any;
}
/**
* Compare the native datatype value against two other values to ensure
* it is with the range established. The minimum and maximum are included
* in the range.
*
* Supports these tokens: {Minimum} and {Maximum}
*
* When data types differ or don't support GreaterThan/LessThan evaluate as Undetermined.
* Supports Config.conversionLookupKey, but its only applied to the incoming value, not Min/Max.
*/
export class RangeCondition extends OneValueConditionBase<RangeConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.Range; }
public evaluate(valueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
valueHost = this.ensurePrimaryValueHost(valueHost, valueHostsManager);
let value = valueHost.getValue();
if (value == null) // includes undefined
{
this.logNothingToEvaluate('value', valueHostsManager.services);
return ConditionEvaluateResult.Undetermined;
}
let services = valueHostsManager.services;
// let lookupKey = this.config.conversionLookupKey ?? valueHost.getDataType();
let valueInfo = this.tryConversion(value, valueHost.getDataType(), this.config.conversionLookupKey, services);
if (valueInfo.failed)
return ConditionEvaluateResult.Undetermined;
let lower = this.config.minimum != null ? // null/undefined
services.dataTypeComparerService.compare(this.config.minimum, valueInfo.value,
null, valueInfo.lookupKey ?? null) :
ComparersResult.Equal; // always valid
if (lower === ComparersResult.Undetermined) {
this.logTypeMismatch(services, 'value', 'minimum', valueInfo.value, this.config.minimum ?? '');
return ConditionEvaluateResult.Undetermined;
}
let upper = this.config.maximum != null ? // null/undefined
services.dataTypeComparerService.compare(this.config.maximum, valueInfo.value,
null, valueInfo.lookupKey ?? null) :
ComparersResult.Equal; // always value
if (upper === ComparersResult.Undetermined) {
this.logTypeMismatch(valueHostsManager.services, 'value', 'maximum', valueInfo.value, this.config.maximum ?? '');
return ConditionEvaluateResult.Undetermined;
}
if (lower === ComparersResult.Equal || lower === ComparersResult.LessThan)
if (upper === ComparersResult.Equal || upper === ComparersResult.GreaterThan)
return ConditionEvaluateResult.Match;
return ConditionEvaluateResult.NoMatch;
}
public override getValuesForTokens(valueHost: IValidatorsValueHostBase, valueHostsManager: IValueHostsManager): Array<TokenLabelAndValue> {
let list: Array<TokenLabelAndValue> = [];
list = list.concat(super.getValuesForTokens(valueHost, valueHostsManager));
// same order of precidence as in Evaluate
list.push({
tokenLabel: 'Minimum',
associatedValue: this.config.minimum ?? null,
purpose: 'parameter'
});
list.push({
tokenLabel: 'Maximum',
associatedValue: this.config.maximum ?? null,
purpose: 'parameter'
});
return list;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.Comparison;
}
}
//#region CompareToSecondValueHost conditions
/**
* ConditionConfig for {@link EqualToCondition}
*/
export interface EqualToConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Two values must be equal. Both values are retrieved from ValueHosts.
*/
export class EqualToCondition extends CompareToSecondValueHostConditionBase<EqualToConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.EqualTo; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
return comparison === ComparersResult.Equal ?
ConditionEvaluateResult.Match :
ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link NotEqualToCondition}
*/
export interface NotEqualToConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Two values must not be equal. Both values are retrieved from ValueHosts.
*/
export class NotEqualToCondition extends CompareToSecondValueHostConditionBase<NotEqualToConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.NotEqualTo; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
return comparison !== ComparersResult.Equal ?
ConditionEvaluateResult.Match :
ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link GreaterThanCondition}
*/
export interface GreaterThanConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Value 1 must be greater than Value 2. Both values are retrieved from ValueHosts.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined.
*/
export class GreaterThanCondition extends CompareToSecondValueHostConditionBase<GreaterThanConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.GreaterThan; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.GreaterThan:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link LessThanCondition}
*/
export interface LessThanConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Value 1 must be less than Value 2. Both values are retrieved from ValueHosts.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined.
*/
export class LessThanCondition extends CompareToSecondValueHostConditionBase<LessThanConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.LessThan; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.LessThan:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link GreaterThanOrEqualCondition}
*/
export interface GreaterThanOrEqualConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Value 1 must be greater than or equal Value 2. Both values are retrieved from ValueHosts.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class GreaterThanOrEqualCondition extends CompareToSecondValueHostConditionBase<GreaterThanOrEqualConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.GreaterThanOrEqual; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.GreaterThan:
case ComparersResult.Equal:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link LessThanOrEqualCondition}
*/
export interface LessThanOrEqualConditionConfig extends CompareToSecondValueHostConditionBaseConfig { }
/**
* Value 1 must be less than or equal Value 2. Both values are retrieved from ValueHosts.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class LessThanOrEqualCondition extends CompareToSecondValueHostConditionBase<LessThanOrEqualConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.LessThanOrEqual; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.LessThan:
case ComparersResult.Equal:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
//#endregion CompareToSecondValueHost
//#region CompareToSecondValue condition
/**
* ConditionConfig for {@link EqualToValueCondition}
*/
export interface EqualToValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must be equal to a second value, assigned in its ConditionConfig.secondValue.
*/
export class EqualToValueCondition extends CompareToValueConditionBase<EqualToValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.EqualToValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
return comparison === ComparersResult.Equal ?
ConditionEvaluateResult.Match :
ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link NotEqualToValueCondition}
*/
export interface NotEqualToValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must not be equal to a second value, assigned in its ConditionConfig.secondValue.
*/
export class NotEqualToValueCondition extends CompareToValueConditionBase<NotEqualToValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.NotEqualToValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
return comparison !== ComparersResult.Equal ?
ConditionEvaluateResult.Match :
ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link GreaterThanValueCondition}
*/
export interface GreaterThanValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must be greater than a second value, assigned in its ConditionConfig.secondValue.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class GreaterThanValueCondition extends CompareToValueConditionBase<GreaterThanValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.GreaterThanValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.GreaterThan:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link LessThanValueCondition}
*/
export interface LessThanValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must be less than a second value, assigned in its ConditionConfig.secondValue.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class LessThanValueCondition extends CompareToValueConditionBase<LessThanValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.LessThanValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.LessThan:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link GreaterThanOrEqualValueCondition}
*/
export interface GreaterThanOrEqualValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must be greater than or equal to a second value, assigned in its ConditionConfig.secondValue.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class GreaterThanOrEqualValueCondition extends CompareToValueConditionBase<GreaterThanOrEqualValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.GreaterThanOrEqualValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.GreaterThan:
case ComparersResult.Equal:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
/**
* ConditionConfig for {@link LessThanOrEqualValueCondition}
*/
export interface LessThanOrEqualValueConditionConfig extends CompareToValueConditionBaseConfig { }
/**
* Value from ValueHost must be less than or equal to a second value, assigned in its ConditionConfig.secondValue.
*
* Evaluates data types that do not support GreaterThan/LessThan as Undetermined
*/
export class LessThanOrEqualValueCondition extends CompareToValueConditionBase<LessThanOrEqualValueConditionConfig> {
public static get DefaultConditionType(): ConditionType { return ConditionType.LessThanOrEqualValue; }
protected compareTwoValues(comparison: ComparersResult): ConditionEvaluateResult {
switch (comparison) {
case ComparersResult.LessThan:
case ComparersResult.Equal:
return ConditionEvaluateResult.Match;
case ComparersResult.NotEqual:
return ConditionEvaluateResult.Undetermined;
default:
return ConditionEvaluateResult.NoMatch;
}
}
}
//#endregion CompareToSecondValue condition
/**
* ConditionConfig for {@link StringLengthCondition}
*/
export interface StringLengthConditionConfig extends StringConditionBaseConfig {
/**
* Native data type representing the minimum of the range.
* When undefined or null, no minimum, like LessThanOrEqualConditon.
*/
minimum?: number | null;
/**
* Native data type representing the maximum of the range.
* When undefined or null, no maximum, like GreaterThanOrEqualConditon.
*/
maximum?: number | null;
}
/**
* Evaluates the length of a string in characters (after trimming if Trim is true).
*
* Compares the result to non-null Minimum and/or Maximum parameters.
*
* Supports these tokens: {Length}, {Minimum} and {Maximum}
*
* Supports validateOptions.duringEdit = true so long as Config.supportsDuringEdit
* is true or undefined. In that case, it respects the Config.trim property.
*/
export class StringLengthCondition extends StringConditionBase<StringLengthConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.StringLength; }
protected evaluateString(text: string, valueHost: IValueHost, services: IValidationServices): ConditionEvaluateResult {
let len = text.length; // already trimmed
return this.evaluateLength(len, valueHost);
}
private evaluateLength(len: number, valueHost: IValueHost): ConditionEvaluateResult
{
valueHost.saveIntoInstanceState('Len', len);
if (this.config.minimum != null) // null/undefined
if (len < this.config.minimum)
return ConditionEvaluateResult.NoMatch;
if (this.config.maximum != null) // null/undefined
if (len > this.config.maximum)
return ConditionEvaluateResult.NoMatch;
return ConditionEvaluateResult.Match;
}
public override getValuesForTokens(valueHost: IValidatorsValueHostBase, valueHostsManager: IValueHostsManager): Array<TokenLabelAndValue> {
let list: Array<TokenLabelAndValue> = [];
list = list.concat(super.getValuesForTokens(valueHost, valueHostsManager));
// same order of precidence as in Evaluate
list.push({
tokenLabel: 'Length',
associatedValue: valueHost.getFromInstanceState('Len') ?? 0,
purpose: 'parameter'
});
list.push({
tokenLabel: 'Minimum',
associatedValue: this.config.minimum ?? null,
purpose: 'parameter'
});
list.push({
tokenLabel: 'Maximum',
associatedValue: this.config.maximum ?? null,
purpose: 'parameter'
});
return list;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.Comparison;
}
}
/**
* ConditionConfig for {@link AllMatchCondition}
*/
export interface AllMatchConditionConfig extends EvaluateChildConditionResultsBaseConfig
{
}
/**
* All Children must evaluate as Match for a result of Match.
*
* If any are still Undetermined after treatUndeterminedAs is applied, this results as Undetermined.
*
* Any child that does not specify its Config.valueHostName will use the ValueHost passed into evaluate()
*/
export class AllMatchCondition extends EvaluateChildConditionResultsBase<AllMatchConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.And; }
protected evaluateChildren(conditions: ICondition[], parentValueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult {
for (let condition of conditions)
switch (this.cleanupChildResult(condition.evaluate(parentValueHost, valueHostsManager))) {
case ConditionEvaluateResult.NoMatch:
return ConditionEvaluateResult.NoMatch;
case ConditionEvaluateResult.Undetermined:
return ConditionEvaluateResult.Undetermined;
}
return ConditionEvaluateResult.Match;
}
}
/**
* ConditionConfig for {@link AnyMatchCondition}
*/
export interface AnyMatchConditionConfig extends EvaluateChildConditionResultsBaseConfig
{
}
/**
* At least one Child Condition must evaluate as Match for a result of Match.
*
* If any are still Undetermined after treatUndeterminedAs is applied, this results as Undetermined.
*
* Any child that does not specify its Config.valueHostName will use the ValueHost passed into evaluate()
*/
export class AnyMatchCondition extends EvaluateChildConditionResultsBase<AnyMatchConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.Or; }
protected evaluateChildren(conditions: ICondition[], parentValueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult {
let countMatches = 0;
for (let condition of conditions)
switch (this.cleanupChildResult(condition.evaluate(parentValueHost, valueHostsManager))) {
case ConditionEvaluateResult.Match:
countMatches++;
break;
case ConditionEvaluateResult.Undetermined:
return ConditionEvaluateResult.Undetermined;
}
return countMatches > 0 ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
}
/**
* ConditionConfig for {@link CountMatchesCondition}
*/
export interface CountMatchesConditionConfig extends EvaluateChildConditionResultsBaseConfig {
/**
* Must have at least this many matches. 0 or higher.
* When undefined, the Minimum is 1.
* 0 is supported, allowing for there to be 0 matches. However,
* that is a special case. Its more likely the user wants to count
* at least 1.
*/
minimum?: number;
/**
* Must have no more than this many matches.
* When undefined, there is no Maximum.
*/
maximum?: number;
}
/**
* Counts the number of child Conditions that evaluate as Match and determines if that count
* is within a range of Config.minimum to Config.maximum.
* When Minimum isn't supplied, it defaults to 1.
*
* Any child that does not specify its Config.valueHostName will use the ValueHost passed into evaluate()
*/
export class CountMatchesCondition extends EvaluateChildConditionResultsBase<CountMatchesConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.CountMatches; }
protected evaluateChildren(conditions: ICondition[], parentValueHost: IValueHost | null, valueHostsManager: IValueHostsManager): ConditionEvaluateResult {
let countMatches = 0;
for (let condition of conditions)
switch (this.cleanupChildResult(condition.evaluate(parentValueHost, valueHostsManager))) {
case ConditionEvaluateResult.Match:
countMatches++;
break;
case ConditionEvaluateResult.Undetermined:
return ConditionEvaluateResult.Undetermined;
}
let minimum = this.config.minimum ?? 1;
if (minimum !== undefined && countMatches < minimum)
return ConditionEvaluateResult.NoMatch;
if (this.config.maximum !== undefined && countMatches > this.config.maximum)
return ConditionEvaluateResult.NoMatch;
return ConditionEvaluateResult.Match;
}
}
/**
* ConditionConfig for {@link PositiveCondition}
*/
export interface PositiveConditionConfig extends NumberConditionBaseConfig
{
}
/**
* Evaluates a number to confirm it is a value of 0 or higher.
* The value can be from an object that has a DataTypeConverter to make it into a number.
* Defaults to a DataTypeCheck ConditionCategory.
*/
export class PositiveCondition extends NumberConditionBase<PositiveConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.Positive; }
protected evaluateNumber(value: number, valueHost: IValueHost, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
return value >= 0 ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.DataTypeCheck;
}
}
/**
* ConditionConfig for {@link IntegerCondition}
*/
export interface IntegerConditionConfig extends NumberConditionBaseConfig
{
}
/**
* Evaluates a number to confirm it is a value is an integer.
* The value can be from an object that has a DataTypeConverter to make it into a number.
* Defaults to a DataTypeCheck ConditionCategory.
*/
export class IntegerCondition extends NumberConditionBase<IntegerConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.Integer; }
protected evaluateNumber(value: number, valueHost: IValueHost, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
return value === Math.trunc(value) ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.DataTypeCheck;
}
}
/**
* ConditionConfig for {@link MaxDecimalsCondition}
*/
export interface MaxDecimalsConditionConfig extends NumberConditionBaseConfig
{
/**
* Maximum number of decimal places allowed.
* Requires a value.
*/
maxDecimals: number;
}
/**
* Evaluates a number to confirm it is a value of 0 or higher.
* The value can be from an object that has a DataTypeConverter to make it into a number.
* Defaults to a DataTypeCheck ConditionCategory.
*/
export class MaxDecimalsCondition extends NumberConditionBase<MaxDecimalsConditionConfig>
{
public static get DefaultConditionType(): ConditionType { return ConditionType.MaxDecimals; }
constructor(config: MaxDecimalsConditionConfig)
{
super(config);
assertNotNull(config.maxDecimals, 'maxDecimals');
if (config.maxDecimals < 1)
throw new CodingError('maxDecimals must be 1 or higher');
}
protected evaluateNumber(value: number, valueHost: IValueHost, valueHostsManager: IValueHostsManager): ConditionEvaluateResult | Promise<ConditionEvaluateResult> {
let poweredValue = value * Math.pow(10, this.config.maxDecimals);
return poweredValue === Math.floor(poweredValue) ? ConditionEvaluateResult.Match : ConditionEvaluateResult.NoMatch;
}
protected get defaultCategory(): ConditionCategory {
return ConditionCategory.DataTypeCheck;
}
}