4

I had to create a custom configuration section for my library. One of the parameters in the configuration should be positive double. However, for some reason I could not find any Validator for double.

For instance, here's how I use the integer validator:

[ConfigurationProperty("someProp", IsRequired = false, DefaultValue = 15)]
[IntegerValidator(MaxValue = 200, MinValue = 0)]
public int SomeProperty
{
   get
   {
      return (int)this["someProp"];
   }
   set
   {
      this["someProp"] = value;
   }
}

I've looked through Google and was not able to find any mention of DoubleValidator or FloatValidator. Is it possible to somehow make sure the property is a positive double? Are there any reasons DoubleValidator is not present in the System.Configuration? Maybe I am mistaken and double properties should not be stored in the config file.

5
  • Same question here, have you been able to sort it ? Commented Feb 26, 2016 at 12:31
  • Nope, unfortunately I haven't. Commented Feb 26, 2016 at 19:19
  • 1
    Have a look at my question here: stackoverflow.com/questions/39348791/… Commented Sep 8, 2016 at 8:08
  • @MortenToudahl Thanks, looks like it might be the thing I needed back then. I hardly need it anymore, but thank you anyway. Commented Sep 8, 2016 at 9:34
  • Yea, i was actually going to post it here as an answer. But then i ran into the default value problem :) Commented Sep 8, 2016 at 11:27

2 Answers 2

2

I needed a float validator for my own project, and found your question. This is how I made my validator. When/if you use it, you should remember to set a default value on the ConfigurationPropertyAttribute that you annotate the property with.

Thanks to Kasper Videbæk for finding my mistake: ConfigurationValidatorBase validate method receives default value

class DoubleValidator : ConfigurationValidatorBase
{
    public double MinValue { get; private set; }
    public double MaxValue { get; private set; }

    public DoubleValidator(double minValue, double maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
    }

    public override bool CanValidate(Type type)
    {
        return type == typeof(double);
    }

    public override void Validate(object obj)
    {
        double value;
        try
        {
            value = Convert.ToDouble(obj);
        }
        catch (Exception)
        {
            throw new ArgumentException();
        }

        if (value < MinValue)
        {
            throw new ConfigurationErrorsException($"Value too low, minimum value allowed: {MinValue}");
        }

        if (value > MaxValue)
        {
            throw new ConfigurationErrorsException($"Value too high, maximum value allowed: {MaxValue}");
        }
    }
}

The attribute to use on the configurationproperty

class DoubleValidatorAttribute : ConfigurationValidatorAttribute
{
    public double MinValue { get; set; }
    public double MaxValue { get; set; }

    public DoubleValidatorAttribute(double minValue, double maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
    }

    public override ConfigurationValidatorBase ValidatorInstance => new DoubleValidator(MinValue, MaxValue);
}
Sign up to request clarification or add additional context in comments.

Comments

-1

You could try RangeValidator. Something like

[Range(0,double.MaxValue, ErrorMessage = "Number must be positive. ")]
public float someProperty (...) { ...}

You can see this SO answer for more examples https://stackoverflow.com/a/17164247 .

1 Comment

The System.ComponentModel.DataAnnotations attributes are validation attributes for systems that support it. Not relevant at all to custom configuration sections. Remember that an attribute does nothing of itself: the code handling the attributed type does. And ConfigurationSectionHandler does nothing with the RangeAttribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.