Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from nodes-vapor/injection-fix
Browse files Browse the repository at this point in the history
Fixed test and expected behaviour of extractModel(injecting:)
  • Loading branch information
BrettRToomey authored Jan 21, 2017
2 parents a4e94ea + be30566 commit 3c02f80
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions Sources/Sanitizable+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ extension Request {
/// - badRequest: Thrown when the request doesn't have a JSON body.
/// - updateErrorThrown: `Sanitizable` models have the ability to override
/// the error thrown when a model fails to instantiate.
///
///
/// - Returns: The extracted, sanitized `Model`.
public func extractModel<M: Model>() throws -> M where M: Sanitizable {
return try extractModel(injecting: .null)
}


/// Extracts a `Model` from the Request's JSON, first by adding/overriding
/// the given values and next stripping sensitive fields.
///
Expand All @@ -29,14 +29,12 @@ extension Request {
guard let json = self.json else {
throw Abort.badRequest
}

var node = json.makeNode()
var sanitized = json.permit(M.permitted)
values.nodeObject?.forEach { key, value in
node[key] = value
sanitized[key] = JSON(value)
}

let sanitized = try JSON(node: node).permit(M.permitted)


try M.preValidate(data: sanitized)

let model: M
Expand Down
2 changes: 1 addition & 1 deletion Tests/SanitizedTests/SanitizedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class SanitizedTests: XCTestCase {
let model: TestModel = try request.extractModel(
injecting: ["id": 1337]
)
XCTAssertNil(model.id)
XCTAssertEqual(model.id, 1337)
XCTAssertEqual(model.name, "Brett")
XCTAssertEqual(model.email, "[email protected]")
}
Expand Down

0 comments on commit 3c02f80

Please sign in to comment.