# Creating and attaching script validators

## Creating a validator <a href="#walidatoryskryptowe-validationscript-tworzeniewalidatora" id="walidatoryskryptowe-validationscript-tworzeniewalidatora"></a>

A script validator is one of the artifact types in the repository. To add a script validator, go to the tab **Library → Script validators**, and then click the button on the right side **Add a script validator.**

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2FojbL83aL5vYYiRnu0yCl%2Fimage.png?alt=media&#x26;token=841f51d4-0e7c-42f7-9fbb-cd4522496078" alt=""><figcaption><p><em><strong>Figure 1.</strong> Adding script validators</em></p></figcaption></figure>

## Edit window <a href="#walidatoryskryptowe-validationscript-oknoedycji" id="walidatoryskryptowe-validationscript-oknoedycji"></a>

The edit window in the repository allows you to change the validator content and the returned keys and default error messages. In the section **Parameters** all input and output validator keys resulting from the code content should be added (error keys). The parameters window can be displayed by clicking the button in the left panel ![](https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2FlcsQX63BJLjsjRv1no0n%2Fimage.png?alt=media\&token=3d1b4f04-bad2-4656-b0d6-1520fc814dcf).

More information about the editor itself can be found in the [Script validator editor](https://docs.eximee.com/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/walidatory-skryptowe-validationscript/edytor-walidatorow-skryptowych).

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2F1dtppZw8rgiRMfjB0YX1%2Fobraz.png?alt=media&#x26;token=d2c96196-58ba-44f1-aac9-9553a892c89e" alt=""><figcaption><p><em><strong>Figure 2.</strong> Script validator edit window</em></p></figcaption></figure>

## Attaching to the application <a href="#walidatoryskryptowe-validationscript-podpiecienawniosku" id="walidatoryskryptowe-validationscript-podpiecienawniosku"></a>

Attaching a script validator to an application does not differ from attaching a standard validator. For a specific component in the panel **Properties** you should select **Data quality → Validators → Add validator**, and then search for its name. In the tab **Input parameters** you should select the appropriate component in the field **Select component or constant**.\
The switch in the column ***Call when empty*** is responsible for invoking the validator even when there is no value in the indicated field (e.g. when a value is removed from a text field).

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2Fc8AxoQJA5ifkjQjEfbll%2Fimage.png?alt=media&#x26;token=db68e283-e499-4cd4-8fba-e242c6644d65" alt=""><figcaption><p><em><strong>Figure 3.</strong> Attaching a script validator to the application</em></p></figcaption></figure>

## Creating a validator error message <a href="#walidatoryskryptowe-validationscript-tworzeniekomunikatubleduwalidatora" id="walidatoryskryptowe-validationscript-tworzeniekomunikatubleduwalidatora"></a>

The content of the error message (key translation) of the script validator must be defined in the component/application after attaching the validator. Adding the message content in the script is not an error, but it will not be visible in the edit window of the component/application. After attaching the validator, both the key and its translation will display the key name defined in the content of the script validator:

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2FwWKQM0nWS9hQtTcy3tHg%2Fimage.png?alt=media&#x26;token=a35b71eb-59fe-4471-8ab5-bf1196dc207c" alt=""><figcaption><p><em><strong>Figure 4.</strong> Translation of the script validator key</em></p></figcaption></figure>

\
You should edit the translation column and enter the message text that should appear when the validator returns a specific error message key.

If it is necessary to use variables in the error message, these variables should be listed in the code as parameters (parameters: \[value1, value2, etc.]), and then referenced in the error message in the component/application as follows: {0} for the first parameter, {1} for the second parameter, {2} for the third, etc.

```
let minimumAge = 18;
let maximumAge = 25;
if (minimumAge > ageDifference || maximumAge < ageDifference) {
            return [{
            'key': 'en.LCL_birthDate.outOfRange',
            'parameters': [minimumAge, maximumAge, 'This application can be submitted by people aged {0}-{1}.']               
        }];

```

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2FEodT5VCbrwNK3W4ORHoN%2Fimage.png?alt=media&#x26;token=b539cff0-4330-41cf-9b8c-3b55c1f6a118" alt=""><figcaption><p><em><strong>Figure 5.</strong> Translation of the script validator key powered by variables</em></p></figcaption></figure>

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2FCXXOfiYPEMMv0VGs0ceS%2Fimage.png?alt=media&#x26;token=394a11cf-a017-4418-822c-4e57c1fc6484" alt=""><figcaption><p><em><strong>Figure 6.</strong> Error message content visible on the application</em></p></figcaption></figure>
