Script Validators API
Sample validator code
Below is an example code that shows how to use input and how data should be returned:
function callService(context) {
let input1 = context.getFirstParameter("input1");
if (input1 === "api") {
return [{
'key': 'pl.error',
'parameters': [input1, "parameter {0} is incorrect"]
}
];
} else if (input1 != null && input1 !== "") {
return [{
'key': 'pl.error',
'parameters': [input1, "another hardcoded parameter"]
}];
} else {
return [];
}
}Entry 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 from 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 from the input data map, and if it does not exist or is empty returns the value "defaultIfEmpty",
Map<String, List<String>> getInputParameters() - returns the entire input data map it stores,
String isVisible(final String id) - retrieves information whether a 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 a component with the given id (NOTE! Available attributes depend on the component),
List<Map<String, String>> callService(String name, Map<String, List<String>> input) - allows calling ServiceProxy,
ValidatorOutput validate(String name, Map<String, List<String>> input) - allows calling the validator,
Example of retrieving a list (e.g. for fields in a repeatable section):
Output from the validator
Map list: array of objects containing the fields "key" and "parameters".
Optional fields:
type - validator type is set to INLINE by default, another option is POPUP,
parameters - variables that we can send to the script validation message
attributes - e.g. POPUP attributes adding a 'next' button and changing e.g. the component value after pressing 'next':
e.g. attributes for GesTable row validation:
Example INLINE validation - GesTable rows
Logging
Logging input data is automatic.
Manual logging should be consistent with the description in Logging in ScriptCode
Throwing business exceptions
Business errors have been described in detail on the page Error pages.
Last updated
Was this helpful?
