コード例 #1
0
        public IValidationContext Create(Func <IValidationContextOptions, IValidationContextOptions> options = null)
        {
            var newOptions = new ValidationContextOptions();

            var setOptionsFunction = options ?? (o => o);

            var finalOptions = setOptionsFunction(newOptions);

            if (!ReferenceEquals(newOptions, finalOptions))
            {
                throw new InvalidProcessedReferenceException(typeof(IValidationContextOptions));
            }

            return(new ValidationContext(finalOptions));
        }
コード例 #2
0
        public IValidationContext Clone(Func <IValidationContextOptions, IValidationContextOptions> modifyOptions = null)
        {
            var blankOptions = new ValidationContextOptions
            {
                ValidationOptions = OptionsService.GetVerifiedValidationOptions(Options.ValidationOptions)
            };

            var options = modifyOptions != null
                ? modifyOptions(blankOptions)
                : blankOptions;

            var finalOptions = OptionsService.GetMerged(Options, options);

            return(new ValidationContext(finalOptions));
        }
コード例 #3
0
            public void GetVerifiedValidationContextOptions_Should_ThrowException_When_InvalidValidationOptions(object validationOptions)
            {
                var specifications = new Dictionary <Type, object>
                {
                    { typeof(User), new Specification <User>(c => c) },
                    { typeof(Address), new Specification <Address>(c => c) }
                };

                var translations = new[]
                {
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1_" },
                        { "key2", "value2_" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1__" },
                        { "key2", "value2__" },
                        { "key3", "value3__" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key4", "value4__" }
                    }),
                    new Translation("t2", new Dictionary <string, string>
                    {
                        { "key5", "value5__" }
                    })
                };

                var options = new ValidationContextOptions
                {
                    Specifications    = specifications,
                    Translations      = translations,
                    ValidationOptions = (ValidationOptions)validationOptions
                };

                Assert.ThrowsAny <InvalidOperationException>(() => OptionsService.GetVerifiedValidationContextOptions(options));
            }
コード例 #4
0
            public void GetVerifiedValidationContextOptions_Should_ThrowException_When_DefaultTranslationNameDoesntExist()
            {
                var options = new ValidationContextOptions
                {
                    Specifications    = new Dictionary <Type, object>(),
                    Translations      = new List <Translation>(),
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.ArgumentNullException,
                        ValidationStrategy = ValidationStrategy.FailFast,
                        TranslationName    = "non_existing",
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                Assert.Throws <TranslationNotFoundException>(() => OptionsService.GetVerifiedValidationContextOptions(options));
            }
コード例 #5
0
            public void GetVerifiedValidationContextOptions_Should_Verify()
            {
                var specifications = new Dictionary <Type, object>
                {
                    { typeof(User), new Specification <User>(c => c) },
                    { typeof(Address), new Specification <Address>(c => c) }
                };

                var translations = new[]
                {
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1_" },
                        { "key2", "value2_" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key1", "value1__" },
                        { "key2", "value2__" },
                        { "key3", "value3__" }
                    }),
                    new Translation("t1", new Dictionary <string, string>
                    {
                        { "key4", "value4__" }
                    }),
                    new Translation("t2", new Dictionary <string, string>
                    {
                        { "key5", "value5__" }
                    })
                };

                var options = new ValidationContextOptions
                {
                    Specifications    = specifications,
                    Translations      = translations,
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.ArgumentNullException,
                        ValidationStrategy = ValidationStrategy.FailFast,
                        TranslationName    = "t1",
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                var result = OptionsService.GetVerifiedValidationContextOptions(options);

                Assert.NotNull(result);

                Assert.Equal(specifications.Count, result.Specifications.Count);

                Assert.Equal(specifications.ElementAt(0).Key, result.Specifications.ElementAt(0).Key);
                Assert.Equal(specifications.ElementAt(0).Value, result.Specifications.ElementAt(0).Value);
                Assert.Equal(specifications.ElementAt(1).Key, result.Specifications.ElementAt(1).Key);
                Assert.Equal(specifications.ElementAt(1).Value, result.Specifications.ElementAt(1).Value);

                var t1 = result.Translations.First(t => t.Name == "t1");

                Assert.Equal("value1__", t1.Dictionary["key1"]);
                Assert.Equal("value2__", t1.Dictionary["key2"]);
                Assert.Equal("value3__", t1.Dictionary["key3"]);
                Assert.Equal("value4__", t1.Dictionary["key4"]);

                var t2 = result.Translations.First(t => t.Name == "t2");

                Assert.Equal("value5__", t2.Dictionary["key5"]);

                Assert.Equal(ValidationStrategy.FailFast, result.ValidationOptions.ValidationStrategy);
                Assert.Equal("t1", result.ValidationOptions.TranslationName);
                Assert.Equal(NullRootStrategy.ArgumentNullException, result.ValidationOptions.NullRootStrategy);
            }
コード例 #6
0
            public void GetMerged_Should_GetMerged_When_TranslationExistOnlyInBased()
            {
                var baseOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key1", "value1__" },
                            { "key2", "value2__" },
                            { "key3", "value3__" }
                        }),
                        new Translation("t2", new Dictionary <string, string>
                        {
                            { "key1", "v1" },
                            { "key2", "v2" }
                        })
                    },
                    Specifications = new Dictionary <Type, object>
                    {
                        { typeof(User), new Specification <User>(c => c) }
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.NoErrors,
                        TranslationName    = "t1",
                        ValidationStrategy = ValidationStrategy.Force,
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                var newOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key2", "value2___" },
                            { "key3", "value3___" },
                            { "key4", "value4___" }
                        })
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.NoErrors,
                        TranslationName    = "t2",
                        ValidationStrategy = ValidationStrategy.Force,
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                var result = OptionsService.GetMerged(baseOptions, newOptions);

                Assert.NotNull(result);

                Assert.Equal(1, result.Specifications.Count);
                Assert.Equal(baseOptions.Specifications[typeof(User)], result.Specifications[typeof(User)]);

                var t1 = result.Translations.First(t => t.Name == "t1");

                Assert.Equal("value1__", t1.Dictionary["key1"]);
                Assert.Equal("value2___", t1.Dictionary["key2"]);
                Assert.Equal("value3___", t1.Dictionary["key3"]);
                Assert.Equal("value4___", t1.Dictionary["key4"]);

                Assert.Equal(NullRootStrategy.NoErrors, result.ValidationOptions.NullRootStrategy);
                Assert.Equal("t2", result.ValidationOptions.TranslationName);
                Assert.Equal(ValidationStrategy.Force, result.ValidationOptions.ValidationStrategy);
                Assert.Equal("*", result.ValidationOptions.CollectionForceKey);
                Assert.Equal(10, result.ValidationOptions.MaxDepth);
                Assert.Equal("Required", result.ValidationOptions.RequiredError.Message);
                Assert.Equal("Required", result.ValidationOptions.RequiredError.ToFormattedMessage());
            }
