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
Grouped validation rules per property #1314
Merged
grumpydev
merged 2 commits into
NancyFx:master
from
thecodejunkie:modelvalidationdescriptor-improvements
Nov 7, 2013
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
namespace Nancy.Validation | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
|
@@ -10,24 +11,31 @@ public class CompositeValidator : IModelValidator | |
{ | ||
private readonly IEnumerable<IModelValidator> validators; | ||
|
||
/// <summary> | ||
/// Gets the description of the validator. | ||
/// </summary> | ||
public ModelValidationDescriptor Description { get; private set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CompositeValidator"/> class. | ||
/// </summary> | ||
/// <param name="validators">The validators.</param> | ||
public CompositeValidator(IEnumerable<IModelValidator> validators) | ||
/// <param name="modelType">The type of the model that is being validated.</param> | ||
public CompositeValidator(IEnumerable<IModelValidator> validators, Type modelType) | ||
{ | ||
var modelValidators = | ||
validators.ToArray(); | ||
|
||
this.Description = CreateCompositeDescription(modelValidators); | ||
this.ModelType = modelType; | ||
this.Description = CreateCompositeDescription(modelValidators, modelType); | ||
this.validators = modelValidators; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the description of the validator. | ||
/// </summary> | ||
public ModelValidationDescriptor Description { get; private set; } | ||
|
||
/// <summary> | ||
/// The type of the model that is being validated by the validator. | ||
/// </summary> | ||
public Type ModelType { get; private set; } | ||
|
||
/// <summary> | ||
/// Validates the specified instance. | ||
/// </summary> | ||
|
@@ -42,17 +50,18 @@ public ModelValidationResult Validate(object instance, NancyContext context) | |
.SelectMany(r => r.Errors) | ||
.ToArray(); | ||
|
||
return !errors.Any() ? | ||
return (!errors.Any()) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's with the superfluous brackets? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the way it's (usually) done in our code base |
||
ModelValidationResult.Valid : | ||
new ModelValidationResult(errors); | ||
} | ||
|
||
private static ModelValidationDescriptor CreateCompositeDescription(IEnumerable<IModelValidator> validators) | ||
private static ModelValidationDescriptor CreateCompositeDescription(IEnumerable<IModelValidator> validators, Type modelType) | ||
{ | ||
var rules = | ||
validators.SelectMany(v => v.Description.Rules); | ||
var rules = validators | ||
.SelectMany(v => v.Description.Rules) | ||
.ToDictionary(x => x.Key, x => x.Value); | ||
|
||
return new ModelValidationDescriptor(rules); | ||
return new ModelValidationDescriptor(rules, modelType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pff.. call yourself the human stylecop with xml comments like that? ;)