All Projects → gperdomor → sanitize

gperdomor / sanitize

Licence: MIT license
Powerful model extraction from Vapor JSON requests

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to sanitize

VaporGCM
A simple Android GCM/FCM library for Swift/Vapor
Stars: ✭ 25 (+47.06%)
Mutual labels:  vapor
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (+305.88%)
Mutual labels:  vapor
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (+105.88%)
Mutual labels:  vapor
sqlite-kit
Non-blocking SQLite client library with SQL builder built on SwiftNIO
Stars: ✭ 51 (+200%)
Mutual labels:  vapor
Vapor-JWTAuthorization
Vapor JWT Authorization
Stars: ✭ 45 (+164.71%)
Mutual labels:  vapor
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+288.24%)
Mutual labels:  vapor
fluent-provider
A provider for including Fluent in Vapor applications
Stars: ✭ 13 (-23.53%)
Mutual labels:  vapor
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (+5.88%)
Mutual labels:  vapor
mysql-provider
MySQL provider for the Vapor web framework.
Stars: ✭ 31 (+82.35%)
Mutual labels:  vapor
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (+605.88%)
Mutual labels:  vapor
VaporCRUDRouter
A Rails-inspired extension to Vapor's routing system
Stars: ✭ 58 (+241.18%)
Mutual labels:  vapor
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+294.12%)
Mutual labels:  vapor
mongodb-vapor
MongoDB + Vapor integration
Stars: ✭ 31 (+82.35%)
Mutual labels:  vapor
sqlite-nio
Non-blocking wrapper for libsqlite3-dev using SwiftNIO
Stars: ✭ 33 (+94.12%)
Mutual labels:  vapor
apns
Helpful extensions and abstractions for using APNSwift
Stars: ✭ 75 (+341.18%)
Mutual labels:  vapor
vapor-docs
The Vapor documentation.
Stars: ✭ 46 (+170.59%)
Mutual labels:  vapor
swiftybeaver-provider
SwiftyBeaver Logging Provider for Vapor, the server-side Swift web framework https://swiftybeaver.com
Stars: ✭ 32 (+88.24%)
Mutual labels:  vapor
readme
Welcome to Vapor development at Nodes 📖
Stars: ✭ 47 (+176.47%)
Mutual labels:  vapor
OrderSystem
An independent micro-service that takes orders in and processes payments.
Stars: ✭ 16 (-5.88%)
Mutual labels:  vapor
JSONAPISerializer
JSONAPISerializer for Server Side Swift
Stars: ✭ 21 (+23.53%)
Mutual labels:  vapor

Swift Version Vapor Version Build Status codebeat badge codecov GitHub license

Sanitize

Powerful model extraction from JSON requests.

Installation

Add this project to the Package.swift dependencies of your Vapor project:

  .Package(url: "https://github.com/gperdomor/sanitize.git", majorVersion: 1)

or for Swift 4:

  .package(url: "https://github.com/gperdomor/sanitize.git", from: "1.0.0")

Usage

Model

Before you're able to extract your model from a request it needs to conform to the protocol Sanitizable adding a [String] named allowedKeys with a list of keys you wish to allow:

import Sanitize

class User: Sanitizable { // or struct
    var id: Node?
    var name: String
    var email: String

    // Valid properties taken from the request json
    static var allowedKeys: [String] = ["name", "email"]

    //...
}

Now that you have a conforming model, you can safely extract it from a Request

Request Body

{
  "id": 1,
  "name": "John Appleseed",
  "email": "[email protected]"
}

Routes

drop.post("model") { req in
    var user: User = try req.extractModel()
    print(user.id == nil) // prints `true` because was removed (`id` is not a allowed key)
    try user.save()
    return user
}

Pre and Post validations

You can also configure some preSanitize and postSanitize validations, this validations will be executed before and after model initialization.

extension User {
    static func preSanitize(data: JSON) throws {
        guard data["name"]?.string != nil else {
            throw Abort(
                .badRequest,
                metadata: nil,
                reason: "No name provided."
            )
        }

        guard data["email"]?.string != nil else {
            throw Abort(
                .badRequest,
                metadata: nil,
                reason: "No email provided."
            )
        }
    }

    func postSanitize() throws {
        guard email.characters.count > 8 else {
            throw Abort(
                .badRequest,
                metadata: nil,
                reason: "Email must be longer than 8 characters."
            )
        }
    }
}

Credits

This package is developed and maintained by Gustavo Perdomo.

This package is heavily inspired by Sanitized

License

Sanitize is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].