Validate Data with Script Component

Nikolina Špehar avatar
Nikolina Špehar
Cover for Validate Data with Script Component

Continuing from my previous blog post - Data Validation - I wanted to go a bit more in-depth into what’s possible with a tool like Bytechef. This time, I’ll connect to Liferay’s API to retrieve data and demonstrate how the Script component can be used to handle the entire validation process programmatically.

Data Validation Workflow

Validating Geographical Locations

In this more advanced workflow, I connected my Liferay system to the workflow using the Liferay API to pull in data that requires validation. The data was saved inside Liferay’s objects, making it easy to retrieve via API endpoint.

Instead of using the Loop component (as in the previous example), I switched to the Script component for the main validation logic. This change highlights the full power of the Script component - and shows how much you can achieve with just a bit of coding knowledge.

As mentioned earlier, the data import is handled by the Liferay component. The data is then passed to the JSON File component, which serves as input for the Script component.

Below is a snippet from the Script component. Inside a for loop, I iterate through each data element and call the Google Maps component to fetch the corresponding address.

def getAddressFromGmResult(gmResult):
	return gmResult.results[0].address_components

def perform(input, context):

	invalid = []

	for cashpoint in input.cp:
		latDB = cashpoint.GEO_SIRINA
		lngDB = cashpoint.GEO_DUZINA

		gmResult = None
		for attempt in range(5):
			try:
				gmResult = context.component.googleMaps.getAddress({'latitude': latDB, 'longitude': lngDB})
				break
			except Exception as e:
				print(f"Attempt {attempt+1} failed: {e}")
				if attempt < 4:
					time.sleep(10)
				else:
					print("All 5 attempts to fetch Google Maps address failed.")
					gmResult = None

		if gmResult is None:
			continue

		ulicaDB = cashpoint.ULICA.lower()
		ulicaKbrDB = cashpoint.ULICA_KBR

		gmAddressObject = getAddressFromGmResult(gmResult)

		gmUlica = gmAddressObject[1].long_name.lower()
		gmUlicaKbr = gmAddressObject[0].long_name

		check_ulica = ulicaDB == gmUlica
		check_ulica_kbr = ulicaKbrDB == gmUlicaKbr

		if not (check_ulica and check_ulica_kbr):
			invalid_cp = replace_none_with_dash(cashpoint)
			invalid.append(invalid_cp)

	return invalid

To ensure workflow stability, I included a fail-safe mechanism to handle potential unavailability of the Google Maps API. If an HTTP request to the API fails, the Script component retries up to four additional times. If all five attempts fail, the data element is flagged as invalid, and processing continues with the next element.

After all elements have been checked, the Script component returns a list of invalid data entries. In the final step, this list is saved as a CSV file and uploaded to Google Drive for further review or correction.

Conclusion

By combining Bytechef’s flexibility with the power of the Script component, we can take data validation to the next level. Connecting directly to Liferay’s API allows for dynamic, real-time validation, while scripting enables greater control, customization, and error handling. This approach not only demonstrates how adaptable Bytechef can be but also shows that with just a bit of coding knowledge, you can build highly efficient and automated data validation workflows tailored to any system.