A utility for validating and working with American states and state abbreviations.
You can access arrays of the states, state names, and state abbreviations through the AllStates
struct.
print(AllStates.states)
print(AllStates.names)
print(AllStates.abbreviations)
You can test if a string is a state name or a state abbreviation.
guard let fl = "fl".state, let florida = "florida".state else {
throw fatalError()
}
print(fl.name == florida.name)
// True
print(fl.abbreviation == florida.abbreviation)
// True
print(fl == florida)
// False
For states with multiple words, the white spaces are ignored / stripped when initializing.
let one = AmericanState.fromString("newyork")!
let two = AmericanState.fromString("new york")!
print(one == two)
/// True
This uses the vapor/validation package. Refer to the docs for more usage examples.
import Validation
import AmericanStateKit
struct MyModel: Codable, Reflectable {
var state: String?
}
extension MyModel: Validatable {
static func validations() throws -> Validations<MyModel> {
var validations = Validations(MyModel.self)
try validations.add(\.state, !.nil && .state)
return validations
}
/// helper method for testing.
var isValid: Bool {
do {
try self.validate()
return true
} catch {
return false
}
}
}
var model = MyModel(state: nil)
print(model.isValid)
// False
model.state = "foo"
print(model.isValid)
// False
model.state = "oh"
print(model.isValid)
// True
link |
Stars: 0 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics