- 16 Oct 2021
- 1 Minute to read
- Print
- DarkLight
Custom Validation for Reports With Parameters
- Updated on 16 Oct 2021
- 1 Minute to read
- Print
- DarkLight
Custom validation is not recommended for anyone other than advanced users. Using custom validation can cause the inexperienced user to leave proper integer or string notation and cause errors to occur.
Instead of relying on IMAT's search server validation for reports with parameters, it is possible to set customized validation. Use this if the need to verify a specific object arises or to set specific validation needs, such as validating the presence of Social Security Numbers.
The compound report script must define the custom validation before it is used within the parameters section. In most cases, it will be a defined function. After the custom validation logic has been defined, include the name of the function within the parameters section. Write the parameter with this line, replacing
params.add('param_name', type='str', validation=<customvalidation>)
The report below creates a function to recognize SSNs by their pattern. in the parameters section it then uses the validation option and sets it equal to the name of the function, ssnValidation.
Example:
import re
SSN_PATTERN = re.compile("\d{3}-\d{2}-\d{4}") #000-00-0000 pattern
def ssnValidation(parameter):
if parameter.value is None or re.match(SSN_PATTERN, parameter.value):
return (True, 'Passed Social Security Regular Expression')
else:
return (False, 'Failed Social Security Regular Expression, Should be in format 000-00-0000')
params.add("socialSecurity", type="str", validation=ssnValidation)
params.end()
print(params.socialSecurity)