> For the complete documentation index, see [llms.txt](https://rockon-games.gitbook.io/metaforge-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rockon-games.gitbook.io/metaforge-documentation/readme/validators.md).

# Validators

## Overview

Validators in MetaForge are used to ensure that model object properties meet certain criteria before being saved or applied. By using validators, developers can add custom validation logic to properties, ensuring data integrity and preventing invalid configurations in the Unreal Engine editor. Validators help in maintaining a consistent and error-free editing experience.

## Defining Validators

To use validators, you can annotate your model object properties using the `Validator` metadata tag. This tag allows you to specify a function that will be called to validate the property's value. The function must be defined within your class, must be of the form `bool FunctionName(FName ContextId)`, and must be marked as a `UFUNCTION` to be accessible. The new value to test against can be retrieved from the validator system using the context ID. The type of the parameter is based on the property being validated.

If the property uses an `SNameProperty` drawer, it will be an `FName`, for example:

```cpp
FName newValue = Validator::GetContextParameter<FName>(ContextId);
```

If the property uses an `SIntPropertyDrawer`, the value will be an `int32`, for example:

```cpp
int32 newValue = Validator::GetContextParameter<int32>(ContextId);
```

Carefully check the type of the property you are validating when using this.

### Example Code

Here is an example of using validators in a model object:

```cpp
#pragma once

#include "CoreMinimal.h"
#include "UDataModel.h"
#include "UMyModelObject.generated.h"

#if WITH_EDITOR
#include "Metadata/Validator.h"
#endif

UCLASS()
class UMyModelObject : public UObject, public IDataModel
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere, Category = "MyModel", meta = (Validator = "ValidateAdvancedSettingValue", Component = "SFloatProperty"))
    float AdvancedSettingValue;

    UFUNCTION()
    bool ValidateAdvancedSettingValue(FName ContextId)
    {
        #if WITH_EDITOR
        // Ensure that the value is greater than or equal to 0
        float newValue = Validator::GetContextParameter<float>(ContextId);

        if (newValue < 0) {
            ShowErrorNotification("Value must be greater than zero.");
            return false;
        }
        #endif
        return true;
    }
};
```

In this example:

* **`AdvancedSettingValue`** is a property that requires validation. It is annotated with the `Validator` tag, specifying the function `ValidateAdvancedSettingValue`.
* **`ValidateAdvancedSettingValue()`** is a function that checks if `AdvancedSettingValue` is non-negative. This function ensures that only valid values are assigned to `AdvancedSettingValue`.

## Use Cases for Validators

* **Value Range Enforcement**: Ensure that numeric properties fall within an acceptable range, such as positive values or within specific bounds.
* **Dependency Checks**: Validate a property based on the value of another property, ensuring that configurations are consistent.
* **Custom Validation Logic**: Apply any custom validation logic to enforce project-specific rules and requirements.

## Best Practices for Validators

* **Keep Validation Simple**: Ensure that validation functions are straightforward and perform minimal computation to avoid slowing down the editor.
* **Use Clear Error Messages**: When using validators, provide clear messages to the user to indicate why a value is invalid and how to correct it.
* **Avoid Circular Dependencies**: Be careful not to create circular dependencies between properties when defining validators, as this can lead to unexpected behaviors.

## Summary

Validators are a powerful tool in MetaForge for ensuring that properties adhere to specific rules before they are saved or applied. By adding validators to your model objects, you can maintain data integrity, prevent invalid configurations, and provide users with a more robust and user-friendly editor experience.
