Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gperdomor/sanitize
Powerful model extraction from Vapor JSON requests
https://github.com/gperdomor/sanitize
swift vapor
Last synced: about 2 months ago
JSON representation
Powerful model extraction from Vapor JSON requests
- Host: GitHub
- URL: https://github.com/gperdomor/sanitize
- Owner: gperdomor
- License: mit
- Created: 2017-09-28T15:37:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-11T18:12:14.000Z (over 6 years ago)
- Last Synced: 2024-05-01T16:49:09.455Z (5 months ago)
- Topics: swift, vapor
- Language: Swift
- Homepage:
- Size: 42 KB
- Stars: 17
- Watchers: 4
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Swift Version](https://img.shields.io/badge/Swift-3.1_and_4.0-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-2-brightgreen.svg)](http://vapor.codes)
[![Build Status](https://img.shields.io/circleci/project/github/gperdomor/sanitize.svg?label=Build)](https://circleci.com/gh/gperdomor/sanitize)
[![codebeat badge](https://codebeat.co/badges/96ac7dc6-b1a7-4cc5-bb95-8a33f967bb65)](https://codebeat.co/projects/github-com-gperdomor-sanitize-master)
[![codecov](https://img.shields.io/codecov/c/github/gperdomor/sanitize.svg)](https://codecov.io/gh/gperdomor/sanitize)
[![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)# Sanitize
Powerful model extraction from JSON requests.
## Installation
Add this project to the `Package.swift` dependencies of your Vapor project:
```swift
.Package(url: "https://github.com/gperdomor/sanitize.git", majorVersion: 1)
```or for Swift 4:
```swift
.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:```swift
import Sanitizeclass 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
```json
{
"id": 1,
"name": "John Appleseed",
"email": "[email protected]"
}
```### Routes
```swift
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.```swift
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](https://github.com/gperdomor).This package is heavily inspired by [Sanitized](https://github.com/nodes-vapor/sanitized)
## License
Sanitize is released under the [MIT License](LICENSE).