Skip to content

Commit

Permalink
Bugfix/error value (#28)
Browse files Browse the repository at this point in the history
* fixed return value

* 1.1.5 jsonlogic

* fixed json

* fixed returned value

Co-authored-by: Alexandr Chernyy <pingus.nikalex@gmail.com>
  • Loading branch information
alexchornyi and pingus-nikalex authored Jul 7, 2021
1 parent 14bcdce commit 327880e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "jsonlogic", url: "https://github.com/eu-digital-green-certificates/json-logic-swift.git", from: "1.1.2"),
.package(name: "jsonlogic", url: "https://github.com/eu-digital-green-certificates/json-logic-swift.git", from: "1.1.5"),
.package(name: "SwiftyJSON", url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "5.0.0")
],
targets: [
Expand Down
48 changes: 36 additions & 12 deletions Sources/CertLogic/CertLogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public typealias Codable = Decodable & Encodable
final public class CertLogicEngine {

private var schema: JSON?
private var payloadJSON: JSON?
private var rules: [Rule]

public init(schema: String, rules: [Rule]) {
Expand All @@ -36,15 +37,15 @@ final public class CertLogicEngine {
}

public func validate(external: ExternalParameter, payload: String) -> [ValidationResult] {
let payloadJSON = JSON(parseJSON: payload)
self.payloadJSON = JSON(parseJSON: payload)
var result: [ValidationResult] = []

let rulesItems = getListOfRulesFor(external: external, issuerCountryCode: external.issueCountryCode)
if(rules.count == 0) {
result.append(ValidationResult(rule: nil, result: .passed, validationErrors: nil))
return result
}
guard let qrCodeSchemeVersion = payloadJSON["ver"].rawValue as? String else {
guard let qrCodeSchemeVersion = self.payloadJSON?["ver"].rawValue as? String else {
result.append(ValidationResult(rule: nil, result: .fail, validationErrors: nil))
return result
}
Expand Down Expand Up @@ -104,7 +105,7 @@ final public class CertLogicEngine {
let externalJsonString = String(data: jsonData, encoding: .utf8)!

var result = ""
result = "{" + "{" + "\"\(Constants.external)\":" + "\(externalJsonString)" + "}," + "\"\(Constants.payload)\":" + "\(payload)" + "}" + "}"
result = "{" + "\"\(Constants.external)\":" + "\(externalJsonString)" + "," + "\"\(Constants.payload)\":" + "\(payload)" + "}"
return result
}

Expand Down Expand Up @@ -201,8 +202,8 @@ final public class CertLogicEngine {
}

// Get details rule error by affected fields
public func getDetailsOfError(rule: Rule, external: ExternalParameter) -> String {
var value: String = ""
public func getDetailsOfError(rule: Rule, external: ExternalParameter) -> Dictionary<String, String> {
var result: Dictionary<String, String> = Dictionary()
rule.affectedString.forEach { key in
var keyToGetValue: String? = nil
let arrayKeys = key.components(separatedBy: ".")
Expand All @@ -216,16 +217,12 @@ final public class CertLogicEngine {
}
// All other keys will skiped (example: "r.0")
if let keyToGetValue = keyToGetValue {
if let newValue = self.getValueFromSchemeBy(external: external, key: keyToGetValue) {
if value.count == 0 {
value = value + "\(newValue)"
} else {
value = value + " / " + "\(newValue)"
}
if let newValue = self.getValueFromSchemeBy(external: external, key: keyToGetValue), let newPayloadValue = self.getValueFromPayloadBy(external: external, key: keyToGetValue) {
result[newValue] = newPayloadValue
}
}
}
return value
return result
}

private func getValueFromSchemeBy(external: ExternalParameter, key: String) -> String? {
Expand All @@ -245,6 +242,27 @@ final public class CertLogicEngine {
return nil
}

private func getValueFromPayloadBy(external: ExternalParameter, key: String) -> String? {
var section = Constants.payloadTestEntry
if external.certificationType == .recovery {
section = Constants.payloadRecoveryEntry
}
if external.certificationType == .vacctination {
section = Constants.payloadVaccinationEntry
}
if external.certificationType == .test {
section = Constants.payloadTestEntry
}
if let newValue = self.payloadJSON?[section][0][key].string {
return newValue
}
if let newValue = self.payloadJSON?[section][0][key].number {
return newValue.stringValue
}
return nil
}


}

extension CertLogicEngine {
Expand All @@ -254,11 +272,17 @@ extension CertLogicEngine {
static let defSchemeVersion = "1.0.0"
static let maxVersion: Int = 2
static let majorVersionForSkip: Int = 10000

static let testEntry = "test_entry"
static let vaccinationEntry = "vaccination_entry"
static let recoveryEntry = "recovery_entry"
//Schema section
static let schemeDefsSection = "$defs"
static let properties = "properties"
static let description = "description"
//Payload section
static let payloadTestEntry = "t"
static let payloadVaccinationEntry = "v"
static let payloadRecoveryEntry = "r"
}
}

0 comments on commit 327880e

Please sign in to comment.