Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Pass NancyContext to DataAnnotationValidation Adapters #1739

Merged
merged 1 commit into from
Nov 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public void Should_get_property_validators_from_factory()
{
// Given
var instance = new ModelUnderTest();
var context = new NancyContext();

// When
this.validator.Validate(instance, new NancyContext());
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.validatorFactory.GetValidators(typeof(ModelUnderTest))).MustHaveHappened();
Expand All @@ -53,43 +54,46 @@ public void Should_invoke_all_validators_returned_by_factory_with_instance_being
{
// Given
var instance = new ModelUnderTest();
var context = new NancyContext();

// When
this.validator.Validate(instance, new NancyContext());
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.propertyValidator1.Validate(instance)).MustHaveHappened();
A.CallTo(() => this.propertyValidator2.Validate(instance)).MustHaveHappened();
A.CallTo(() => this.propertyValidator1.Validate(instance, context)).MustHaveHappened();
A.CallTo(() => this.propertyValidator2.Validate(instance, context)).MustHaveHappened();
}

[Fact]
public void Should_invoke_validatable_object_adapter_with_instance_being_validated()
{
// Given
var instance = new ModelUnderTest();
var context = new NancyContext();

// When
this.validator.Validate(instance, new NancyContext());
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.validatableObjectAdapter.Validate(instance)).MustHaveHappened();
A.CallTo(() => this.validatableObjectAdapter.Validate(instance, context)).MustHaveHappened();
}

[Fact]
public void Should_contain_validation_results_from_all_validators()
{
// Given
var instance = new ModelUnderTest();
var context = new NancyContext();

var result1 = new ModelValidationError("Foo", string.Empty);
var result2 = new ModelValidationError("Bar", string.Empty);
var result3 = new ModelValidationError("Baz", string.Empty);

A.CallTo(() => this.propertyValidator1.Validate(instance)).Returns(new[] { result1 });
A.CallTo(() => this.propertyValidator2.Validate(instance)).Returns(new[] { result2, result3 });
A.CallTo(() => this.propertyValidator1.Validate(instance, context)).Returns(new[] { result1 });
A.CallTo(() => this.propertyValidator2.Validate(instance, context)).Returns(new[] { result2, result3 });

// When
var results = this.validator.Validate(instance, new NancyContext());
var results = this.validator.Validate(instance, context);

// Then
results.Errors.Count().ShouldEqual(3);
Expand All @@ -101,11 +105,12 @@ public void Should_contain_validation_result_from_validatable_object_adapter()
// Given
var instance = new ModelUnderTest();
var result = new ModelValidationError("Foo", string.Empty);
var context = new NancyContext();

A.CallTo(() => this.validatableObjectAdapter.Validate(instance)).Returns(new[] { result });
A.CallTo(() => this.validatableObjectAdapter.Validate(instance, context)).Returns(new[] { result });

// When
var results = this.validator.Validate(instance, new NancyContext());
var results = this.validator.Validate(instance, context);

// Then
results.Errors.Count().ShouldEqual(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Should_invoke_validate_on_instance()
var instance = new ModelUnderTest();

// When
this.validator.Validate(instance);
this.validator.Validate(instance, new NancyContext());

// Then
instance.ValidatedWasInvoked.ShouldBeTrue();
Expand All @@ -38,7 +38,7 @@ public void Should_invoke_validate_with_instance()
var instance = new ModelUnderTest();

// When
this.validator.Validate(instance);
this.validator.Validate(instance, new NancyContext());

// Then
instance.InstanceBeingValidated.ShouldBeSameAs(instance);
Expand All @@ -57,7 +57,7 @@ public void Should_return_validation_error_for_all_validation_results()
};

// When
var results = this.validator.Validate(instance);
var results = this.validator.Validate(instance, new NancyContext());

// Then
results.Count().ShouldEqual(2);
Expand All @@ -70,7 +70,7 @@ public void Should_not_return_errors_if_model_not_implements_IValidatableObject(
var instance = new ModelNotImplementingIValidatableObject();

// When
var result = this.validator.Validate(instance);
var result = this.validator.Validate(instance, new NancyContext());

// Then
result.Count().ShouldEqual(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PropertyValidatorFixture()
this.error1 =
new ModelValidationError("error1", string.Empty);

A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._))
A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._, A<NancyContext>._))
.Returns(new[] {this.error1});

this.adapter2 =
Expand All @@ -37,7 +37,7 @@ public PropertyValidatorFixture()
this.error2 =
new ModelValidationError("error2", string.Empty);

A.CallTo(() => this.adapter2.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._))
A.CallTo(() => this.adapter2.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._, A<NancyContext>._))
.Returns(new[] { this.error2 });

this.mappings =
Expand Down Expand Up @@ -65,61 +65,67 @@ public PropertyValidatorFixture()
public void Should_call_validate_on_each_validator_for_each_attribute_when_validate_is_invoked()
{
// Given
var context = new NancyContext();

// When
this.validator.Validate(null);
this.validator.Validate(null, context);

// Then
A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._)).MustHaveHappened();
A.CallTo(() => this.adapter2.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._)).MustHaveHappened();
A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._, A<NancyContext>._)).MustHaveHappened();
A.CallTo(() => this.adapter2.Validate(A<object>._, A<ValidationAttribute>._, A<PropertyDescriptor>._, A<NancyContext>._)).MustHaveHappened();
}

