Exemplo n.º 1
0
        public void ModelStateDictionary_AddsCustomErrorMessage_WhenModelStateSet()
        {
            // Arrange
            var expected   = "Hmm, the value 'some value' is not valid for Length.";
            var dictionary = new ModelStateDictionary();

            dictionary.SetModelValue("key", new string[] { "some value" }, "some value");

            var messageProvider = new ModelBindingMessageProvider
            {
                MissingBindRequiredValueAccessor = name => "Unexpected MissingBindRequiredValueAccessor use",
                MissingKeyOrValueAccessor        = () => "Unexpected MissingKeyOrValueAccessor use",
                ValueMustNotBeNullAccessor       = value => "Unexpected ValueMustNotBeNullAccessor use",
                AttemptedValueIsInvalidAccessor  =
                    (value, name) => $"Hmm, the value '{ value }' is not valid for { name }.",
                UnknownValueIsInvalidAccessor = name => "Unexpected InvalidValueWithUnknownAttemptedValueAccessor use",
                ValueIsInvalidAccessor        = value => "Unexpected InvalidValueWithUnknownModelErrorAccessor use",
                ValueMustBeANumberAccessor    = name => "Unexpected ValueMustBeANumberAccessor use",
            };
            var bindingMetadataProvider = new DefaultBindingMetadataProvider(messageProvider);
            var compositeProvider       = new DefaultCompositeMetadataDetailsProvider(new[] { bindingMetadataProvider });
            var provider = new DefaultModelMetadataProvider(compositeProvider);
            var metadata = provider.GetMetadataForProperty(typeof(string), nameof(string.Length));

            // Act
            dictionary.TryAddModelError("key", new FormatException(), metadata);

            // Assert
            var entry = Assert.Single(dictionary);

            Assert.Equal("key", entry.Key);
            var error = Assert.Single(entry.Value.Errors);

            Assert.Equal(expected, error.ErrorMessage);
        }
Exemplo n.º 2
0
        public DefaultBindingMetadataProvider(ModelBindingMessageProvider messageProvider)
        {
            if (messageProvider == null)
            {
                throw new ArgumentNullException(nameof(messageProvider));
            }

            _messageProvider = messageProvider;
        }
Exemplo n.º 3
0
        public DefaultBindingMetadataProvider(ModelBindingMessageProvider messageProvider)
        {
            if (messageProvider == null)
            {
                throw new ArgumentNullException(nameof(messageProvider));
            }

            _messageProvider = messageProvider;
        }
 public ModelMessagesProvider(ModelBindingMessageProvider messages)
 {
     messages.AttemptedValueIsInvalidAccessor  = (value, field) => String.Format(Validations.InvalidField, field);
     messages.UnknownValueIsInvalidAccessor    = (field) => String.Format(Validations.InvalidField, field);
     messages.MissingBindRequiredValueAccessor = (field) => String.Format(Validations.Required, field);
     messages.ValueMustNotBeNullAccessor       = (field) => String.Format(Validations.Required, field);
     messages.ValueIsInvalidAccessor           = (value) => String.Format(Validations.InvalidValue, value);
     messages.ValueMustBeANumberAccessor       = (field) => String.Format(Validations.Numeric, field);
     messages.MissingKeyOrValueAccessor        = () => Validations.RequiredValue;
 }
 public ModelMessagesProvider(ModelBindingMessageProvider messages)
 {
     messages.AttemptedValueIsInvalidAccessor = (value, field) => String.Format(Validations.InvalidField, field);
     messages.UnknownValueIsInvalidAccessor = (field) => String.Format(Validations.InvalidField, field);
     messages.MissingBindRequiredValueAccessor = (field) => String.Format(Validations.Required, field);
     messages.ValueMustNotBeNullAccessor = (field) => String.Format(Validations.Required, field);
     messages.ValueIsInvalidAccessor = (value) => String.Format(Validations.InvalidValue, value);
     messages.ValueMustBeANumberAccessor = (field) => String.Format(Validations.Numeric, field);
     messages.MissingKeyOrValueAccessor = () => Validations.RequiredValue;
 }
