@@ -24,40 +24,69 @@ func format(context: Context, format: Any, instance: Any, schema: [String: Any])
24
24
}
25
25
26
26
27
- func validateIPv4( _ context: Context , _ value: Any ) -> AnySequence < ValidationError > {
28
- if let ipv4 = value as? String {
29
- if let expression = try ? NSRegularExpression ( pattern: " ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ " , options: NSRegularExpression . Options ( rawValue: 0 ) ) {
30
- if expression. matches ( in: ipv4, options: NSRegularExpression . MatchingOptions ( rawValue: 0 ) , range: NSMakeRange ( 0 , ipv4. count) ) . count == 1 {
31
- return AnySequence ( EmptyCollection ( ) )
32
- }
27
+ func validateIPv4( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
28
+ if let expression = try ? NSRegularExpression ( pattern: " ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ " , options: NSRegularExpression . Options ( rawValue: 0 ) ) {
29
+ if expression. matches ( in: value, options: NSRegularExpression . MatchingOptions ( rawValue: 0 ) , range: NSMakeRange ( 0 , value. utf16. count) ) . count == 1 {
30
+ return AnySequence ( EmptyCollection ( ) )
33
31
}
32
+ }
34
33
35
- return AnySequence ( [
36
- ValidationError (
37
- " ' \( ipv4) ' is not a IPv4 address. " ,
38
- instanceLocation: context. instanceLocation,
39
- keywordLocation: context. keywordLocation
40
- )
41
- ] )
34
+ return AnySequence ( [
35
+ ValidationError (
36
+ " ' \( value) ' is not a IPv4 address. " ,
37
+ instanceLocation: context. instanceLocation,
38
+ keywordLocation: context. keywordLocation
39
+ )
40
+ ] )
41
+ }
42
+
43
+
44
+ func validateIPv6( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
45
+ if !value. contains ( " % " ) {
46
+ var buf = UnsafeMutablePointer< Int8> . allocate( capacity: Int ( INET6_ADDRSTRLEN) )
47
+ if inet_pton ( AF_INET6, value, & buf) == 1 {
48
+ return AnySequence ( EmptyCollection ( ) )
49
+ }
42
50
}
43
51
44
- return AnySequence ( EmptyCollection ( ) )
52
+ return AnySequence ( [
53
+ ValidationError (
54
+ " ' \( value) ' is not a value address. " ,
55
+ instanceLocation: context. instanceLocation,
56
+ keywordLocation: context. keywordLocation
57
+ )
58
+ ] )
45
59
}
46
60
47
61
62
+ func validateURI( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
63
+ // Using the regex from http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris
48
64
49
- func validateIPv6 ( _ context : Context , _ value : Any ) -> AnySequence < ValidationError > {
50
- if let ipv6 = value as? String {
51
- if !ipv6 . contains ( " % " ) {
52
- var buf = UnsafeMutablePointer < Int8 > . allocate ( capacity : Int ( INET6_ADDRSTRLEN ) )
53
- if inet_pton ( AF_INET6 , ipv6 , & buf ) == 1 {
65
+ if let expression = try ? NSRegularExpression ( pattern : " ((?<= \\ ()[A-Za-z][A-Za-z0-9 \\ + \\ . \\ -]*:([A-Za-z0-9 \\ . \\ -_~:/ \\ ?# \\ [ \\ ]@! \\ $&' \\ ( \\ ) \\ * \\ +,;=]|%[A-Fa-f0-9]{2})+(?= \\ )))|([A-Za-z][A-Za-z0-9 \\ + \\ . \\ -]*:([A-Za-z0-9 \\ . \\ -_~:/ \\ ?# \\ [ \\ ]@! \\ $&' \\ ( \\ ) \\ * \\ +,;=]|%[A-Fa-f0-9]{2})+) " , options : NSRegularExpression . Options ( rawValue : 0 ) ) {
66
+ let result = expression . matches ( in : value, options : NSRegularExpression . MatchingOptions ( rawValue : 0 ) , range : NSMakeRange ( 0 , value . utf16 . count ) )
67
+ if result . count == 1 {
68
+ let foundRange = result [ 0 ] . range
69
+ if foundRange . location == 0 && foundRange . length == value . utf16 . count {
54
70
return AnySequence ( EmptyCollection ( ) )
55
71
}
56
72
}
73
+ }
74
+
75
+ return AnySequence ( [
76
+ ValidationError (
77
+ " ' \( value) ' is not a valid uri. " ,
78
+ instanceLocation: context. instanceLocation,
79
+ keywordLocation: context. keywordLocation
80
+ )
81
+ ] )
82
+ }
83
+
57
84
85
+ func validateUUID( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
86
+ if UUID ( uuidString: value) == nil {
58
87
return AnySequence ( [
59
88
ValidationError (
60
- " ' \( ipv6 ) ' is not a IPv6 address . " ,
89
+ " ' \( value ) ' is not a valid uuid . " ,
61
90
instanceLocation: context. instanceLocation,
62
91
keywordLocation: context. keywordLocation
63
92
)
@@ -68,23 +97,13 @@ func validateIPv6(_ context: Context, _ value: Any) -> AnySequence<ValidationErr
68
97
}
69
98
70
99
71
- func validateURI( _ context: Context , _ value: Any ) -> AnySequence < ValidationError > {
72
- if let uri = value as? String {
73
- // Using the regex from http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris
74
-
75
- if let expression = try ? NSRegularExpression ( pattern: " ((?<= \\ ()[A-Za-z][A-Za-z0-9 \\ + \\ . \\ -]*:([A-Za-z0-9 \\ . \\ -_~:/ \\ ?# \\ [ \\ ]@! \\ $&' \\ ( \\ ) \\ * \\ +,;=]|%[A-Fa-f0-9]{2})+(?= \\ )))|([A-Za-z][A-Za-z0-9 \\ + \\ . \\ -]*:([A-Za-z0-9 \\ . \\ -_~:/ \\ ?# \\ [ \\ ]@! \\ $&' \\ ( \\ ) \\ * \\ +,;=]|%[A-Fa-f0-9]{2})+) " , options: NSRegularExpression . Options ( rawValue: 0 ) ) {
76
- let result = expression. matches ( in: uri, options: NSRegularExpression . MatchingOptions ( rawValue: 0 ) , range: NSMakeRange ( 0 , uri. count) )
77
- if result. count == 1 {
78
- let foundRange = result [ 0 ] . range
79
- if foundRange. location == 0 && foundRange. length == uri. count {
80
- return AnySequence ( EmptyCollection ( ) )
81
- }
82
- }
83
- }
84
-
100
+ func validateRegex( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
101
+ do {
102
+ _ = try NSRegularExpression ( pattern: value)
103
+ } catch {
85
104
return AnySequence ( [
86
105
ValidationError (
87
- " ' \( uri ) ' is not a valid uri . " ,
106
+ " ' \( value ) ' is not a valid regex . " ,
88
107
instanceLocation: context. instanceLocation,
89
108
keywordLocation: context. keywordLocation
90
109
)
@@ -95,68 +114,34 @@ func validateURI(_ context: Context, _ value: Any) -> AnySequence<ValidationErro
95
114
}
96
115
97
116
98
- func validateUUID( _ context: Context , _ value: Any ) -> AnySequence < ValidationError > {
99
- if let value = value as? String {
100
- if UUID ( uuidString: value) == nil {
101
- return AnySequence ( [
102
- ValidationError (
103
- " ' \( value) ' is not a valid uuid. " ,
104
- instanceLocation: context. instanceLocation,
105
- keywordLocation: context. keywordLocation
106
- )
107
- ] )
108
- }
117
+ func validateJSONPointer( _ context: Context , _ value: String ) -> AnySequence < ValidationError > {
118
+ guard !value. isEmpty else {
119
+ return AnySequence ( EmptyCollection ( ) )
109
120
}
110
121
111
- return AnySequence ( EmptyCollection ( ) )
112
- }
113
-
114
-
115
- func validateRegex( _ context: Context , _ value: Any ) -> AnySequence < ValidationError > {
116
- if let value = value as? String {
117
- do {
118
- _ = try NSRegularExpression ( pattern: value)
119
- } catch {
120
- return AnySequence ( [
121
- ValidationError (
122
- " ' \( value) ' is not a valid regex. " ,
123
- instanceLocation: context. instanceLocation,
124
- keywordLocation: context. keywordLocation
125
- )
126
- ] )
127
- }
122
+ if !value. hasPrefix ( " / " ) {
123
+ return AnySequence ( [
124
+ ValidationError (
125
+ " ' \( value) ' is not a valid json-pointer. " ,
126
+ instanceLocation: context. instanceLocation,
127
+ keywordLocation: context. keywordLocation
128
+ )
129
+ ] )
128
130
}
129
131
130
- return AnySequence ( EmptyCollection ( ) )
131
- }
132
-
133
-
134
- func validateJSONPointer( _ context: Context , _ value: Any ) -> AnySequence < ValidationError > {
135
- if let value = value as? String , !value. isEmpty {
136
- if !value. hasPrefix ( " / " ) {
137
- return AnySequence ( [
138
- ValidationError (
139
- " ' \( value) ' is not a valid json-pointer. " ,
140
- instanceLocation: context. instanceLocation,
141
- keywordLocation: context. keywordLocation
142
- )
143
- ] )
144
- }
145
-
146
- if value
147
- . replacingOccurrences ( of: " ~0 " , with: " " )
148
- . replacingOccurrences ( of: " ~1 " , with: " " )
149
- . contains ( " ~ " )
150
- {
151
- // unescaped ~
152
- return AnySequence ( [
153
- ValidationError (
154
- " ' \( value) ' is not a valid json-pointer. " ,
155
- instanceLocation: context. instanceLocation,
156
- keywordLocation: context. keywordLocation
157
- )
158
- ] )
159
- }
132
+ if value
133
+ . replacingOccurrences ( of: " ~0 " , with: " " )
134
+ . replacingOccurrences ( of: " ~1 " , with: " " )
135
+ . contains ( " ~ " )
136
+ {
137
+ // unescaped ~
138
+ return AnySequence ( [
139
+ ValidationError (
140
+ " ' \( value) ' is not a valid json-pointer. " ,
141
+ instanceLocation: context. instanceLocation,
142
+ keywordLocation: context. keywordLocation
143
+ )
144
+ ] )
160
145
}
161
146
162
147
return AnySequence ( EmptyCollection ( ) )
0 commit comments