Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to validate values

I have a class with many fields which represents different physical values.

class Tunnel
{
    private double _length;
    private double _crossSectionArea;
    private double _airDensity;
    //...

Each field is exposed using read/write property. I need to check on setter that the value is correct and generate exception otherwise. All validations are similar:

    public double Length
    {
        get { return _length; }
        set
        {
            if (value <= 0) throw new ArgumentOutOfRangeException("value",
                    "Length must be positive value.");
            _length = value;
        }
    }

    public double CrossSectionArea
    {
        get { return _crossSectionArea; }
        set
        {
            if (value <= 0) throw new ArgumentOutOfRangeException("value",
                    "Cross-section area must be positive value.");
            _crossSectionArea = value;
        }
    }

    public double AirDensity
    {
        get { return _airDensity; }
        set
        {
            if (value < 0) throw new ArgumentOutOfRangeException("value",
                    "Air density can't be negative value.");
            _airDensity = value;
        }
    }
    //...

Is there any elegant and flexible way to accomplish such validation?

like image 848
kyrylomyr Avatar asked Jul 12 '11 14:07

kyrylomyr


People also ask

What is the client-side validation?

Client-side validation is an initial check and an important feature of good user experience; by catching invalid data on the client-side, the user can fix it straight away.

How to validate inputs in HTML?

To validate the form using HTML, we will use HTML <input> required attribute. The <input> required attribute is a Boolean attribute that is used to specify the input element must be filled out before submitting the Form.


2 Answers

Assuming you want this sort of behaviour, you might consider some helper methods, e.g.

public static double ValidatePositive(double input, string name)
{
    if (input <= 0)
    {
        throw new ArgumentOutOfRangeException(name + " must be positive");
    }
    return input;
}

public static double ValidateNonNegative(double input, string name)
{
    if (input < 0)
    {
        throw new ArgumentOutOfRangeException(name + " must not be negative");
    }
    return input;
}

Then you can write:

public double AirDensity
{
    get { return _airDensity; }
    set
    {            
        _airDensity = ValidationHelpers.ValidateNonNegative(value,
                                                            "Air density");
    }
}

If you need this for various types, you could even make it generic:

public static T ValidateNonNegative(T input, string name)
    where T : IComparable<T>
{
    if (input.CompareTo(default(T)) < 0)
    {
        throw new ArgumentOutOfRangeException(name + " must not be negative");
    }
    return input;
}

Note that none of this is terribly i18n-friendly...

like image 121
Jon Skeet Avatar answered Oct 23 '22 15:10

Jon Skeet


All depends what technology you are using - if you're under MVC you can use Attributes, like this;

http://msdn.microsoft.com/en-us/library/ee256141(v=vs.98).aspx

like image 37
Mikey Hogarth Avatar answered Oct 23 '22 17:10

Mikey Hogarth