Click here to Skip to main content
15,894,825 members
Articles / Web Development / ASP.NET

An Object Level Validation Framework

Rate me:
Please Sign up or sign in to vote.
4.94/5 (28 votes)
10 Nov 20065 min read 111.1K   492   85  
Provides a basic/advanced object level framework for validation.
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

namespace Cobalt.Validation.Tests
{
    [TestFixture]
    public class RangeValidatorTest
    {

        #region Integer Test

        private class IntegerTestClass : ValidatableBase
        {
            private int testProperty;

            [RangeValidator(5,10)]
            public int TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }
        }

        [Test]
        public void IntegerTest()
        {
            IntegerTestClass tc = new IntegerTestClass();

            Assert.IsFalse(tc.IsValid);

            tc.TestProperty = 4;

            Assert.IsFalse(tc.IsValid);

            tc.TestProperty = 5;

            Assert.IsTrue(tc.IsValid);

            tc.TestProperty = 9;

            Assert.IsTrue(tc.IsValid);

            tc.TestProperty = 10;

            Assert.IsTrue(tc.IsValid);

            tc.TestProperty = 12;

            Assert.IsFalse(tc.IsValid);
        }

        #endregion

        #region DateTime Test

        private class DateTimeTestClass : ValidatableBase
        {
            private DateTime testProperty;

            [RangeValidator("1/1/1980", "1/1/2000")]
            public DateTime TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }
        }

        [Test]
        public void DateTimeTest()
        {
            DateTimeTestClass tc = new DateTimeTestClass();

            Assert.IsFalse(tc.IsValid);

            tc.TestProperty = new DateTime(1979, 1, 1);

            Assert.IsFalse(tc.IsValid);

            tc.TestProperty = new DateTime(1980, 10, 11);

            Assert.IsTrue(tc.IsValid);

            tc.TestProperty = new DateTime(1999, 12, 31);

            Assert.IsTrue(tc.IsValid);

            tc.TestProperty = DateTime.Now;

            Assert.IsFalse(tc.IsValid);
        }

        #endregion

        #region NonIComparableTest Test

        private class NonIComparableTestClass : ValidatableBase
        {
            private Dummy testProperty;

            [RangeValidator(0,1)]
            public Dummy TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }

            public struct Dummy
            {
                public int f;

                public Dummy(int f)
                {
                    this.f = f;
                }
            }

        }

        [Test]
        [ExpectedException(typeof(InvalidValidatorException))]
        public void NonIComparableTest()
        {
            NonIComparableTestClass tc = new NonIComparableTestClass();

            bool isValid = tc.IsValid;
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I work at a financial services firm using mostly .NET. I enjoy my job immensely and love teaching, training, and mentoring younger developers.

Comments and Discussions