Chirrup is a value validator for Swift.
Available validation rules(or types)
IsTrue-- check if value is TrueGreater(than: String)-- check if value is strictly greater thanLower(than: String)-- check if value is strictly lower thanNonEmpty-- check if value is not empty, uses Swift'sisEmptyBetween(from: String, to: String)-- check if value is between two other values. UsesGreaterandLowerrules in its implementationContains(value: String)-- check if string contains the value. Case insensitiveIsNumeric-- check if value is numeric one. Tries to convert toDouble?
First create the Chirrup instance
let chirrup = Chirrup()Then the following methods are available for use:
public func validate(fieldName: String, value: String, with rules: [ValidationRule],
_ callback: validationCallback? = nil) -> [ValidationRule]public func validate(fieldName: String, value: String, with rule: ValidationRule,
_ callback: validationCallbackSingle? = nil) -> ValidationRule?fieldName: Stringis passed tocallbackif such is provided as the last argumentvalueto be validatedrulesvalidation types to validate value with- and optional
callbackwhich runs no matter what if passed as an argument. It gets theerrorsarray and the field name(fieldbelow in Examples section) or just one error - it depends on callback type:
public typealias validationCallback = (errors: [ValidationRule], fieldName: String) -> ()
public typealias validationCallbackSingle = (error: ValidationRule?, fieldName: String) -> ()chirrup.validate("Search field", value: sender.text!,
with: [ValidationRule(.NonEmpty),
ValidationRule(.Contains(value: "UberCar"))]) { errors, field in
let errorMessages = self.chirrup.formatMessagesFor(field, from: errors)
// errorMessages == "Search field should not be empty\nSearch field should contain `UberCar`"
self.errorLabelSearchField.text = errorMessages
}or instead of closure callback one can use errors returned by validate method
let errors = chirrup.validate("Search field", value: sender.text!,
with: [ValidationRule(.NonEmpty),
ValidationRule(.Contains(value: "UberCar"))])
expect(chirrup.formatMessagesFor(fieldName, from: errors))
.to(equal("Search field should not be empty\nSearch field should contain `UberCar`"))or call validate with single validation rule(one can use callback closure here as well)
let error = chirrup.validate("Activator", value: false,
with: ValidationRule(.IsTrue))
expect(error!.errorMessageFor(fieldName))
.to(equal("Activator should be true"))errors and error returned above are validation rules themselves and could be initialized with several options and have following constructor
public init(_ type: ValidationRuleType,
message: String? = nil, on: (() -> Bool)? = nil)where
typeis one of rules described abovemessage:String?is optional and represents full error message, e.g. "This field should include 'hey there!' "onis optional too and you can pass some code which returnsBool. In case it returnstruethe validation rule is evaluated otherwise skipped
let val = ""
let error = chirrup.validate(fieldName, value: val,
with: ValidationRule(.Lower(than: "10000.00"), on: { !val.isEmpty }))Above validation is skipped.
Add Chirrup to your Cartfile:
github "bilogub/Chirrup"
