> For the complete documentation index, see [llms.txt](https://vostok.gitbook.io/configuration/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vostok.gitbook.io/configuration/advanced-scenarios/apply-custom-validators.md).

# Apply custom validators

**Requires**: [abstractions module](/configuration/modules/abstractions.md), [main module](/configuration/modules/configuration.md) (constraints).

Validation feature allows to associate a custom user-made validator with a settings type. Validation occurs during [binding](/configuration/concepts-and-basics/binding-nodes-to-models.md) and results in binding errors for incorrect settings.

```
[ValidateBy(typeof(MySettingsValidator))]
class MySettings
{
    int CacheCapacity { get; }
}
```

```
class MySettingsValidator: ISettingsValidator<MySettings>
{
    // Returns validation errors. Empty enumerable == success.
    public IEnumerable<string> Validate(MySettings settings)
    {
        if (settings.CacheCapacity <= 0)
            yield return "Cache capacity must be positive.";
    }
}
```

### Constraints

There are also built-in validation constraints you can use to create a custom validator. Just inherit your validator class from `ConstraintsValidator` and override a method returning constraints to be checked:

```
[ValidateBy(typeof(TestConfigValidator))]
class TestConfig
{
    public int Number;
    public string String;
}
```

```
class TestConfigValidator : ConstraintsValidator<TestConfig>
{
    protected override IEnumerable<Constraint<TestConfig>> GetConstraints()
    {
        yield return new NotNullOrWhitespaceConstraint<TestConfig>(settings => settings.String);
        yield return new RangeConstraint<TestConfig, int>(settings => settings.Number, 2, 10);
    }
}
```

Here's a list of all currently implemented constraint types:

* `NotNullConstraint` for arbitrary reference types;
* `NotNullOrEmptyConstraint` for strings;
* `NotNullOrWhitespaceConstraint` for strings;
* `RangeConstraint`, `LessConstraint`, `LessOrEqualConstraint`, `GreaterConstraint` and `GreaterOrEqualConstraint` for any types that implement `IComparable`;
* `UniqueConstraint` to check that a set of field/properties only contains unique values;

### Related pages

{% content-ref url="/pages/-M6UVVWxjimC0yLfXc\_P" %}
[Obtain settings from provider](/configuration/basic-scenarios/obtain-settings-from-provider.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M6UQlGKGPuVt1P3lsHf" %}
[Binding nodes to models](/configuration/concepts-and-basics/binding-nodes-to-models.md)
{% endcontent-ref %}
