https://github.com/gperdomor/sanitize
Powerful model extraction from Vapor JSON requests
https://github.com/gperdomor/sanitize
swift vapor
Last synced: 8 months ago
JSON representation
Powerful model extraction from Vapor JSON requests
- Host: GitHub
- URL: https://github.com/gperdomor/sanitize
- Owner: gperdomor
- License: mit
- Archived: true
- Created: 2017-09-28T15:37:47.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-11T18:12:14.000Z (over 7 years ago)
- Last Synced: 2024-08-07T23:55:56.616Z (11 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
[](http://swift.org)
[](http://vapor.codes)
[](https://circleci.com/gh/gperdomor/sanitize)
[](https://codebeat.co/projects/github-com-gperdomor-sanitize-master)
[](https://codecov.io/gh/gperdomor/sanitize)
[](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).