示例#1
0
        /// <summary>
        /// Performs the validation of this configuration object.
        /// Returns empty list if no problems found, otherwise list contains validation problems.
        /// </summary>
        public IEnumerable <ConfigurationValidationItem> Validate()
        {
            // Helper to collect and perform validations
            var validations = new ConfigurationValidationCollector <SampleConfig>(this);

            // Here are validations
            validations.ValidateNotZero(c => c.SomeValue, "Configuration should not contain default value (=0).");
            validations.ValidateNotNullOrEmpty(c => c.SomeName, "Configuration should specify value for Name.");
            validations.ValidateUri(c => c.SomeEndpoint, "External API endpoint is incorrect");
            validations.ValidateEmail(c => c.SomeEmail, "E-mail address is wrong.");
            validations.ValidateIpV4Address(c => c.SomeIp, "IP address is not valid.");
            validations.ValidatePublicIpV4Address(c => c.SomeIp, "IP address is not a public IP address.");

            // Generic methods, expecting boolean outcome of Linq expression
            validations.ValidateMust(c => c.SomeEndpoint.StartsWith("https", StringComparison.OrdinalIgnoreCase), nameof(this.SomeEndpoint), "Enpoint is no SSL secured.");
            validations.ValidateMust(c => c.SomeEndpoint.EndsWith("/", StringComparison.OrdinalIgnoreCase), nameof(this.SomeEndpoint), "Enpoint should end with shash /.");
            validations.ValidateMust(c =>
                                     c.SomeName.Contains("sparta", StringComparison.OrdinalIgnoreCase) &&
                                     c.SomeValue > 10,
                                     $"{nameof(this.SomeName)} and {nameof(this.SomeValue)}",
                                     "Combined validations failed.");

            // Syntactic sugar
            validations.ValidateStartsWith(c => c.SomeEndpoint, "https", "Enpoint is no SSL secured.");
            validations.ValidateEndsWith(c => c.SomeEndpoint, "/", "Enpoint should end in slash /.");

            // Returning all found validation problems
            return(validations.Result);
        }
        public void Url_ValidVariations_Validates(string ep)
        {
            var cfg = new TestConfig {
                SomeEndpoint = ep
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateUri(c => c.SomeEndpoint, "Not valid URL!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void Url_InvalidVariations_Validates(string ep)
        {
            var cfg = new TestConfig {
                SomeEndpoint = ep
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateUri(c => c.SomeEndpoint, "Not valid URL!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEndpoint");
            coll.Result[0].ConfigurationValue.Should().Be(ep);
            coll.Result[0].ValidationMessage.Should().Be("Not valid URL!");
        }