# Validation pipeline

**This section explains Validation pipelines and their usage with Langstack entities. By the end of this section, you will be able to understand fundamentals and hands-on applications of the Validation pipeline.**

## What is a Validation pipeline?

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validationmenu.png" alt=""><figcaption></figcaption></figure>

* The Validation pipeline section consists of templates to create a series of checks. These checks can be performed on the data before being stored in a target entity.&#x20;
* A Validation pipeline can act as an independent unit usable in multiple Langstack applications without interfering with the logic of the Langstack application using it.&#x20;
* The validation pipeline can be edited without changing the Langstack applications associated with it. For example, suppose a Validation “Username\_Acc\_customers\_validation” has a target entity “Username\_Acc\_customers”. In this case, the Validation rules can be updated without any impact on the target entity schema or the data within or any changes to the Langstack application that is using it.&#x20;
* One Validation pipeline can have one target entity configured. In the image below, the Validation “UserName\_Acc\_customers\_validation” has one target entity “UserName\_Acc\_customers”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validname.png" alt=""><figcaption></figcaption></figure>

* A Validation pipeline can be executed through a dedicated API gateway. This option is available to the API.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validinapigtway.png" alt=""><figcaption></figcaption></figure>

* Other Langstack application templates working with an entity, e.g. ETL pipeline or Process, can configure the Validation pipeline in the “Target Entities” tab if a target entity is selected.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11protevalid.png" alt=""><figcaption></figcaption></figure>

* Multiple Validation pipelines can be created for the same entity. \
  For example, in banking operations, loan evaluation is different between students and non-students, in such a case, a different validation can be made for students through one API gateway and another API gateway for non students. \
  While the same API is used for both API gateways, the validation steps are different depending on the customer's profile for the loan type. The validations may be defined as follows:
  * “UserName\_Acc\_customers\_validation”: For a customer to qualify for a student loan, required details include Name, Social Security Number, Guardian’s Name, Account Number, College Reference. Validation 1 is set for the students API gateway.&#x20;
  * UserName\_Acc\_newcustomers\_validation” For a customer to qualify for a house mortgage, required details include Name, Social Security Number, Account Number, Annual Salary. Validation 2 is set for non-students.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11customers2valids.png" alt=""><figcaption></figcaption></figure>

* Target entities in Langstack applications can be linked to one Validation pipeline, however, the user can choose in the API>Target Entities tab not to invoke the linked Validation pipeline.&#x20;
* To view the shared variables for this Validation pipeline, go to Variables. Two (2) variables are defined automatically on the creation of this validation:&#x20;
  * Variable Name: “Source\_UserName\_Acc\_customers”, Data Type: “UserName\_Acc\_customers” \
    The variable name is automatically defined by the convention {Source\_(target\_entity\_name)} and the data type is the entity itself. This variable contains the information to be validated and added to the entity as a source. For example, the API application sends the information of a record to the Validation pipeline, and that record sent to the Validation pipeline becomes the source.&#x20;
  * Variable Name: “Response”, Data Type: “JXPResponse”\
    This variable contains the response to be sent to the Action that attempted the creation or updating the Entity record, This response is in terms of success or failure of the Validation result.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validvartab.png" alt=""><figcaption></figcaption></figure>

* To define the Actions, go to the “Code” tab of the Validation pipeline. \
  On creating a new Validation pipeline, the system automatically creates all relevant Actions and their descriptions for all functions.

{% hint style="info" %}
During Validation pipeline execution, this section will be executed after initialization of the Variables.
{% endhint %}

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validcodetab.png" alt=""><figcaption></figcaption></figure>

* In the On Initialize function, a sequence of actions is already defined, which is executed when the validation is to be initialized. \
  The Response object (which is of type JXPResponse) is initialized with successful response details. (This means that if the validation did not encounter invalid data during validation, it will respond with success.) The On Initialize function looks as per the image below.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11codeoninitialize.png" alt=""><figcaption></figcaption></figure>

* The “On Validation step success” function is empty.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11onvalidstepssuccess.png" alt=""><figcaption></figcaption></figure>

* The “On Validation step fail” function is automatically configured to set the response status as “failed”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11onvalidstepsfail.png" alt=""><figcaption></figcaption></figure>

* The “On Error” function is automatically configured to set the response status as “failed”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validonerror.png" alt=""><figcaption></figcaption></figure>

* The “On Exit” function is automatically configured to send the Response.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validonexit.png" alt=""><figcaption></figcaption></figure>

* When the Code>On Initialize function is executed, then the Validation steps function is executed. Steps in Validation are similar to Action boxes in API and other templates.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11validsteps.png" alt=""><figcaption></figcaption></figure>

* The Validation pipeline consists of a sequence of Validation Steps. To create a Validation Step, click the \[+ Step] button in the Validation steps function.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11addvalidsteps.png" alt=""><figcaption></figcaption></figure>

* A Validation Step in a Validation pipeline is similar to an “Action box”. A Step has a description field and three (3) tabs: Criteria, Code, and Variables.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11step3tabs.png" alt=""><figcaption></figcaption></figure>

* Criteria tab: Users need to define a criteria in the Step. For each Step, at least one Criteria bar must exist in the Criteria tab to determine which section is to be executed in the Code tab.
* Code tab: The sequence of executing the functions is as follows:&#x20;
  * On Initialize function: the actions defined here are executed when the Step is initialized and before evaluating the criteria found in the Criteria tab and after all the variables defined in the step are initialized.&#x20;
  * On Validation step success function: the actions defined in this section are executed when the criteria in the Step is evaluated true.&#x20;
  * On Validation step fail function: the actions defined in this section are executed when the criteria in the Step is evaluated false.&#x20;
  * On Error function: the actions defined in this section are executed when an error is encountered during the execution of On Initialize, On Validation step success or On Validation step fail, e.g. when mandatory information is invalid.
* Variables: the variables defined in the Step>Code tab are initialized before any action or evaluation of the criteria is performed.&#x20;
* When any Langstack application that associates a Validation pipeline to a Target Entity and attempts to create or update an Entity record in it, the data of the Entity record is passed to the Validation pipeline and becomes the data source for the Validation pipeline. This data is stored in the form of an Entity variable type and is defined in the shared variables with the name Source\_{Entity type name}.&#x20;
* To exit Validation Steps, use the Exit validation Action which can exit with either true or false.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11exitvalidation.png" alt=""><figcaption></figcaption></figure>

* If the Validation pipeline returns false, the create or Update Entity record operation will not be performed.
* If the Validation pipeline returns true, this indicates a successful Validation pipeline status. Any changes to values of the entity record fields in the Validation pipeline will be included in the updated or created record. \
  The image below shows effects of the Validation Pipeline result on the entity record.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_11/ch11vpflow.png" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.langstack.com/welcome/get-started/learn-langstack/validation-pipeline.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
