By Danilo Topalovic.
When I wrote SwiftValidate I encountered some problems in form validation. A bunch of field values weren't really errorneous but needed to be changed in some way.
Of course a validator should not change the value in any way so I decided to do another project with filtering.
Similar to Zend\Filter this also works with a filter chain which applies a bunch of filters to a single value. I Use SwiftFilter along with SwiftValidate together with Eureka.
- iOS 8.0+
- Xcode 7.0+
See CocoaPods for easy installation into your project
Add SwiftFilter
to your Podfile
platform :ios, '8.0'
use_frameworks!
pod 'SwiftValidate'
See Carthage
Add ``SwiftFilterto your
Cartfile`
github "dtop/SwiftFilter"
let inValue: String = " \t \n Some foo String \t\t \n "
let chain = FilterChain()
<~~ TrimFilter()
<~~ ReplaceFilter {
$0.replaceType = .ReplaceString(searchStr: "foo", replacementStr: "bar")
}
do {
let outValue: String? = chain.filter(inValue, nil)
} catch _ {
}
Enables you to add a verry custom behavior to the filter chain by adding your callback which is executed on the value
Configuration
value | type | default | description |
---|---|---|---|
callback |
closure | ! | the callback you have to provide |
Parameters
input | output | explination |
---|---|---|
Any |
Any |
- |
Allows to change the char case of the given string
Configuration
value | type | default | description |
---|---|---|---|
charCase |
CharCaseFilter.Case |
! | .Lowercase, .Uppercase, .Ucfirst, .Ucwords |
Parameters
input | output | explination |
---|---|---|
String |
String |
only String is supported |
Allows to replace a String with another
Configuration
value | type | default | description |
---|---|---|---|
replaceType |
ReplaceFilter.Case |
! | @example see below |
// searches and replaces all string occurences
let filter = ReplaceFilter {
$0.replaceType = .ReplaceString(searchStr: "foo", replacementStr: "bar")
}
// replaces a given range that is seered by advancements of stringIndex
let filter = ReplaceFilter {
$0.replaceType = .ReplaceRange(advStart: 0, advEnd: 3, replacementStr: "bar")
}
Parameters
input | output | explination |
---|---|---|
String |
String |
only String is supported |
Trims whitespace around the given String
Configuration
none
Parameters
input | output | explination |
---|---|---|
String |
String |
only String is supported |
Allows the replacement of a substring by regex pattern matching
Configuration
value | type | default | description |
---|---|---|---|
pattern |
String | ! | the pattern to match against |
options |
NSRegularExpressionOptions | 0 | options for the regular expression |
matchingOptions |
NSMatchingOptions | 0 | options for the matching |
replacement |
String | ! | the replacement |
Parameters
input | output | explination |
---|---|---|
String |
String |
only String is supported |
Adding own filters that are working with the chain is quite easy
class MyFilter: FilterProtocol {
var mySetting: String!
required init(@noescape _ initialize: (RegexReplaceFilter) -> Void = { _ in }) {
initialize(self)
}
func filter<I: Any, O: Any>(value: I?, _ context: [String: AnyObject]?) throws -> O? {
// your filtering
}
}
// can be used:
let chain = FilterChain()
<~~ MyFilter {
$0.mySetting = "foo"
}