Script validators API

Example validator code

Below is sample code that shows how to use the input and how to return data:

function callService(context) {
    let input1 = context.getFirstParameter("input1");
    if (input1 === "api") {
        return [{
                'key': 'pl.error',
                'parameters': [input1, "parameter {0} is invalid"]
            }
            ];
    } else if (input1 != null && input1 !== "") {
        return [{
                'key': 'pl.error',
                'parameters': [input1, "another hardcoded parameter"]
            }];
    } else {
        return [];
    }
}

Input to the script validator

As input (parameter context in the code above) there is an object that provides the following methods:

  • List<String> getParameters(final String name) - retrieves a specific list from the input data map,

  • String getFirstParameter(final String name) - retrieves the first element from a specific list in the input data map or returns the value "null",

  • String getFirstParameter(final String name, final String defaultIfEmpty) - retrieves the first element from a specific list in the input data map, and if it is missing or empty returns the value "defaultIfEmpty",

  • Map<String, List<String>> getInputParameters() - returns the entire input data map that it stores,

  • String isVisible(final String id) - retrieves information whether the component with the given id is visible,

  • String getValue(final String id) - retrieves the value of the component (also session variable) with the given id,

  • String getData(final String id, final String attribute) - retrieves the value of a specific attribute for the component with the given id (NOTE! The retrievable attributes depend on the component),

  • List<Map<String, String>> callService(String name, Map<String, List<String>> input) - allows calling a ServiceProxy,

  • ValidatorOutput validate(String name, Map<String, List<String>> input) - allows calling a validator,

Example of retrieving a list (e.g. for fields in a repeatable section):

function callService(context) {
    let inputFromRepeatableSection = context.getParameters("inputFromRepeatableSection").toArray();
}

Output from the validator

  1. List of maps: an array of objects containing the fields "key" and "parameters".

  2. Optional fields:

    1. type - validator type is set to INLINE by default, another option is POPUP,

    2. parameters - variables that we can send to the script validation message

    3. attributes - for example POPUP attributes adding a 'next' button and changing, for example, the value of a component after pressing 'next':

return [{
          'key': 'pl.errorPopup',
          'parameters': [input, 'The provided value is incorrect'],
          'type': 'POPUP',
          'attributes': {
              'componentUpdateId': componentID,
              'componentUpdateValue':'potato'
          }
      }];

for example attributes for row validation in GesTable:

Example INLINE validation - GesTable rows

return [{
          'key': 'pl.errorGesTable',
          'parameters': [input, 'The provided value is incorrect'],
          'type': 'INLINE',
          'attributes': {
              'colName': 'columnName',
              'rowId':id
          }
      }];

Logging

Logging of input data is automatic.

Manual logging should comply with the description in Logging in ScriptCode

Throwing business exceptions

It is possible to throw a business exception from within the script. Sample code:

function callService(context) {
    let firstArgument = context.getFirstParameter('firstArgument');
    let builder =  context.getErrorPageDefinitionBuilder();  
    builder.bodyTextContent("textContentName-*"); // TextContent component as an error page
    builder.logobarTitleKey("form.title"); // Content of the top bar (if present)
    context.throwBusinessException(builder);
    ...
}

Possible methods that can be called on the 'context' object:

context.getErrorPageDefinitionBuilder()
.body(string)
.styleName(string);
.msg(string)
.pageTitle(string)
.pageTitleKey(string)
.sidebarSlot2TextContent(string);
.sidebarSlot2(string);
.sidebarSlot1TextContent(string);
.sidebarSlot1(string);
.retryButtonAvailable(string)
.retryButtonAvailable(boolean);
.logobarTitle(string);
.logobarTitleKey(string)
.logobarDescription(string);
.bodyTextContent(string);
.parameter(String key, String value)
.cause(Throwable cause)
.redirectUrl(string)
.redirectDelayInMillis(string)
.businessFormIdentifier(string)

Examples of validators:

demoEmailValidator, demoPESELValidator, demoIDCardValidator, demoMinMaxValidator

Last updated

Was this helpful?