Skip to content

Commit 6091e9c

Browse files
committed
SPRNET-1234
1 parent 96a447f commit 6091e9c

18 files changed

+325
-183
lines changed

BreakingChanges-1.3.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Changes (1.2.0 to 1.2.1 or greater)
1+
Changes (1.2.0 to 1.3 or greater)
22
========================
33

44
Spring.Core
@@ -32,6 +32,8 @@ Spring.Core
3232

3333
4. AbstractApplicationContext.CaseSensitive renamed to AbstractApplicationContext.IsCaseSensitive
3434

35+
5. Spring.Validation: Base class BaseValidator changed to BaseSingleValidator for single validators as compared to group validators
36+
which now commonly derive from BaseGroupValidator instead of ValidatorGroup
3537

3638
Spring.Aop
3739
----------

src/Spring/Spring.Core/Spring.Core.2008.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,8 @@
10441044
<Compile Include="Util\StringUtils.cs">
10451045
<SubType>Code</SubType>
10461046
</Compile>
1047+
<Compile Include="Validation\BaseSimpleValidator.cs" />
1048+
<Compile Include="Validation\BaseValidatorGroup.cs" />
10471049
<Compile Include="Validation\Actions\ErrorMessageAction.cs" />
10481050
<Compile Include="Validation\Actions\ExpressionAction.cs" />
10491051
<Compile Include="Validation\AnyValidatorGroup.cs">

src/Spring/Spring.Core/Validation/AnyValidatorGroup.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region License
22