Exemplo n.º 6
0
 public MvcOptions()
 {
     CacheProfiles                 = new Dictionary <string, CacheProfile>(StringComparer.OrdinalIgnoreCase);
     Conventions                   = new List <IApplicationModelConvention>();
     Filters                       = new FilterCollection();
     FormatterMappings             = new FormatterMappings();
     InputFormatters               = new FormatterCollection <IInputFormatter>();
     OutputFormatters              = new FormatterCollection <IOutputFormatter>();
     ModelBinderProviders          = new List <IModelBinderProvider>();
     ModelBindingMessageProvider   = new ModelBindingMessageProvider();
     ModelMetadataDetailsProviders = new List <IMetadataDetailsProvider>();
     ModelValidatorProviders       = new List <IModelValidatorProvider>();
     ValueProviderFactories        = new List <IValueProviderFactory>();
 }
Exemplo n.º 7
0
 public MvcOptions()
 {
     CacheProfiles = new Dictionary<string, CacheProfile>(StringComparer.OrdinalIgnoreCase);
     Conventions = new List<IApplicationModelConvention>();
     Filters = new FilterCollection();
     FormatterMappings = new FormatterMappings();
     InputFormatters = new FormatterCollection<IInputFormatter>();
     OutputFormatters = new FormatterCollection<IOutputFormatter>();
     ModelBinderProviders = new List<IModelBinderProvider>();
     ModelBindingMessageProvider = new ModelBindingMessageProvider();
     ModelMetadataDetailsProviders = new List<IMetadataDetailsProvider>();
     ModelValidatorProviders = new List<IModelValidatorProvider>();
     ValueProviderFactories = new List<IValueProviderFactory>();
 }
Exemplo n.º 8
0
 internal static void AddClientValidators(this DataSetMvcConfiguration config, ModelBindingMessageProvider messageProvider)
 {
     AddClientValidators(config,
                         new MaxLengthClientValidator(),
                         new NumericClientValidator(messageProvider),
                         new RegularExpressionClientValidator(),
                         new RequiredClientValidator(),
                         new StringLengthClientValidator());
 }
 public ModelMessagesProviderTests()
 {
     messages = new ModelBindingMessageProvider();
     new ModelMessagesProvider(messages);
 }
 public ModelMessagesProviderTests()
 {
     messages = new ModelBindingMessageProvider();
     new ModelMessagesProvider(messages);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of <see cref="NumericClientValidator"/> class.
 /// </summary>
 /// <param name="messageProvider">The message provider.</param>
 public NumericClientValidator(ModelBindingMessageProvider messageProvider)
 {
     _messageProvider = messageProvider ?? throw new ArgumentNullException();
 }
Exemplo n.º 12
0
        public void ModelStateDictionary_AddsCustomErrorMessage_WhenModelStateSet()
        {
            // Arrange
            var expected = "Hmm, the value 'some value' is not valid for Length.";
            var dictionary = new ModelStateDictionary();
            dictionary.SetModelValue("key", new string[] { "some value" }, "some value");

            var messageProvider = new ModelBindingMessageProvider
            {
                MissingBindRequiredValueAccessor = name => "Unexpected MissingBindRequiredValueAccessor use",
                MissingKeyOrValueAccessor = () => "Unexpected MissingKeyOrValueAccessor use",
                ValueMustNotBeNullAccessor = value => "Unexpected ValueMustNotBeNullAccessor use",
                AttemptedValueIsInvalidAccessor =
                    (value, name) => $"Hmm, the value '{ value }' is not valid for { name }.",
                UnknownValueIsInvalidAccessor = name => "Unexpected InvalidValueWithUnknownAttemptedValueAccessor use",
                ValueIsInvalidAccessor = value => "Unexpected InvalidValueWithUnknownModelErrorAccessor use",
                ValueMustBeANumberAccessor = name => "Unexpected ValueMustBeANumberAccessor use",
            };
            var bindingMetadataProvider = new DefaultBindingMetadataProvider(messageProvider);
            var compositeProvider = new DefaultCompositeMetadataDetailsProvider(new[] { bindingMetadataProvider });
            var provider = new DefaultModelMetadataProvider(compositeProvider);
            var metadata = provider.GetMetadataForProperty(typeof(string), nameof(string.Length));

            // Act
            dictionary.TryAddModelError("key", new FormatException(), metadata);

            // Assert
            var entry = Assert.Single(dictionary);
            Assert.Equal("key", entry.Key);
            var error = Assert.Single(entry.Value.Errors);
            Assert.Equal(expected, error.ErrorMessage);
        }