Skip to content

Commit

Permalink
[format] Support IPv4 Validation #3
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Mar 8, 2015
1 parent 1196163 commit b8b3cbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion JSONSchema/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct Schema {
self.schema = schema

formats = [
:
"ipv4": validateIPv4,
]
}

Expand Down
16 changes: 16 additions & 0 deletions JSONSchema/Validators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,19 @@ func validateDependencies(key:String, dependencies:[String])(value:AnyObject) ->

return true
}

// MARK: Format

func validateIPv4(value:AnyObject) -> ValidationResult {
if let ipv4 = value as? String {
if let expression = 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: NSRegularExpressionOptions(0), error: nil) {
if expression.matchesInString(ipv4, options: NSMatchingOptions(0), range: NSMakeRange(0, ipv4.utf16Count)).count == 1 {
return .Valid
}
}

return .Invalid(["'\(ipv4)' is not valid IPv4 address."])
}

return .Valid
}

0 comments on commit b8b3cbd

Please sign in to comment.