[Fact]
public void Should_pass_instance_to_validator_when_validate_is_invoked()
{
// Given
var instance = new Model();
var context = new NancyContext();

// When
this.validator.Validate(instance);
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.adapter1.Validate(instance, A<ValidationAttribute>._, A<PropertyDescriptor>._)).MustHaveHappened();
A.CallTo(() => this.adapter1.Validate(instance, A<ValidationAttribute>._, A<PropertyDescriptor>._, context)).MustHaveHappened();
}

[Fact]
public void Should_pass_attribute_to_validator_when_validate_is_invoked()
{
// Given
var instance = new Model();
var context = new NancyContext();

// When
this.validator.Validate(instance);
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.adapter1.Validate(A<object>._, this.mappings.Keys.First(), A<PropertyDescriptor>._)).MustHaveHappened();
A.CallTo(() => this.adapter1.Validate(A<object>._, this.mappings.Keys.First(), A<PropertyDescriptor>._, A<NancyContext>._)).MustHaveHappened();
}

[Fact]
public void Should_pass_descriptor_to_validator_when_validate_is_invoked()
{
// Given
var instance = new Model();
var context = new NancyContext();

// When
this.validator.Validate(instance);
this.validator.Validate(instance, context);

// Then
A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, this.descriptor)).MustHaveHappened();
A.CallTo(() => this.adapter1.Validate(A<object>._, A<ValidationAttribute>._, this.descriptor, A<NancyContext>._)).MustHaveHappened();
}

[Fact]
public void Should_return_an_aggregated_list_of_model_validation_errors_from_all_adapters()
{
// Given
var instance = new Model();
var context = new NancyContext();

// When
var results = this.validator.Validate(instance);
var results = this.validator.Validate(instance, context);

// Then
results.Contains(this.error1).ShouldBeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public ModelValidationResult Validate(object instance, NancyContext context)
foreach (var validator in this.validators)
{
var results =
validator.Validate(instance);
validator.Validate(instance, context);

errors.AddRange(results);
}

errors.AddRange(this.validatableObjectAdapter.Validate(instance));
errors.AddRange(this.validatableObjectAdapter.Validate(instance, context));

return new ModelValidationResult(errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public virtual IEnumerable<ModelValidationRule> GetRules(ValidationAttribute att
/// <param name="instance">The instance that should be validated.</param>
/// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
/// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor)
public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context)
{
var context =
var validationContext =
new ValidationContext(instance, null, null)
{
MemberName = descriptor == null ? null : descriptor.Name
Expand All @@ -62,7 +63,7 @@ public virtual IEnumerable<ModelValidationError> Validate(object instance, Valid
}

var result =
attribute.GetValidationResult(instance, context);
attribute.GetValidationResult(instance, validationContext);

if (result != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class DefaultValidatableObjectAdapter : IValidatableObjectAdapter
/// Validates the specified instance.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="context1"></param>
/// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="ModelValidationError"/> objects.</returns>
public IEnumerable<ModelValidationError> Validate(object instance)
public IEnumerable<ModelValidationError> Validate(object instance, NancyContext context1)
{
var validateable =
instance as IValidatableObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface IDataAnnotationsValidatorAdapter
/// <param name="instance">The instance that should be validated.</param>
/// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
/// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor);
IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context);
}
}
3 changes: 2 additions & 1 deletion src/Nancy.Validation.DataAnnotations/IPropertyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public interface IPropertyValidator
/// Gets the validation result for the specified <paramref name="instance"/>.
/// </summary>
/// <param name="instance">The instance that should be validated.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="ModelValidationError"/> objects.</returns>
IEnumerable<ModelValidationError> Validate(object instance);
IEnumerable<ModelValidationError> Validate(object instance, NancyContext context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public interface IValidatableObjectAdapter
/// Validates the given instance.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
IEnumerable<ModelValidationError> Validate(object instance);
IEnumerable<ModelValidationError> Validate(object instance, NancyContext context);
}
}
5 changes: 3 additions & 2 deletions src/Nancy.Validation.DataAnnotations/PropertyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public IEnumerable<ModelValidationRule> GetRules()
/// Gets the validation result for the specified <paramref name="instance"/>.
/// </summary>
/// <param name="instance">The instance that should be validated.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="ModelValidationError"/> objects.</returns>
public IEnumerable<ModelValidationError> Validate(object instance)
public IEnumerable<ModelValidationError> Validate(object instance, NancyContext context)
{
var errors =
new List<ModelValidationError>();
Expand All @@ -59,7 +60,7 @@ public IEnumerable<ModelValidationError> Validate(object instance)
foreach (var adapter in attributeAdapter.Value)
{
var results =
adapter.Validate(instance, attributeAdapter.Key, this.Descriptor);
adapter.Validate(instance, attributeAdapter.Key, this.Descriptor, context);

errors.AddRange(results);
}
Expand Down