コード例 #7
0
            public void GetMerged_Should_GetMerged_InvertedFullExample()
            {
                var baseOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key2", "value2___" },
                            { "key3", "value3___" },
                            { "key4", "value4___" }
                        }),
                        new Translation("t2", new Dictionary <string, string>
                        {
                            { "key1", "v1" },
                            { "key2", "v2" }
                        })
                    },
                    Specifications = new Dictionary <Type, object>
                    {
                        { typeof(User), new Specification <User>(c => c) },
                        { typeof(Address), new Specification <Address>(c => c) }
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.NoErrors,
                        TranslationName    = "t2",
                        ValidationStrategy = ValidationStrategy.Force,
                        CollectionForceKey = "[]",
                        MaxDepth           = 5,
                        RequiredError      = new Error("Ultimately required")
                    }
                };

                var newOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key1", "value1__" },
                            { "key2", "value2__" },
                            { "key3", "value3__" }
                        })
                    },
                    Specifications = new Dictionary <Type, object>
                    {
                        { typeof(User), new Specification <User>(c => c) }
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.ArgumentNullException,
                        TranslationName    = "t1",
                        ValidationStrategy = ValidationStrategy.FailFast,
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                var result = OptionsService.GetMerged(baseOptions, newOptions);

                Assert.NotNull(result);

                Assert.Equal(2, result.Specifications.Count);

                Assert.Equal(newOptions.Specifications[typeof(User)], result.Specifications[typeof(User)]);
                Assert.Equal(baseOptions.Specifications[typeof(Address)], result.Specifications[typeof(Address)]);

                var t1 = result.Translations.First(t => t.Name == "t1");

                Assert.Equal("value1__", t1.Dictionary["key1"]);
                Assert.Equal("value2__", t1.Dictionary["key2"]);
                Assert.Equal("value3__", t1.Dictionary["key3"]);
                Assert.Equal("value4___", t1.Dictionary["key4"]);

                var t2 = result.Translations.First(t => t.Name == "t2");

                Assert.Equal("v1", t2.Dictionary["key1"]);
                Assert.Equal("v2", t2.Dictionary["key2"]);

                Assert.Equal(NullRootStrategy.ArgumentNullException, result.ValidationOptions.NullRootStrategy);
                Assert.Equal("t1", result.ValidationOptions.TranslationName);
                Assert.Equal(ValidationStrategy.FailFast, result.ValidationOptions.ValidationStrategy);
            }
コード例 #8
0
            public void GetMerged_Should_ThrowException_When_InvalidValidationOptions(object validationOptionsObject, bool inBaseOptions)
            {
                var validationOptions = (ValidationOptions)validationOptionsObject;

                var baseOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key1", "value1__" },
                            { "key2", "value2__" },
                            { "key3", "value3__" }
                        })
                    },
                    Specifications = new Dictionary <Type, object>
                    {
                        { typeof(User), new Specification <User>(c => c) }
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.ArgumentNullException,
                        TranslationName    = "t1",
                        ValidationStrategy = ValidationStrategy.FailFast,
                        CollectionForceKey = "*",
                        MaxDepth           = 10,
                        RequiredError      = new Error("Required")
                    }
                };

                var newOptions = new ValidationContextOptions
                {
                    Translations = new[]
                    {
                        new Translation("t1", new Dictionary <string, string>
                        {
                            { "key2", "value2___" },
                            { "key3", "value3___" },
                            { "key4", "value4___" }
                        }),
                        new Translation("t2", new Dictionary <string, string>
                        {
                            { "key1", "v1" },
                            { "key2", "v2" }
                        })
                    },
                    Specifications = new Dictionary <Type, object>
                    {
                        { typeof(User), new Specification <User>(c => c) },
                        { typeof(Address), new Specification <Address>(c => c) }
                    },
                    ValidationOptions = new ValidationOptions
                    {
                        NullRootStrategy   = NullRootStrategy.NoErrors,
                        TranslationName    = "t2",
                        ValidationStrategy = ValidationStrategy.Force,
                        CollectionForceKey = "[]",
                        MaxDepth           = 5,
                        RequiredError      = new Error("Ultimately required")
                    }
                };

                if (inBaseOptions)
                {
                    baseOptions.ValidationOptions = validationOptions;
                }
                else
                {
                    newOptions.ValidationOptions = validationOptions;
                }

                Assert.ThrowsAny <InvalidOperationException>(() => { OptionsService.GetMerged(baseOptions, newOptions); });
            }