public static IModelMetadataProvider CreateDefaultProvider(IList <IMetadataDetailsProvider> providers) { var detailsProviders = new List <IMetadataDetailsProvider>() { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider( new MvcOptions(), Options.Create(new MvcDataAnnotationsLocalizationOptions()), stringLocalizerFactory: null), new DataMemberRequiredBindingMetadataProvider(), }; MvcCoreMvcOptionsSetup.ConfigureAdditionalModelMetadataDetailsProviders(detailsProviders); detailsProviders.AddRange(providers); var validationProviders = TestModelValidatorProvider.CreateDefaultProvider(); detailsProviders.Add(new HasValidatorsValidationMetadataProvider(validationProviders.ValidatorProviders)); var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
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); }
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 bindingMetadataProvider = new DefaultBindingMetadataProvider(); var compositeProvider = new DefaultCompositeMetadataDetailsProvider(new[] { bindingMetadataProvider }); var optionsAccessor = new OptionsAccessor(); optionsAccessor.Value.ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor = (value, name) => $"Hmm, the value '{ value }' is not valid for { name }."; var provider = new DefaultModelMetadataProvider(compositeProvider, optionsAccessor); 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); }
public static IModelMetadataProvider CreateDefaultProvider() { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider() }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider)); }
private IModelMetadataProvider CreateModelMetadataProvider() { var metadataDetailsProvider = new DefaultCompositeMetadataDetailsProvider( new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DataAnnotationsMetadataProvider() } ); return(new DefaultModelMetadataProvider(metadataDetailsProvider)); }
public IModelMetadataProvider CreateModelMetadataProvider() { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider(new Mock <IOptions <MvcDataAnnotationsLocalizationOptions> >().Object, null) }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider)); }
public static IModelMetadataProvider CreateProvider(IList <IMetadataDetailsProvider> providers) { var detailsProviders = new List <IMetadataDetailsProvider>(); if (providers != null) { detailsProviders.AddRange(providers); } var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
// Creates a provider with all the defaults - includes data annotations public static IModelMetadataProvider CreateDefaultProvider() { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(CreateMessageProvider()), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider(), new DataMemberRequiredBindingMetadataProvider(), }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider)); }
/// <summary> /// Pieced this together from aspnet/Mvc github. See remarks for link. /// </summary> /// <remarks>Reference: https://github.com/aspnet/Mvc/blob/67a1f2dda9bcb5795033b0c0ce775d931627fe17/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/LabelTagHelperTest.cs#L156</remarks> protected ModelExpression GetModelExpression(DateTime?instance, string format = null) { var providers = new List <IMetadataDetailsProvider> { new DefaultMetaDataHelperProvider(format) }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(providers); var metadataProvider = new DefaultModelMetadataProvider(compositeDetailsProvider); var containerExplorer = metadataProvider.GetModelExplorerForType(typeof(DateTime?), instance); var modelExplorer = containerExplorer.GetExplorerForModel(instance); var modelExpression = new ModelExpression("", modelExplorer); return(modelExpression); }
public IModelMetadataProvider CreateModelMetadataProvider() { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider( Options.Create(new MvcDataAnnotationsLocalizationOptions()), null) }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
protected static ModelMetadataProvider CreateModelMetadataProvider(bool addHasValidatorsProvider) { var detailsProviders = new List <IMetadataDetailsProvider> { new DefaultValidationMetadataProvider(), }; if (addHasValidatorsProvider) { detailsProviders.Add(new HasValidatorsValidationMetadataProvider(ValidatorProviders)); } var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
// Creates a provider with all the defaults - includes data annotations public static ModelMetadataProvider CreateDefaultProvider(IStringLocalizerFactory stringLocalizerFactory = null) { var detailsProviders = new List <IMetadataDetailsProvider> { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), CreateDefaultDataAnnotationsProvider(stringLocalizerFactory), new DataMemberRequiredBindingMetadataProvider(), }; MvcCoreMvcOptionsSetup.ConfigureAdditionalModelMetadataDetailsProviders(detailsProviders); var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
public static IModelMetadataProvider CreateDefaultProvider(IList <IMetadataDetailsProvider> providers) { var detailsProviders = new List <IMetadataDetailsProvider>() { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider(), new DataMemberRequiredBindingMetadataProvider(), }; detailsProviders.AddRange(providers); var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, new TestOptionsManager <MvcOptions>())); }
// Creates a provider with all the defaults - includes data annotations public static IModelMetadataProvider CreateDefaultProvider(IStringLocalizerFactory stringLocalizerFactory = null) { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider( new TestOptionsManager <MvcDataAnnotationsLocalizationOptions>(), stringLocalizerFactory), new DataMemberRequiredBindingMetadataProvider(), }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, new TestOptionsManager <MvcOptions>())); }
public static IEnumerable <KeyValuePair <Type, ModelMetadata> > CreateMetadataForTypes(params Type[] types) { var provider = new EmptyModelMetadataProvider(); var detailsProvider = new DefaultCompositeMetadataDetailsProvider( Enumerable.Empty <IMetadataDetailsProvider>()); foreach (var type in types) { var key = ModelMetadataIdentity.ForType(type); var cache = new DefaultMetadataDetails(key, ModelAttributes.GetAttributesForType(type)); var metadata = new DefaultModelMetadata(provider, detailsProvider, cache); Xunit.Assert.Equal(type, metadata.ModelType); Xunit.Assert.Null(metadata.PropertyName); Xunit.Assert.Null(metadata.ContainerType); yield return(KeyValuePair.Create <Type, ModelMetadata>(type, metadata)); } }
private IModelMetadataProvider CreateModelMetadataProvider() { var metadataDetailsProvider = new DefaultCompositeMetadataDetailsProvider( new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(new ModelBindingMessageProvider { MissingBindRequiredValueAccessor = name => $"A value for the '{ name }' property was not provided.", MissingKeyOrValueAccessor = () => $"A value is required.", ValueMustNotBeNullAccessor = value => $"The value '{ value }' is invalid.", }), new DataAnnotationsMetadataProvider() } ); return(new DefaultModelMetadataProvider(metadataDetailsProvider)); }
public static IModelMetadataProvider CreateDefaultProvider(IList <IMetadataDetailsProvider> providers) { var detailsProviders = new List <IMetadataDetailsProvider>() { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider( Options.Create(new MvcDataAnnotationsLocalizationOptions()), stringLocalizerFactory: null), new DataMemberRequiredBindingMetadataProvider(), }; detailsProviders.AddRange(providers); var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
public IModelMetadataProvider CreateModelMetadataProvider() { var detailsProviders = new IMetadataDetailsProvider[] { new DefaultBindingMetadataProvider(), new DefaultValidationMetadataProvider(), new DataAnnotationsMetadataProvider( Options.Create(new MvcDataAnnotationsLocalizationOptions()), null), new BindingSourceMetadataProvider(typeof(CancellationToken), BindingSource.Special), new BindingSourceMetadataProvider(typeof(IFormFile), BindingSource.FormFile), new BindingSourceMetadataProvider(typeof(IFormFileCollection), BindingSource.FormFile), new BindingSourceMetadataProvider(typeof(IEnumerable <IFormFile>), BindingSource.FormFile) }; var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); return(new DefaultModelMetadataProvider(compositeDetailsProvider, Options.Create(new MvcOptions()))); }
public void GetFormattedWeek_ReturnsExpectedFormattedValue(int year, int month, int day, string expectedOutput) { // Arrange var detailsProvider = new DefaultCompositeMetadataDetailsProvider( Enumerable.Empty <IMetadataDetailsProvider>()); var key = ModelMetadataIdentity.ForType(typeof(DateTime)); var cache = new DefaultMetadataDetails(key, new ModelAttributes(new object[0], null, null)); var provider = new EmptyModelMetadataProvider(); var metadata = new DefaultModelMetadata(provider, detailsProvider, cache); var model = new DateTime(year, month, day); var modelExplorer = new ModelExplorer(provider, metadata, model); // Act var formattedValue = FormatWeekHelper.GetFormattedWeek(modelExplorer); // Assert Assert.Equal(expectedOutput, formattedValue); }