Vostok.Configuration
HomeQuickstartConcepts and basicsBasic scenarios
  • Home
  • Quickstart
  • Concepts and basics
    • Settings nodes
      • Value nodes
      • Array nodes
      • Object nodes
    • Settings nodes merging
    • Settings nodes scoping
    • Binding nodes to models
    • Configuration sources
    • Configuration provider
    • Caching and performance
    • Error handling
  • Modules
    • Abstractions
    • Configuration
    • Sources
    • Sources.CC
    • Sources.Json
    • Sources.Yaml
    • Sources.Xml
    • Sources.Vault
    • Logging
    • Microsoft
  • Sources
    • Constant sources
    • Object source
    • XML sources
    • YAML sources
    • JSON sources
    • Vault source
    • ClusterConfig source
    • Command line source
    • Environment variables source
  • Binders
    • Primitives
    • Collections
    • Classes and structs
    • Constructor injection
  • Basic scenarios
    • Assign sources to types
    • Obtain settings from provider
    • Observe settings via provider
    • Print settings
    • Log settings updates
    • Log errors
    • Combine sources
    • Scope sources
    • Make settings secret
    • Make settings required
  • Advanced scenarios
    • Use name aliases
    • Use dynamic interfaces
    • Use shared provider instance
    • Use value substitutions
    • Nest sources
    • Freeze sources
    • Transform sources
    • Create custom sources
    • Apply custom validators
    • Apply custom binders
    • Apply source data to existing object
    • Print contents of a source
Powered by GitBook
On this page
  • Constraints
  • Related pages

Was this helpful?

Export as PDF
  1. Advanced scenarios

Apply custom validators

PreviousCreate custom sourcesNextApply custom binders

Last updated 4 years ago

Was this helpful?

Requires: , (constraints).

Validation feature allows to associate a custom user-made validator with a settings type. Validation occurs during 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

abstractions module
main module
binding
Obtain settings from provider
Binding nodes to models