Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Rewrite of validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rynowak committed Sep 25, 2015
1 parent 0889b18 commit 8a502db
Show file tree
Hide file tree
Showing 64 changed files with 2,895 additions and 1,659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Microsoft.Framework.Internal;
using Microsoft.AspNet.Mvc.ModelBinding.Validation;

namespace Microsoft.AspNet.Mvc.ModelBinding
{
Expand Down Expand Up @@ -59,6 +60,8 @@ public static ModelBindingContext CreateBindingContext(
ModelState = modelState,
OperationBindingContext = operationBindingContext,
ValueProvider = operationBindingContext.ValueProvider,

ValidationState = new ValidationStateDictionary(),
};
}

Expand All @@ -74,6 +77,7 @@ public static ModelBindingContext CreateChildBindingContext(
ModelState = parent.ModelState,
OperationBindingContext = parent.OperationBindingContext,
ValueProvider = parent.ValueProvider,
ValidationState = parent.ValidationState,

Model = model,
ModelMetadata = modelMetadata,
Expand Down Expand Up @@ -174,5 +178,11 @@ public static ModelBindingContext CreateChildBindingContext(
/// is eligible for model binding.
/// </summary>
public Func<ModelBindingContext, string, bool> PropertyFilter { get; set; }

/// <summary>
/// Gets or sets the <see cref="ValidationStateDictionary"/>. Used for tracking validation state to
/// customize validation behavior for a model object.
/// </summary>
public ValidationStateDictionary ValidationState { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct ModelBindingResult : IEquatable<ModelBindingResult>
/// <returns>A <see cref="ModelBindingResult"/> representing a failed model binding operation.</returns>
public static ModelBindingResult Failed([NotNull] string key)
{
return new ModelBindingResult(key, model: null, isModelSet: false, validationNode: null);
return new ModelBindingResult(key, model: null, isModelSet: false);
}

/// <summary>
Expand All @@ -49,14 +49,12 @@ public static Task<ModelBindingResult> FailedAsync([NotNull] string key)
/// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <param name="model">The model value. May be <c>null.</c></param>
/// <param name="validationNode">The <see cref="ModelValidationNode"/>. May be <c>null</c>.</param>
/// <returns>A <see cref="ModelBindingResult"/> representing a successful model bind.</returns>
public static ModelBindingResult Success(
[NotNull] string key,
object model,
ModelValidationNode validationNode)
object model)
{
return new ModelBindingResult(key, model, isModelSet: true, validationNode: validationNode);
return new ModelBindingResult(key, model, isModelSet: true);
}

/// <summary>
Expand All @@ -65,22 +63,19 @@ public static ModelBindingResult Success(
/// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <param name="model">The model value. May be <c>null.</c></param>
/// <param name="validationNode">The <see cref="ModelValidationNode"/>. May be <c>null</c>.</param>
/// <returns>A completed <see cref="Task{ModelBindingResult}"/> representing a successful model bind.</returns>
public static Task<ModelBindingResult> SuccessAsync(
[NotNull] string key,
object model,
ModelValidationNode validationNode)
object model)
{
return Task.FromResult(Success(key, model, validationNode));
return Task.FromResult(Success(key, model));
}

private ModelBindingResult(string key, object model, bool isModelSet, ModelValidationNode validationNode)
private ModelBindingResult(string key, object model, bool isModelSet)
{
Key = key;
Model = model;
IsModelSet = isModelSet;
ValidationNode = validationNode;
}

/// <summary>
Expand Down Expand Up @@ -109,11 +104,6 @@ private ModelBindingResult(string key, object model, bool isModelSet, ModelValid
/// </summary>
public bool IsModelSet { get; }

/// <summary>
/// A <see cref="ModelValidationNode"/> associated with the current <see cref="ModelBindingResult"/>.
/// </summary>
public ModelValidationNode ValidationNode { get; }

/// <inheritdoc />
public override bool Equals(object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
using Microsoft.Framework.Internal;
Expand All @@ -14,6 +15,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// A metadata representation of a model type, property or parameter.
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
public abstract class ModelMetadata
{
private bool? _isComplexType;
Expand Down Expand Up @@ -407,5 +409,17 @@ public string GetDisplayName()
{
return DisplayName ?? PropertyName ?? ModelType.Name;
}

private string DebuggerToString()
{
if (Identity.MetadataKind == ModelMetadataKind.Type)
{
return $"ModelMetadata (Type: '{ModelType.Name}')";
}
else
{
return $"ModelMetadata (Property: '{ContainerType.Name}.{PropertyName}' Type: '{ModelType.Name}')";
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. 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.Validation
{
/// <summary>
/// Defines a strategy for enumerating the child entries of a model object which should be validated.
/// </summary>
public interface IValidationStrategy
{
/// <summary>
/// Gets an <see cref="IEnumerator{ValidationEntry}"/> containing a <see cref="ValidationEntry"/> for
/// each child entry of the model object to be validated.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> associated with <paramref name="model"/>.</param>
/// <param name="key">The model prefix associated with <paramref name="model"/>.</param>
/// <param name="model">The model object.</param>
/// <returns>An <see cref="IEnumerator{ValidationEntry}"/>.</returns>
IEnumerator<ValidationEntry> GetChildren(ModelMetadata metadata, string key, object model);
}
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
/// <summary>
/// A context object for <see cref="IModelValidator"/>.
/// </summary>
public class ModelValidationContext
{
public ModelValidationContext(
[NotNull] ModelBindingContext bindingContext,
[NotNull] ModelExplorer modelExplorer)
: this(
bindingContext.BindingSource,
bindingContext.OperationBindingContext.ValidatorProvider,
bindingContext.ModelState,
modelExplorer)
{
}

public ModelValidationContext(
BindingSource bindingSource,
[NotNull] IModelValidatorProvider validatorProvider,
[NotNull] ModelStateDictionary modelState,
[NotNull] ModelExplorer modelExplorer)
{
ModelState = modelState;
ValidatorProvider = validatorProvider;
ModelExplorer = modelExplorer;
BindingSource = bindingSource;
}

/// <summary>
/// Constructs a new instance of the <see cref="ModelValidationContext"/> class using the
/// <paramref name="parentContext" /> and <paramref name="modelExplorer"/>.
/// Gets or sets the model object.
/// </summary>
/// <param name="parentContext">Existing <see cref="ModelValidationContext"/>.</param>
/// <param name="modelExplorer">
/// <see cref="ModelExplorer"/> associated with the new <see cref="ModelValidationContext"/>.
/// </param>
/// <returns>
/// A new instance of the <see cref="ModelValidationContext"/> class using the
/// <paramref name="parentContext" /> and <paramref name="modelExplorer"/>.
/// </returns>
public static ModelValidationContext GetChildValidationContext(
[NotNull] ModelValidationContext parentContext,
[NotNull] ModelExplorer modelExplorer)
{
return new ModelValidationContext(
modelExplorer.Metadata.BindingSource,
parentContext.ValidatorProvider,
parentContext.ModelState,
modelExplorer);
}

public ModelExplorer ModelExplorer { get; }

public ModelStateDictionary ModelState { get; }
public object Model { get; set; }

public BindingSource BindingSource { get; set; }
/// <summary>
/// Gets or sets the model container object.
/// </summary>
public object Container { get; set; }

public IModelValidatorProvider ValidatorProvider { get; }
/// <summary>
/// Gets or sets the <see cref="ModelMetadata"/> associated with <see cref="Model"/>.
/// </summary>
public ModelMetadata Metadata { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
/// <summary>
/// Contains data needed for validating a child entry of a model object. See <see cref="IValidationStrategy"/>.
/// </summary>
public struct ValidationEntry
{
/// <summary>
/// Creates a new <see cref="ValidationEntry"/>.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> associated with <paramref name="model"/>.</param>
/// <param name="key">The model prefix associated with <paramref name="model"/>.</param>
/// <param name="model">The model object.</param>
public ValidationEntry(ModelMetadata metadata, string key, object model)
{
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
}

if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

Metadata = metadata;
Key = key;
Model = model;
}

/// <summary>
/// The model prefix associated with <see cref="Model"/>.
/// </summary>
public string Key { get; }

/// <summary>
/// The <see cref="ModelMetadata"/> associated with <see cref="Model"/>.
/// </summary>
public ModelMetadata Metadata { get; }

/// <summary>
/// The model object.
/// </summary>
public object Model { get; }
}
}
Loading

0 comments on commit 8a502db

Please sign in to comment.