This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DataAnnotationsValidator.cs
75 lines (64 loc) · 3.11 KB
/
DataAnnotationsValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace Nancy.Validation.DataAnnotations
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The default Data Annotations implementation of <see cref="IModelValidator"/>.
/// </summary>
public class DataAnnotationsValidator : IModelValidator
{
private ModelValidationDescriptor descriptor;
private readonly IValidatableObjectAdapter validatableObjectAdapter;
private readonly IEnumerable<IPropertyValidator> validators;
/// <summary>
/// Initializes a new instance of the <see cref="DataAnnotationsValidator"/> class.
/// </summary>
/// <param name="typeForValidation">The type for validation.</param>
/// <param name="factory">The <see cref="IPropertyValidatorFactory"/> instance that should be used by the validator.</param>
/// <param name="validatableObjectAdapter">The <see cref="validatableObjectAdapter"/> instance that should be used by the validator.</param>
public DataAnnotationsValidator(Type typeForValidation, IPropertyValidatorFactory factory, IValidatableObjectAdapter validatableObjectAdapter)
{
this.ModelType = typeForValidation;
this.validatableObjectAdapter = validatableObjectAdapter;
this.validators = factory.GetValidators(typeForValidation);
}
/// <summary>
/// Gets the description of the validator.
/// </summary>
/// <returns>An <see cref="ModelValidationDescriptor"/> instance.</returns>
public ModelValidationDescriptor Description
{
get { return this.descriptor ?? (this.descriptor = GetModelValidationDescriptor()); }
}
/// <summary>
/// Gets the <see cref="System.Type"/> of the model that is being validated by the validator.
/// </summary>
public Type ModelType { get; private set; }
/// <summary>
/// Validates the specified 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>A <see cref="ModelValidationResult"/> with the result of the validation.</returns>
public ModelValidationResult Validate(object instance, NancyContext context)
{
var errors =
new List<ModelValidationError>();
foreach (var validator in this.validators)
{
var results =
validator.Validate(instance, context);
errors.AddRange(results);
}
errors.AddRange(this.validatableObjectAdapter.Validate(instance, context));
return new ModelValidationResult(errors);
}
private ModelValidationDescriptor GetModelValidationDescriptor()
{
var rules =
this.validators.SelectMany(x => x.GetRules());
return new ModelValidationDescriptor(rules, this.ModelType);
}
}
}