Skip to content

InsectQY/MoyaResultValidate

main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

MoyaResultValidate

CI Status Version License Platform

Why?

Sometimes we need to verify that the data returned by the server is reasonable, when Moya returns Result.success.

JSON returned by the server.

{
  "code": 2000,
  "msg": "success",
  "data": ...
}

Validate the JSON or XML or other type results. It seems not elegant.

provider.request(.baidu) { result in
    switch result {
    case let .success(response):
	if response["code"] == 2000 {
          /// success code
        } else {
          /// error code
        }
    case let .failure(error):
        break
    }
}

Use MoyaResultValidate

  1. Import.
import MoyaResultValidate
  1. Add plugin to Moya.
MoyaResultValidatePlugin()
  1. Let you already comply with the TargetType enum also implemented MoyaResultValidateable protocol.
extension RequestApi: MoyaResultValidateable {
    
    func isResultSuccess(response: Response) -> Bool {
        // Validation rules
    }
}

Then it will work.Your request will look like this.

The Result.success(response) that does not match isResultSuccess will be transferred to Result.Failure.

You can use error.asValidateError to get Moya.Response.

provider.request(.baidu) { result in
    switch result {
    case let .success(response):
				// succees 
    case let .failure(error):
      	// You can use error.asValidateError get Moya.Response.
        error.asValidateError?.response
    }
}

Requirements

  • iOS 10.0 or later

Installation

MoyaResultValidate is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MoyaResultValidate'

License

MoyaResultValidate is available under the MIT license. See the LICENSE file for more info.

About

Validation plugin for Moya.Result

Resources

License

Stars

Watchers

Forks

Packages

No packages published