Custom Validation for Reports With Parameters
  • 16 Oct 2021
  • 1 Minute to read
  • Dark
    Light

Custom Validation for Reports With Parameters

  • Dark
    Light

Article summary

WARNING:

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 , including removing the <> signs, with the name of the function defined in the report script:

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)

Was this article helpful?

What's Next