Turkish Identification Number Validation in JSON Format
In this Groovy code, the first step is to check the character length of the Turkish Identification Number in JSON format. If it is appropriate, the code validates the identification number using a specific algorithm. The validation steps involve certain mathematical calculations related to the Turkish Identification Number. In case of any errors, an appropriate error message is assigned to the variable "requestErrorMessageToTargetAPI," preventing the request from reaching the backend.
import groovy.json.JsonSlurper
// Create a parser for processing JSON data
def parser = new groovy.json.JsonSlurper()
// Parse the incoming JSON data
def jsonResp = parser.parseText(requestBodyTextFromClient)
// Extract Turkish Identification Numbers from the received JSON
def requestingUserIdentityCardNo = jsonResp.requestingUserIdentityCardNo
def requestedIdentityCardNo = jsonResp.requestedIdentityCardNo
// Method to validate the Turkish Identification Number
def validateTCKN(variableToCheck) {
// Check the length and validity of the Turkish Identification Number
if (!variableToCheck || variableToCheck.length() != 11) {
requestErrorMessageToTargetAPI = 'Please send a valid parameter: ' + variableToCheck + '.'
} else {
// Validate the Turkish Identification Number mathematically
def tcArr = variableToCheck.substring(0, 11).split('')
def odd = tcArr[0].toInteger() + tcArr[2].toInteger() + tcArr[4].toInteger() + tcArr[6].toInteger() + tcArr[8].toInteger()
def even = tcArr[1].toInteger() + tcArr[3].toInteger() + tcArr[5].toInteger() + tcArr[7].toInteger()
odd = odd * 7
def result = odd - even
// Create an error message in case of validation errors
if (result % 10 != tcArr[9].toInteger()) {
requestErrorMessageToTargetAPI = 'Please send a valid parameter: ' + variableToCheck + '.'
}
def tcTotal = 0
for (int i = 0; i < 10; i++) {
tcTotal += tcArr[i].toInteger()
}
// Create an error message in case of validation errors
if (tcTotal % 10 != tcArr[10].toInteger()) {
requestErrorMessageToTargetAPI = 'Please send a valid parameter: ' + variableToCheck + '.'
}
}
}
// Validate both Turkish Identification Numbers
validateTCKN(requestingUserIdentityCardNo)
validateTCKN(requestedIdentityCardNo)
GROOVY
#Sample Request:
{"requestingUserIdentityCardNo": 11111111110, "requestedIdentityCardNo":11111111112 }
GROOVY