diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/CompositeModelValidator.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/CompositeModelValidator.cs deleted file mode 100644 index 0f3355c093..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/CompositeModelValidator.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class CompositeModelValidator : IModelValidator - { - private readonly IEnumerable _validators; - - public CompositeModelValidator(IEnumerable validators) - { - _validators = validators; - } - - public bool IsRequired - { - get { return false; } - } - - public IEnumerable Validate(ModelValidationContext context) - { - var propertiesValid = true; - var metadata = context.ModelMetadata; - - foreach (var propertyMetadata in metadata.Properties) - { - var propertyContext = new ModelValidationContext(context, propertyMetadata); - - foreach (var propertyValidator in _validators) - { - foreach (var validationResult in propertyValidator.Validate(propertyContext)) - { - propertiesValid = false; - yield return CreateSubPropertyResult(propertyMetadata, validationResult); - } - } - } - - if (propertiesValid) - { - foreach (var typeValidator in _validators) - { - foreach (var typeResult in typeValidator.Validate(context)) - { - yield return typeResult; - } - } - } - } - - private static ModelValidationResult CreateSubPropertyResult(ModelMetadata propertyMetadata, - ModelValidationResult propertyResult) - { - return new ModelValidationResult(propertyMetadata.PropertyName + '.' + propertyResult.MemberName, - propertyResult.Message); - } - } -} diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ErrorModelValidator.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ErrorModelValidator.cs deleted file mode 100644 index 5ce180f1d3..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ErrorModelValidator.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - /// - /// A to represent an error. This validator will always throw an exception regardless - /// of the actual model value. - /// This is used to perform meta-validation - that is to verify the validation attributes make sense. - /// - public class ErrorModelValidator : IModelValidator - { - private readonly string _errorMessage; - - public ErrorModelValidator([NotNull] string errorMessage) - { - _errorMessage = errorMessage; - } - - public bool IsRequired - { - get { return false; } - } - - public IEnumerable Validate(ModelValidationContext context) - { - throw new InvalidOperationException(_errorMessage); - } - } -} diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/InvalidModelValidatorProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/InvalidModelValidatorProvider.cs deleted file mode 100644 index 1935a5543a..0000000000 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/InvalidModelValidatorProvider.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Reflection; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class InvalidModelValidatorProvider : AssociatedValidatorProvider - { - protected override IEnumerable GetValidators(ModelMetadata metadata, - IEnumerable attributes) - { - if (metadata.ContainerType == null || string.IsNullOrEmpty(metadata.PropertyName)) - { - // Validate that the type's fields and nonpublic properties don't have any validation attributes on - // them. Validation only runs against public properties - var type = metadata.ModelType; - var nonPublicProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); - foreach (var nonPublicProperty in nonPublicProperties) - { - if (nonPublicProperty.GetCustomAttributes(typeof(ValidationAttribute), inherit: true).Any()) - { - var message = Resources.FormatValidationAttributeOnNonPublicProperty(nonPublicProperty.Name, - type); - yield return new ErrorModelValidator(message); - } - } - - var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - var allFields = metadata.ModelType.GetFields(bindingFlags); - foreach (var field in allFields) - { - if (field.GetCustomAttributes(typeof(ValidationAttribute), inherit: true).Any()) - { - var message = Resources.FormatValidationAttributeOnField(field.Name, type); - yield return new ErrorModelValidator(message); - } - } - } - else - { - // Validate that value-typed properties marked as [Required] are also marked as - // [DataMember(IsRequired=true)]. Certain formatters may not recognize a member as required if it's - // marked as [Required] but not [DataMember(IsRequired=true)]. This is not a problem for reference - // types because [Required] will still cause a model error to be raised after a null value is - // deserialized. - if (metadata.ModelType.GetTypeInfo().IsValueType && - attributes.Any(attribute => attribute is RequiredAttribute)) - { - if (!DataMemberModelValidatorProvider.IsRequiredDataMember(metadata.ContainerType, attributes)) - { - var message = Resources.FormatMissingDataMemberIsRequired(metadata.PropertyName, - metadata.ContainerType); - yield return new ErrorModelValidator(message); - } - } - } - } - } -} diff --git a/src/Microsoft.AspNet.Mvc.Razor.Host/InjectDescriptor.cs b/src/Microsoft.AspNet.Mvc.Razor.Host/InjectDescriptor.cs deleted file mode 100644 index 36b7a566a9..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor.Host/InjectDescriptor.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.Razor.Host; - -namespace Microsoft.AspNet.Mvc.Razor -{ - /// - /// Represents information about an injected property. - /// - public class InjectDescriptor - { - public InjectDescriptor(string typeName, string memberName) - { - if (string.IsNullOrEmpty(typeName)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, "typeName"); - } - - if (string.IsNullOrEmpty(memberName)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, "memberName"); - } - - TypeName = typeName; - MemberName = memberName; - } - - /// - /// Gets the type name of the injected property - /// - public string TypeName { get; private set; } - - /// - /// Gets the name of the injected property. - /// - public string MemberName { get; private set; } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ErrorModelValidatorTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ErrorModelValidatorTest.cs deleted file mode 100644 index a4963c1412..0000000000 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ErrorModelValidatorTest.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class ErrorModelValidatorTest - { - private readonly DataAnnotationsModelMetadataProvider _metadataProvider = new DataAnnotationsModelMetadataProvider(); - - [Fact] - public void ValidateThrowsException() - { - // Arrange - var validator = new ErrorModelValidator("error"); - - // Act and Assert - ExceptionAssert.Throws(() => validator.Validate(null), "error"); - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/InvalidModelValidatorProviderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/InvalidModelValidatorProviderTest.cs deleted file mode 100644 index 880873e0bb..0000000000 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/InvalidModelValidatorProviderTest.cs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Runtime.Serialization; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.ModelBinding -{ - public class InvalidModelValidatorProviderTest - { - private static DataAnnotationsModelMetadataProvider _metadataProvider = new DataAnnotationsModelMetadataProvider(); - - [Fact] - public void GetValidatorsReturnsNothingForValidModel() - { - // Arrange - var validatorProvider = new InvalidModelValidatorProvider(); - - // Act - var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(ValidModel))); - - // Assert - Assert.Empty(validators); - } - - [Fact] - public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelType() - { - // Arrange - var name = typeof(InvalidModel).FullName; - var validatorProvider = new InvalidModelValidatorProvider(); - - // Act - var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(InvalidModel))); - - // Assert - Assert.Equal(2, validators.Count()); - ExceptionAssert.Throws(() => validators.ElementAt(0).Validate(null), - "Non-public property 'Internal' on type '" + name + "' is attributed with one or more validation attributes. Validation attributes on non-public properties are not supported. Consider using a public property for validation instead."); - ExceptionAssert.Throws(() => validators.ElementAt(1).Validate(null), - "Field 'Field' on type '" + name + "' is attributed with one or more validation attributes. Validation attributes on fields are not supported. Consider using a public property for validation instead."); - } - - [Fact] - public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelProperty() - { - // Arrange - var name = typeof(InvalidModel).FullName; - var validatorProvider = new InvalidModelValidatorProvider(); - - // Act - var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(InvalidModel), "Value")); - - // Assert - Assert.Equal(1, validators.Count()); - ExceptionAssert.Throws(() => validators.First().Validate(null), - "Property 'Value' on type '" + name + "' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)]."); - } - - [DataContract] - public class ValidModel - { - [Required] - [DataMember] - [StringLength(10)] - public string Ref { get; set; } - - [DataMember] - internal string Internal { get; set; } - - [Required] - [DataMember(IsRequired = true)] - public int Value { get; set; } - - public string Field; - } - - public class InvalidModel - { - [Required] - public string Ref { get; set; } - - [StringLength(10)] - [RegularExpression("pattern")] - internal string Internal { get; set; } - - [Required] - public int Value { get; set; } - - [StringLength(10)] - public string Field; - } - } -}