VatNumberKit is a Swift library to check and validate VAT numbers (checksum based & online government services) on both iOS and macOS.
Swift Package Manager is a tool for managing the distribution of Swift code. Itβs integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Xcode 11+ is required to build VatNumberKit using Swift Package Manager.
To integrate VatNumberKit into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift
:
dependencies: [
.package(url: "https://github.com/frederik-jacques/vatnumberkit.git", .upToNextMajor(from: "0.1.0"))
]
If you prefer not to use Swift Package Manager, you can integrate VatNumberKit into your project manually.
Use the static method VatNumberKit.validateFormat(vatNumber:)
to check if the VAT number format (based on regexes) is valid.
The result of this call returns a VatNumberKit.ValidationOutput
object which has the following properties.
rawVatNumber
: The original VAT numbervatNumber
: A VatNumber
object (country & number seperated)isValid
: Is the format validimport VatNumberKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let validationOutput = VatNumberKit.validateFormat(vatNumber: "BE0651634023"), validationOutput.isValid {
print("The VAT number has a valid format")
}
}
}
Use the static method VatNumberKit.validateChecksum(rawVatNumber:)
to check if the checksum for the given VAT number is valid.
The result of this call returns a Boolean
, indicating if the checksum is correct.
import VatNumberKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if VatNumberKit.validateChecksum(rawVatNumber: "BE0651634023"){
print("The VAT number has a valid checksum")
}
}
}
VatNumberKit also allows you to validate a VAT number against 2 online services.
Use the static method VatNumberKit.validateOnline(vatNumber:completionHandler:)
to use the online validation.
VatNumberKit will automatically select the correct service, based on the country code.
VatNumberKit.validateOnline(vatNumber: "GB835145337") { result in
switch result {
case .success(let validation):
if validation.isValid {
// Retrieve the meta data for this VAT number (name & address)
// Metadata is not always available.
if let metaData = validation.metaData {
print("§§ Name - \(metaData.name)")
print("§§ Address - \(metaData.address)")
}
}
else {
print("§§ VAT number is not valid")
}
case .failure(let error):
print("Error validating number")
if let validationError = error as? VatNumberKit.ValidationServiceError {
switch validationError {
case .invalidUrl:
print("The request url was not valid")
case .invalidJsonResponse:
print("The json returned from the service is invalid")
case .invalidUrlResponse:
print("The response returned from the service is invalid")
case .validationServiceDown:
print("The validation services is down")
case .vatNumberHasIncorrectNumberOfDigits:
print("The supplied VAT number has an incorrect number of digits (UK service only)")
case .vatNumberDoesNotMatchRegisteredCompany:
print("The supplied VAT number does not match a registered company (UK service only)")
}
}
else {
// Regular error
}
}
Use the static method VatNumberKit.searchVatNumbersInText(:applyChecksumValidation:)
to find VAT numbers in a text.
This method returns a Set of VatNumberKit.ValidationOutput
structs.
import VatNumberKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let validationObjects = VatNumberKit.searchVatNumbersInText(text: "This is a VAT number that is BE0651634023 somewhere in the text. \n Apple's Belgian VAT number is BE0842936235")
// Will contain 2 VatNumberKit.ValidationOutput structs.
}
}
Country | Offline format validation (regex) | Offline checksum validation | Online validation |
---|---|---|---|
Austria | β | β | β |
Belgium | β | β | β |
Bulgaria | β | β | β |
Croatia | β | β | β |
Cyprus | β | β | β |
Czech Republic | β | β | β |
Denmark | β | β | β |
Estonia | β | β | β |
European Entity | β | β | β |
Finland | β | β | β |
France | β | β | β |
Germany | β | β | β |
Great Britain | β | β | β |
Greece | β | β | β |
Hungary | β | β | β |
Ireland | β | β | β |
Italy | β | β | β |
Latvia | β | β | β |
Lithuania | β | β | β |
Luxembourg | β | β | β |
Malta | β | β | β |
The Netherlands | β | β | β |
Northern Ireland | β | β | β |
Poland | β | β | β |
Portugal | β | β | β |
Romania | β | β | β |
Slovakia | β | β | β |
Slovenia | β | β | β |
Spain | β | β | β |
Sweden | β | β | β |
Feel free to open a PR to add other countries!
Sources used to find more information about the checksums, as every country seems to have forgotten to document how it is being calculated :)
VatNumberKit is released under the MIT license. See LICENSE for details.
link |
Stars: 10 |
Last commit: 1 year ago |
Added offline checksum validation
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics