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 #5 from nodes-vapor/feature/allow-injection-of-values
Browse files Browse the repository at this point in the history
Allow injection of values
  • Loading branch information
BrettRToomey authored Jan 19, 2017
2 parents 07f0264 + 802dfe0 commit 2ceece9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
25 changes: 22 additions & 3 deletions Sources/Sanitizable+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@ extension Request {
///
/// - 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.
///
/// - Parameter values: Values to set before sanitizing.
/// - Returns: The extracted, sanitized `Model`.
/// - Throws:
/// - 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.
public func extractModel<M: Model>(injecting values: Node) throws -> M where M: Sanitizable {
guard let json = self.json else {
throw Abort.badRequest
}

let sanitized = json.permit(M.permitted)


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

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

try M.preValidate(data: sanitized)

let model: M
Expand Down
76 changes: 71 additions & 5 deletions Tests/SanitizedTests/SanitizedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class SanitizedTests: XCTestCase {
("testPermitted", testPermitted),
("testEmptyPermitted", testEmptyPermitted),
]



// MARK: - Extraction.

func testBasic() {
let request = buildRequest(body: [
"id": 1,
Expand All @@ -28,14 +31,71 @@ class SanitizedTests: XCTestCase {
XCTAssertEqual(model.email, "[email protected]")
}
}



func testBasicFailed() {
let request = buildInvalidRequest()
expect(toThrow: Abort.badRequest) {
let _: TestModel = try request.extractModel()
}
}



// MARK: - Injection.

func testInjectingNewKeys() {
let request = buildRequest(body: [
"id": 1,
"name": "Brett"
])

expectNoThrow() {
let model: TestModel = try request.extractModel(
injecting: ["email": "[email protected]"]
)
XCTAssertNil(model.id)
XCTAssertEqual(model.name, "Brett")
XCTAssertEqual(model.email, "[email protected]")
}
}

func testOverridingKeys() {
let request = buildRequest(body: [
"id": 1,
"name": "Brett",
"email": "[email protected]"
])

expectNoThrow() {
let model: TestModel = try request.extractModel(
injecting: ["email": "[email protected]"]
)
XCTAssertNil(model.id)
XCTAssertEqual(model.name, "Brett")
XCTAssertEqual(model.email, "[email protected]")
}
}

func testInjectingSanitizedKeys() {
let request = buildRequest(body: [
"id": 1,
"name": "Brett",
"email": "[email protected]"
])

expectNoThrow() {
let model: TestModel = try request.extractModel(
injecting: ["id": 1337]
)
XCTAssertNil(model.id)
XCTAssertEqual(model.name, "Brett")
XCTAssertEqual(model.email, "[email protected]")
}
}


// MARK: - Validation.

func testPreValidateError() {
let request = buildRequest(body: [
"email": "[email protected]"
Expand All @@ -62,7 +122,10 @@ class SanitizedTests: XCTestCase {
let _: TestModel = try request.extractModel()
}
}



// MARK: - Permitted fields.

func testPermitted() {
let json = JSON([
"id": 1,
Expand All @@ -88,7 +151,10 @@ class SanitizedTests: XCTestCase {
XCTAssertNil(result["name"])
XCTAssertNil(result["email"])
}



// MARK: - Patching.

func testPatchBasic() {
let model = try! TestModel(node: [
"id": 15,
Expand Down

0 comments on commit 2ceece9

Please sign in to comment.