-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassure.go
More file actions
782 lines (730 loc) · 22.4 KB
/
assure.go
File metadata and controls
782 lines (730 loc) · 22.4 KB
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
package figtree
import (
"fmt"
"math"
"strings"
"time"
)
// AssureStringHasSuffix ensures a string ends with the given suffix.
var AssureStringHasSuffix = func(suffix string) FigValidatorFunc {
return makeStringValidator(
func(s string) bool { return strings.HasSuffix(s, suffix) },
fmt.Sprintf("string must have suffix %q, got %%q", suffix),
)
}
// AssureStringHasPrefix ensures a string starts with the given prefix.
var AssureStringHasPrefix = func(prefix string) FigValidatorFunc {
return makeStringValidator(
func(s string) bool { return strings.HasPrefix(s, prefix) },
fmt.Sprintf("string must have prefix %q, got %%q", prefix),
)
}
// AssureStringNoSuffixes ensures a string ends with a suffix
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringNoSuffixes = func(suffixes []string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
for _, suffix := range suffixes {
if strings.HasSuffix(v.ToString(), suffix) {
return fmt.Errorf("string must not have suffix substring %q, got %q", suffix, v)
}
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringNoPrefixes ensures a string begins with a prefix
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringNoPrefixes = func(prefixes []string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
for _, prefix := range prefixes {
if strings.HasPrefix(v.ToString(), prefix) {
return fmt.Errorf("string must not have prefix %q, got %q", prefix, v)
}
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringHasSuffixes ensures a string ends with all suffixes in the list provided
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringHasSuffixes = func(suffixes []string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
for _, suffix := range suffixes {
if !strings.HasSuffix(v.ToString(), suffix) {
return fmt.Errorf("string must have suffix %q, got %q", suffix, v)
}
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringHasPrefixes ensures a string begins with a prefix
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringHasPrefixes = func(prefixes []string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
for _, prefix := range prefixes {
if !strings.HasPrefix(v.ToString(), prefix) {
return fmt.Errorf("string must have prefix %q, got %q", prefix, v)
}
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringNoSuffix ensures a string ends with a suffix
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringNoSuffix = func(suffix string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if strings.HasSuffix(vs, suffix) {
return fmt.Errorf("string must not have suffix substring %q, got %q", suffix, vs)
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringNoPrefix ensures a string begins with a prefix
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringNoPrefix = func(prefix string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if strings.HasPrefix(vs, prefix) {
return fmt.Errorf("string must not have prefix substring %q, got %q", prefix, vs)
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringLengthLessThan ensures the string is less than an int
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringLengthLessThan = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if len(vs) > length {
return fmt.Errorf("string must be less than %d chars, got %d", length, len(vs))
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringLengthGreaterThan ensures the string is greater than an int
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringLengthGreaterThan = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if len(vs) < length {
return fmt.Errorf("string must be greater than %d chars, got %d", length, len(vs))
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringSubstring ensures a string contains a specific substring.
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringSubstring = func(sub string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if !strings.Contains(vs, sub) {
return fmt.Errorf("string must contain substring %q, got %q", sub, vs)
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringLength ensures a string contains a specific substring.
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringLength = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if len(vs) < length {
return fmt.Errorf("string must be at least %d chars, got %q", length, len(vs))
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringNotLength ensures a string contains a specific substring.
// Returns a figValidatorFunc that checks for the substring (case-sensitive).
var AssureStringNotLength = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if len(vs) == length {
return fmt.Errorf("string must not be %d chars, got %q", length, len(vs))
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureStringNotEmpty ensures a string is not empty.
// Returns an error if the value is an empty string or not a string.
var AssureStringNotEmpty = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
if len(v.ToString()) == 0 {
return fmt.Errorf("empty string")
}
return nil
}
return ErrInvalidType{tString, value}
}
// AssureStringContains ensures a string contains a specific substring.
// Returns an error if the substring is not found or if the value is not a string.
var AssureStringContains = func(substring string) FigValidatorFunc {
return makeStringValidator(
func(s string) bool { return strings.Contains(s, substring) },
fmt.Sprintf("string must contain %q, got %%q", substring),
)
}
// AssureStringNotContains ensures a string contains a specific substring.
// Returns an error if the substring is not found or if the value is not a string.
var AssureStringNotContains = func(substring string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsString() {
vs := v.ToString()
if strings.Contains(vs, substring) {
return fmt.Errorf("string must not contain %q, got %q", substring, vs)
}
return nil
}
return ErrInvalidType{tString, value}
}
}
// AssureBoolTrue ensures a boolean value is true.
// Returns an error if the value is false or not a bool.
var AssureBoolTrue = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsBool() {
if !v.ToBool() {
return fmt.Errorf("value must be true, got %t", v.ToBool())
}
return nil
}
return ErrInvalidType{tBool, value}
}
// AssureBoolFalse ensures a boolean value is false.
// Returns an error if the value is true or not a bool.
var AssureBoolFalse = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsBool() {
if v.ToBool() {
return fmt.Errorf("bool must be false, got %t", v.ToBool())
}
return nil
}
return ErrInvalidType{tBool, value}
}
// AssureIntPositive ensures an integer is positive.
// Returns an error if the value is zero or negative, or not an int.
var AssureIntPositive = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsInt() {
if v.ToInt() < 0 {
return ErrValue{ErrWayBePositive, v.ToInt(), 0}
}
return nil
}
return ErrInvalidType{tInt, value}
}
// AssureIntNegative ensures an integer is negative.
// Returns an error if the value is zero or positive, or not an int.
var AssureIntNegative = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsInt() {
if v.ToInt() > 0 {
return ErrValue{ErrWayBeNegative, v.ToInt(), 0}
}
return nil
}
return ErrInvalidType{tInt, value}
}
// AssureIntGreaterThan ensures an integer is greater than (but not including) an int.
// Returns an error if the value is below, or not an int.
var AssureIntGreaterThan = func(above int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt() {
return ErrInvalidType{tInt, value}
}
i := v.ToInt()
if i < above {
return ErrValue{ErrWayBeBelow, i, above}
}
return nil
}
}
// AssureIntLessThan ensures an integer is less than (but not including) an int.
// Returns an error if the value is above, or not an int.
var AssureIntLessThan = func(below int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt() {
return ErrInvalidType{tInt, value}
}
i := v.ToInt()
if i > below {
return ErrValue{ErrWayBeBelow, i, below}
}
return nil
}
}
// AssureIntInRange ensures an integer is within a specified range (inclusive).
// Returns an error if the value is outside the range or not an int.
var AssureIntInRange = func(min, max int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt() {
return ErrInvalidType{tInt, value}
}
i := v.ToInt()
if i < min || i > max {
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), i, nil}
}
return nil
}
}
// AssureInt64GreaterThan ensures an integer is greater than (but not including) an int.
// Returns an error if the value is below, or not an int.
var AssureInt64GreaterThan = func(above int64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt64() {
return ErrInvalidType{tInt64, value}
}
i := v.ToInt64()
if i < above {
return ErrValue{ErrWayBeAbove, i, above}
}
return nil
}
}
// AssureInt64LessThan ensures an integer is less than (but not including) an int.
// Returns an error if the value is above, or not an int.
var AssureInt64LessThan = func(below int64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt64() {
return ErrInvalidType{tInt64, value}
}
i := v.ToInt64()
if i > below {
return ErrValue{ErrWayBeBelow, i, below}
}
return nil
}
}
// AssureInt64Positive ensures an int64 is positive.
// Returns an error if the value is zero or negative, or not an int64.
var AssureInt64Positive = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt64() {
return ErrInvalidType{tInt64, value}
}
i := v.ToInt64()
if i <= 0 {
return ErrValue{ErrWayBePositive, i, 0}
}
return nil
}
// AssureInt64InRange ensures an int64 is within a specified range (inclusive).
// Returns a figValidatorFunc that checks the value against min and max.
var AssureInt64InRange = func(min, max int64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsInt64() {
return ErrInvalidType{tInt64, value}
}
i := v.ToInt64()
if i < min || i > max {
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), i, nil}
}
return nil
}
}
// AssureFloat64Positive ensures a float64 is positive.
// Returns an error if the value is zero or negative, or not a float64.
var AssureFloat64Positive = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsFloat64() {
return ErrInvalidType{tFloat64, value}
}
f := v.ToFloat64()
if f <= 0 {
return ErrValue{ErrWayBePositive, f, 0}
}
return nil
}
// AssureFloat64InRange ensures a float64 is within a specified range (inclusive).
// Returns an error if the value is outside the range or not a float64.
var AssureFloat64InRange = func(min, max float64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsFloat64() {
return ErrInvalidType{tFloat64, value}
}
f := v.ToFloat64()
if f < min || f > max {
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), f, nil}
}
return nil
}
}
// AssureFloat64GreaterThan ensures an integer is greater than (but not including) an int.
// Returns an error if the value is below, or not an int.
var AssureFloat64GreaterThan = func(above float64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsFloat64() {
return ErrInvalidType{tFloat64, value}
}
f := v.ToFloat64()
if f < above {
return ErrValue{ErrWayBeBelow, f, above}
}
return nil
}
}
// AssureFloat64LessThan ensures an integer is less than (but not including) an int.
// Returns an error if the value is above, or not an int.
var AssureFloat64LessThan = func(below float64) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsFloat64() {
return ErrInvalidType{tFloat64, value}
}
f := v.ToFloat64()
if f > below {
return ErrValue{ErrWayBeBelow, f, below}
}
return nil
}
}
// AssureFloat64NotNaN ensures a float64 is not NaN.
// Returns an error if the value is NaN or not a float64.
var AssureFloat64NotNaN = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsFloat64() {
return ErrInvalidType{tFloat64, value}
}
n := v.ToFloat64()
if math.IsNaN(n) {
return ErrValue{ErrWayBeNotNaN, n, nil}
}
return nil
}
// AssureDurationGreaterThan ensures a time.Duration is greater than (but not including) a time.Duration.
// Returns an error if the value is below, or not an int.
var AssureDurationGreaterThan = func(above time.Duration) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsDuration() {
return ErrInvalidType{tDuration, value}
}
t := v.ToDuration()
if t < above {
return ErrValue{ErrWayBeAbove, t, above}
}
return nil
}
}
// AssureDurationLessThan ensures a time.Duration is less than (but not including) a time.Duration.
// Returns an error if the value is below, or not an int.
var AssureDurationLessThan = func(below time.Duration) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsDuration() {
return ErrInvalidType{tDuration, value}
}
t := v.ToDuration()
if t > below {
return ErrValue{ErrWayBeBelow, t, below}
}
return nil
}
}
// AssureDurationPositive ensures a time.Duration is positive.
// Returns an error if the value is zero or negative, or not a time.Duration.
var AssureDurationPositive = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsDuration() {
return ErrInvalidType{tDuration, value}
}
d := v.ToDuration()
if d <= 0 {
return fmt.Errorf("duration must be positive, got %s", d)
}
return nil
}
// AssureDurationMin ensures a time.Duration is at least a minimum value.
// Returns a figValidatorFunc that checks the duration against the minimum.
var AssureDurationMin = func(min time.Duration) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsDuration() {
return ErrInvalidType{tDuration, value}
}
d := v.ToDuration()
if d < min {
return fmt.Errorf("duration must be at least %s, got %s", min, d)
}
return nil
}
}
// AssureDurationMax ensures a time.Duration does not exceed a maximum value.
// Returns an error if the value exceeds the max or is not a time.Duration.
var AssureDurationMax = func(max time.Duration) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsDuration() {
return ErrInvalidType{tDuration, value}
}
d := v.ToDuration()
if d > max {
return fmt.Errorf("duration must not exceed %s, got %s", max, d)
}
return nil
}
}
// AssureListNotEmpty ensures a list is not empty.
// Returns an error if the list has no elements or is not a ListFlag.
var AssureListNotEmpty = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
if len(l) == 0 {
return fmt.Errorf("list is empty")
}
return nil
}
// AssureListMinLength ensures a list has at least a minimum number of elements.
// Returns an error if the list is too short or not a ListFlag.
var AssureListMinLength = func(min int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
if len(l) < min {
return fmt.Errorf("list must have at least %d elements, got %d", min, len(l))
}
return nil
}
}
// AssureListContains ensures a list contains a specific string value.
// Returns a figValidatorFunc that checks for the presence of the value.
var AssureListContains = func(inside string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
for _, item := range l {
if item == inside {
return nil
}
}
return fmt.Errorf("list must contain %q, got %v", inside, l)
}
}
// AssureListNotContains ensures a list contains a specific string value.
// Returns a figValidatorFunc that checks for the presence of the value.
var AssureListNotContains = func(not string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
for _, item := range l {
if item == not {
return fmt.Errorf("list cannot contain %s", item)
}
}
return nil
}
}
// AssureListContainsKey ensures a list contains a specific string.
// It accepts *ListFlag, *[]string, or []string and returns an error if the key string is not found
// or the type is invalid.
var AssureListContainsKey = func(key string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
for _, item := range l {
if item == key {
return nil
}
}
return fmt.Errorf("list must contain %q, got %v", key, l)
}
}
// AssureListLength ensures a list has exactly the specified length.
// It accepts *ListFlag, *[]string, or []string and returns an error if the length differs
// or the type is invalid.
var AssureListLength = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsList() {
return ErrInvalidType{tList, value}
}
l := v.ToList()
if len(l) != length {
return fmt.Errorf("list must have length %d, got %d", length, len(l))
}
return nil
}
}
// AssureMapNotEmpty ensures a map is not empty.
// Returns an error if the map has no entries or is not a MapFlag.
var AssureMapNotEmpty = func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if len(m) == 0 {
return fmt.Errorf("map is empty")
}
return nil
}
// AssureMapHasKey ensures a map contains a specific key.
// Returns an error if the key is missing or the value is not a MapFlag.
var AssureMapHasKey = func(key string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if _, exists := m[key]; !exists {
return fmt.Errorf("map must contain key %q", key)
}
return nil
}
}
// AssureMapHasNoKey ensures a map contains a specific key.
// Returns an error if the key is missing or the value is not a MapFlag.
var AssureMapHasNoKey = func(key string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if _, exists := m[key]; exists {
return fmt.Errorf("map must not contain key %q", key)
}
return nil
}
}
// AssureMapValueMatches ensures a map has a specific key with a matching value.
// Returns a figValidatorFunc that checks for the key-value pair.
var AssureMapValueMatches = func(key, match string) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if val, exists := m[key]; exists {
if val != match {
return fmt.Errorf("map value %q must have value %q, got %q", key, match, val)
}
return nil
}
return fmt.Errorf("map key %q does not exist", key)
}
}
// AssureMapHasKeys ensures a map contains all specified keys.
// Returns an error if any key is missing or the value is not a *MapFlag.
var AssureMapHasKeys = func(keys []string) FigValidatorFunc {
return func(value interface{}) error {
var missing []string
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
for _, key := range keys {
if _, exists := m[key]; !exists {
missing = append(missing, key)
}
}
if len(missing) > 0 {
return fmt.Errorf("map must contain keys %v, missing %v", keys, missing)
}
return nil
}
}
// AssureMapLength ensures a map has exactly the specified length.
// It accepts *MapFlag, *map[string]string, or map[string]string and returns an error
// if the length differs or the type is invalid.
var AssureMapLength = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if len(m) != length {
return fmt.Errorf("map must have length %d, got %d", length, len(m))
}
return nil
}
}
// AssureMapNotLength ensures a map has exactly the specified length.
// It accepts *MapFlag, *map[string]string, or map[string]string and returns an error
// if the length differs or the type is invalid.
var AssureMapNotLength = func(length int) FigValidatorFunc {
return func(value interface{}) error {
v := figFlesh{value, nil}
if !v.IsMap() {
return ErrInvalidType{tMap, value}
}
m := v.ToMap()
if len(m) == length {
return fmt.Errorf("map must not have length %d, got %d", length, len(m))
}
return nil
}
}