Automatically Build Business Rules using External Data
using External Data
Sometimes using a schema to validate business logic is a case of using the wrong tool for the job. Schemas are generally tightly versioned, and some validation rules that embed any business logic require frequent updating. For example, the complete lists of Australian Business Numbers (ABNs) or Australian Credit Licence (ACL) Numbers are quite dynamic and change frequently. It would be overkill to release a new version of the schema every day to enforce that only valid ABNs or ACLs are provided in the messages.
One of the strengths of using LIXI2 is that there is a library of samples and tools available to get up and running with an impressive amount of automation on the very first day that you are introduced to LIXI.
One piece of automation that we have built for our library of business rules is something that refreshes the rules that validate a number of attributes, including ABNs, BSBs, ACLs and Business Industry Classifications (BIC Codes). You might want to do a similar task for a number of reasons - to refresh product codes or any number of other tasks.
This blog post shares how you can simply write a script to create or update a business rule and test it against a library of sample messages. The example will use the list of Business Industry Code (BIC) list to verify if an institution is using a valid code. This list is publicly available and using this list we will create a business rule that can be validated by the LIXI library against a library of LIXI messages.
This post builds upon concepts already shown in previous posts - make sure to give this post on Automatically Validating Business Rules Against a Library of Messages a read.
Step 1. Prerequisites, Folder creation and Project Clones
As before, create a folder 'lixi-bic-validation-demo' then clone LIXI2 schemas and samples from LIXILab by running the bash/cmd commands below:
Step 2. Copy the Python Code and Run
Create an empty Python file called validate_bic_standalone_script.py and copy the code from the block at the bottom of this page into the file, hit save and run this Python code.
This script performs the following steps:
- Use the
beautiful souplibrary to locate and retrieve an excel file from the
https://data.gov.auwebsite that contains the full list of BIC codes.
- The retrieved excel file is opened using the
openpyxllibrary and the BIC codes extracted and used to create a Schematron Rule to validate the BIC Code of any Trust Applicant.
- The rule is then used to validate every sample in the LIXI Sample Repository and return any validation errors to the console if they occur.
What Next? Over to you!
This is an extremely simple rule that caters for validating the BIC code for a Trust Applicant in a message. The concept and code can easily be applied to your own business rules. Best of all, this methodology is a great way of building a suite of rules that you can evaluate in a CI/CD suite of automated tests, prior to loading up an external API with untested inputs. You can easily manage different versions of rules for different scenarios. In this way, you can discover errors earlier in your software delivery process while it is easier and chapter to fix them.
Python Code to Save in 'validate_bic_standalone_script.py'
Shane Rigby, LIXI Limited CEO
First Published: January 22, 2021