File: DBStoneValidators.m

package info (click to toggle)
python-stone 3.3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,956 kB
  • sloc: python: 21,786; objc: 498; sh: 29; makefile: 11
file content (145 lines) | stat: -rw-r--r-- 4,884 bytes parent folder | download | duplicates (2)
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
///
/// Copyright (c) 2016 Dropbox, Inc. All rights reserved.
///

#import "DBStoneValidators.h"

@implementation DBStoneValidators

+ (void (^)(NSString *))stringValidator:(NSNumber *)minLength
                              maxLength:(NSNumber *)maxLength
                                pattern:(NSString *)pattern
{

    void (^validator)(NSString *) = ^(NSString *value) {
      if (minLength != nil) {
          if ([value length] < [minLength unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at least %@ characters", [minLength stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }

      if (maxLength != nil) {
          if ([value length] > [maxLength unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at most %@ characters", [minLength stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }

      if (pattern != nil && pattern.length != 0) {
          NSError *error;
          NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
          NSArray *matches = [re matchesInString:value options:0 range:NSMakeRange(0, [value length])];
          if ([matches count] == 0) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must match pattern \"%@\"", [re pattern]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }
    };

    return validator;
}

+ (void (^)(NSNumber *))numericValidator:(NSNumber *)minValue maxValue:(NSNumber *)maxValue
{
    void (^validator)(NSNumber *) = ^(NSNumber *value) {
      if (minValue != nil) {
          if ([value unsignedIntegerValue] < [minValue unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at least %@", [minValue stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }

      if (maxValue != nil) {
          if ([value unsignedIntegerValue] > [maxValue unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at most %@", [maxValue stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }
    };

    return validator;
}

+ (void (^)(NSArray<id> *))arrayValidator:(NSNumber *)minItems
                                 maxItems:(NSNumber *)maxItems
                            itemValidator:(void (^)(id))itemValidator
{
    void (^validator)(NSArray<id> *) = ^(NSArray<id> *value) {
      if (minItems != nil) {
          if ([value count] < [minItems unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at least %@ items", [minItems stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }

      if (maxItems != nil) {
          if ([value count] > [maxItems unsignedIntegerValue]) {
              NSString *exceptionMessage =
                  [NSString stringWithFormat:@"value must be at most %@ items", [maxItems stringValue]];
              [[self class] raiseIllegalStateErrorWithMessage:exceptionMessage];
          }
      }

      if (itemValidator != nil) {
          for (id item in value) {
              itemValidator(item);
          }
      }
    };

    return validator;
}

+ (void (^)(NSDictionary<NSString *, id> *))mapValidator:(void (^)(id))itemValidator
{
    void (^validator)(NSDictionary<NSString *, id> *) = ^(NSDictionary<NSString *, id> *value) {
      if (itemValidator != nil) {
          for (id key in value) {
              itemValidator(value[key]);
          }
      }
    };

    return validator;
}

+ (void (^)(id))nullableValidator:(void (^)(id))internalValidator
{
    void (^validator)(id) = ^(id value) {
      if (value != nil) {
          internalValidator(value);
      }
    };

    return validator;
}

+ (void (^)(id))nonnullValidator:(void (^)(id))internalValidator
{
    void (^validator)(id) = ^(id value) {
      if (value == nil) {
          [[self class] raiseIllegalStateErrorWithMessage:@"Value must not be `nil`"];
      }

      if (internalValidator != nil) {
          internalValidator(value);
      }
    };

    return validator;
}

+ (void)raiseIllegalStateErrorWithMessage:(NSString *)message
{
    NSString *exceptionMessage =
        [NSString stringWithFormat:@"%@:\n%@", message, [[NSThread callStackSymbols] objectAtIndex:0]];
    [NSException raise:@"IllegalStateException" format:exceptionMessage, nil];
}
@end