Swiftpack.co - Package - thecoolwinter/SwiftSWOT
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

SwiftSWOT

Platforms Swift 5.3 Vapor 4

SwiftSWOT is a Vapor service for verifying academic emails against the SWOT database.

SWOT is a community-driven database that has a large list of valid academic emails. This is useful for things like providing student discounts. This package checks against that database to give a reasonably good idea of if a given email is an academic email.

Installation

Installation is done through the Swift Package Manager

.package(url: "https://github.com/thecoolwinter/SwiftSWOT.git", from: "1.0.1")

.target(name: "App", dependencies: [
    .product(name: "Vapor", package: "vapor"),
    .product(name: "SwiftSWOT", package: "SwiftSWOT")
])

Usage

Validate Academic Emails

In your request path, validate emails using the SWOT variable on the request.

app.on(.GET, "validateStudent", ":email") { (req) -> [String: String] in
    guard let email = try? req.parameters.require("email") else {
        return [
            "valid": "false",
            "reason": "Please provide an email to validate."
        ]
    }
    
    let isValidEmail = req.SWOT.validateEmail(email)
    if isValidEmail {
        return [
            "valid": "true"
        ]
    } else {
        return [
            "valid": "false",
            "reason": "Not a valid academic email address."
        ]
    }
}

You can also get the academic institution's name from the email.

app.on(.GET, "institution", ":email") { (req) -> [String: String] in
    guard let email = try? req.parameters.require("email") else {
        return [
            "valid": "false",
            "reason": "Please provide an email to validate.",
            "institutionName": ""
        ]
    }
    
    let institutionName = req.SWOT.institutionFromEmail(email)
    if institutionName != nil {
        return [
            "valid": "true",
            "institutionName": institutionName!
        ]
    } else {
        return [
            "valid": "false",
            "reason": "Not a valid academic email address.",
            "institutionName": ""
        ]
    }
}

Configuration & Customization

The configuration can be set using the SwiftSWOTConfig object on your app.

You can configure:

  • Blacklist
  • Custom institutions that might not be in the database
  • Toggle logging
app.SWOT.configuration = .init(loggingEnabled: true, // Default `false`
                                blacklist: [ // Array of domain names to ignore
                                	"gmail.com",
                                  "yahoo.org"
                                ],
                                trustedDomains: [
                                	"my.gcu.edu": "Grand Canyon University"
                                ])

trustedDomains is a dictionary of Domain : Institution Name. The domain is what SWOT wil search for when validating an email, Institution Name is what will be returned from req.SWOT.institutionFromEmail(email)

Note: gmail.com and yahoo.org are there for example, they will return false if checked against the database.

Github

link
Stars: 2

Dependencies

Releases

Release 1.0.2 - 2021-01-06T14:46:05

Fixed bugs when building for Linux.

String still doesn't conform to CVarArg on Foundation for Linux. It was being used in an NSPredicate to check for valid emails, but has been changed to a NSRegularExpression instead.

Also added some more documentation.

Release 1.0.1 - 2021-01-06T01:10:14