33
/*
4-
* Copyright 2002-2004 the original author or authors.
4+
* Copyright 2002-2009 the original author or authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
#endregion
2020

2121
using System.Collections;
22-
2322
using Spring.Expressions;
2423

2524
namespace Spring.Validation
@@ -37,9 +36,11 @@ namespace Spring.Validation
3736
/// for the contained validators, but only if this validator is not valid (meaning, when none
3837
/// of the contained validators are valid).
3938
/// </p>
39+
/// <p><b>Note</b>, that <see cref="BaseValidatorGroup.FastValidate"/> defaults to <c>true</c> for this validator type!</p>
4040
/// </remarks>
4141
/// <author>Aleksandar Seovic</author>
42-
public class AnyValidatorGroup : ValidatorGroup
42+
/// <author>Erich Eichinger</author>
43+
public class AnyValidatorGroup : BaseValidatorGroup
4344
{
4445
#region Constructors
4546

@@ -55,7 +56,8 @@ public AnyValidatorGroup()
5556
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
5657
/// </summary>
5758
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
58-
public AnyValidatorGroup(string when) : base(when)
59+
public AnyValidatorGroup(string when)
60+
: base(when)
5961
{
6062
this.FastValidate = true;
6163
}
@@ -64,7 +66,8 @@ public AnyValidatorGroup(string when) : base(when)
6466
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
6567
/// </summary>
6668
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
67-
public AnyValidatorGroup(IExpression when) : base(when)
69+
public AnyValidatorGroup(IExpression when)
70+
: base(when)
6871
{
6972
this.FastValidate = true;
7073
}
@@ -80,6 +83,7 @@ public AnyValidatorGroup(IExpression when) : base(when)
8083
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
8184
protected override bool ValidateGroup(IDictionary contextParams, IValidationErrors errors, object validationContext)
8285
{
86+
// capture errors in separate collection to only add them to the error collector in case of errors
8387
ValidationErrors tmpErrors = new ValidationErrors();
8488
bool valid = false;
8589
foreach (IValidator validator in Validators)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#region License
2+
3+
/*
4+
* Copyright © 2002-2009 the original author or authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#endregion
20+
21+
using System.Collections;
22+
using Spring.Expressions;
23+
24+
namespace Spring.Validation
25+
{
26+
/// <summary>
27+
/// Base class that defines common properties for all single validators.
28+
/// </summary>
29+
/// <remarks>
30+
/// <p>
31+
/// Custom single validators should always extend this class instead of
32+
/// simply implementing <see cref="IValidator"/> interface, in
33+
/// order to inherit common validator functionality.
34+
/// </p>
35+
/// </remarks>
36+
/// <author>Aleksandar Seovic</author>
37+
/// <author>Erich Eichinger</author>
38+
public abstract class BaseSimpleValidator : BaseValidator
39+
{
40+
private IExpression test;
41+
42+
/// <summary>
43+
/// Gets or sets the test expression.
44+
/// </summary>
45+
/// <value>The test expression.</value>
46+
public IExpression Test
47+
{
48+
get { return test; }
49+
set { test = value; }
50+
}
51+
52+
/// <summary>
53+
/// Creates a new instance of the validator without any <see cref="Test"/>
54+
/// and <see cref="BaseValidator.When"/> criteria
55+
/// </summary>
56+
public BaseSimpleValidator()
57+
{
58+
}
59+
60+
/// <summary>
61+
/// Creates a new instance of the <see cref="BaseValidator"/> class.
62+
/// </summary>
63+
/// <param name="test">The expression to validate.</param>
64+
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
65+
public BaseSimpleValidator(string test, string when)
66+
: base( when)
67+
{
68+
this.test = (test != null ? Expression.Parse(test) : null);
69+
}
70+
71+
/// <summary>
72+
/// Creates a new instance of the <see cref="BaseValidator"/> class.
73+
/// </summary>
74+
/// <param name="test">The expression to validate.</param>
75+
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
76+
public BaseSimpleValidator(IExpression test, IExpression when):base(when)
77+
{
78+
this.test = test;
79+
}
80+
81+
/// <summary>
82+
/// Validates the specified object.
83+
/// </summary>
84+
/// <param name="validationContext">The object to validate.</param>
85+
/// <param name="contextParams">Additional context parameters.</param>
86+
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
87+
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
88+
public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors)
89+
{
90+
bool valid = true;
91+
92+
if (EvaluateWhen(validationContext, contextParams))
93+
{
94+
valid = Validate(EvaluateTest(validationContext, contextParams));
95+
ProcessActions(valid, validationContext, contextParams, errors);
96+
}
97+
98+
return valid;
99+
}
100+
101+
/// <summary>
102+
/// Validates test object.
103+
/// </summary>
104+
/// <param name="objectToValidate">Object to validate.</param>
105+
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
106+
protected abstract bool Validate(object objectToValidate);
107+
108+
/// <summary>
109+
/// Evaluates test expression.
110+
/// </summary>
111+
/// <param name="rootContext">Root context to use for expression evaluation.</param>
112+
/// <param name="contextParams">Additional context parameters.</param>
113+
/// <returns>Result of the test expression evaluation, or validation context if test is <c>null</c>.</returns>
114+
protected object EvaluateTest(object rootContext, IDictionary contextParams)
115+
{
116+
if (Test == null)
117+
{
118+
return rootContext;
119+
}
120+
return Test.GetValue(rootContext, contextParams);
121+
}
122+
}
123+
}

src/Spring/Spring.Core/Validation/BaseValidator.cs

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region License
22

33
/*
4-
* Copyright © 2002-2005 the original author or authors.
4+
* Copyright © 2002-2009 the original author or authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -18,15 +18,10 @@
1818

1919
#endregion
2020

21-
#region Imports
22-
2321
using System;
2422
using System.Collections;
25-
2623
using Spring.Expressions;
2724

28-
#endregion
29-
3025
namespace Spring.Validation
3126
{
3227
/// <summary>
@@ -40,13 +35,13 @@ namespace Spring.Validation
4035
/// </p>
4136
/// </remarks>
4237
/// <author>Aleksandar Seovic</author>
38+
/// <author>Erich Eichinger</author>
4339
public abstract class BaseValidator : IValidator
4440
{
4541
#region Fields
4642

4743
private IList actions = new ArrayList();
4844

49-
private IExpression test;
5045
private IExpression when;
5146

5247
#endregion
@@ -62,37 +57,24 @@ public BaseValidator()
6257
/// <summary>
6358
/// Creates a new instance of the <see cref="BaseValidator"/> class.
6459
/// </summary>
65-
/// <param name="test">The expression to validate.</param>
6660
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
67-
public BaseValidator(string test, string when)
68-
: this((test != null ? Expression.Parse(test) : null), (when != null ? Expression.Parse(when) : null))
61+
public BaseValidator(string when)
62+
: this((when != null ? Expression.Parse(when) : null))
6963
{}
7064

7165
/// <summary>
7266
/// Creates a new instance of the <see cref="BaseValidator"/> class.
7367
/// </summary>
74-
/// <param name="test">The expression to validate.</param>
7568
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
76-
public BaseValidator(IExpression test, IExpression when)
69+
public BaseValidator(IExpression when)
7770
{
78-
this.test = test;
7971
this.when = when;
8072
}
8173

8274
#endregion
8375

8476
#region Properties
8577

86-
/// <summary>
87-
/// Gets or sets the test expression.
88-
/// </summary>
89-
/// <value>The test expression.</value>
90-
public IExpression Test
91-
{
92-
get { return test; }
93-
set { test = value; }
94-
}
95-
9678
/// <summary>
9779
/// Gets or sets the expression that determines if this validator should be evaluated.
9880
/// </summary>
@@ -133,43 +115,10 @@ public bool Validate(object validationContext, IValidationErrors errors)
133115
/// <param name="contextParams">Additional context parameters.</param>
134116
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
135117
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
136-
public virtual bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors)
137-
{
138-
bool valid = true;
139-
140-
if (EvaluateWhen(validationContext, contextParams))
141-
{
142-
valid = Validate(EvaluateTest(validationContext, contextParams));
143-
ProcessActions(valid, validationContext, contextParams, errors);
144-
}
145-
146-
return valid;
147-
}
148-
149-
/// <summary>
150-
/// Validates test object.
151-
/// </summary>
152-
/// <param name="objectToValidate">Object to validate.</param>
153-
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
154-
protected abstract bool Validate(object objectToValidate);
118+
public abstract bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors);
155119

156120
#region Helper Methods
157121

158-
/// <summary>
159-
/// Evaluates test expression.
160-
/// </summary>
161-
/// <param name="rootContext">Root context to use for expression evaluation.</param>
162-
/// <param name="contextParams">Additional context parameters.</param>
163-
/// <returns>Result of the test expression evaluation, or validation context if test is <c>null</c>.</returns>
164-
protected object EvaluateTest(object rootContext, IDictionary contextParams)
165-
{
166-
if (Test == null)
167-
{
168-
return rootContext;
169-
}
170-
return Test.GetValue(rootContext, contextParams);
171-
}
172-
173122
/// <summary>
174123
/// Evaluates when expression.
175124
/// </summary>

0 commit comments

Comments
 (0)