Swiftpack.co - m-housh/AmericanStateKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by m-housh.
m-housh/AmericanStateKit 0.1.7
Utilities for validating and working with American states and state abbreviations
⭐️ 0
🕓 3 years ago
.package(url: "https://github.com/m-housh/AmericanStateKit.git", from: "0.1.7")


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.




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

Using as a validator.

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)

// False

model.state = "foo"
// False

model.state = "oh"
// True


Stars: 0
Last commit: 3 years ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.


Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics