forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluentValidationModelMetadataProvider.cs
More file actions
276 lines (224 loc) · 9.95 KB
/
FluentValidationModelMetadataProvider.cs
File metadata and controls
276 lines (224 loc) · 9.95 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#region License
// Copyright (c) Jeremy Skinner (http://www.jeremyskinner.co.uk)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion
using ServiceStack.FluentValidation;
using ServiceStack.FluentValidation.Resources;
using ServiceStack.FluentValidation.Results;
using ServiceStack.FluentValidation.Validators;
namespace FluentValidation.Mvc {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
public class FluentValidationModelMetadataProvider : DataAnnotationsModelMetadataProvider {
readonly IValidatorFactory factory;
public FluentValidationModelMetadataProvider(IValidatorFactory factory) {
this.factory = factory;
}
protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, PropertyDescriptor propertyDescriptor) {
var attributes = ConvertFVMetaDataToAttributes(containerType, propertyDescriptor.Name);
return CreateMetadata(attributes, containerType, modelAccessor, propertyDescriptor.PropertyType, propertyDescriptor.Name);
}
IEnumerable<Attribute> ConvertFVMetaDataToAttributes(Type type, string name) {
var validator = factory.GetValidator(type);
if (validator == null) {
return Enumerable.Empty<Attribute>();
}
IEnumerable<IPropertyValidator> validators;
// if (name == null) {
//validators = validator.CreateDescriptor().GetMembersWithValidators().SelectMany(x => x);
// validators = Enumerable.Empty<IPropertyValidator>();
// }
// else {
validators = validator.CreateDescriptor().GetValidatorsForMember(name);
// }
var attributes = validators.OfType<IAttributeMetadataValidator>()
.Select(x => x.ToAttribute())
.Concat(SpecialCaseValidatorConversions(validators));
return attributes.ToList();
}
IEnumerable<Attribute> SpecialCaseValidatorConversions(IEnumerable<IPropertyValidator> validators) {
//Email Validator should be convertible to DataType EmailAddress.
var emailValidators = validators
.OfType<IEmailValidator>()
.Select(x => new DataTypeAttribute(DataType.EmailAddress))
.Cast<Attribute>();
var requiredValidators = validators.OfType<INotNullValidator>().Cast<IPropertyValidator>()
.Concat(validators.OfType<INotEmptyValidator>().Cast<IPropertyValidator>())
.Select(x => new RequiredAttribute())
.Cast<Attribute>();
return requiredValidators.Concat(emailValidators);
}
/*IEnumerable<Attribute> ConvertFVMetaDataToAttributes(Type type) {
return ConvertFVMetaDataToAttributes(type, null);
}*/
/*public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType) {
var attributes = ConvertFVMetaDataToAttributes(modelType);
return CreateMetadata(attributes, null /* containerType ?1?, modelAccessor, modelType, null /* propertyName ?1?);
}*/
}
public interface IAttributeMetadataValidator : IPropertyValidator
{
Attribute ToAttribute();
}
internal class AttributeMetadataValidator : IAttributeMetadataValidator
{
readonly Attribute attribute;
public AttributeMetadataValidator(Attribute attributeConverter)
{
attribute = attributeConverter;
}
public IStringSource ErrorMessageSource
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public string ErrorCode
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context)
{
return Enumerable.Empty<ValidationFailure>();
}
public string ErrorMessageTemplate
{
get { return null; }
set { }
}
public ICollection<Func<object, object>> CustomMessageFormatArguments
{
get { return null; }
}
public bool SupportsStandaloneValidation
{
get { return false; }
}
public Func<object, object> CustomStateProvider
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public Attribute ToAttribute()
{
return attribute;
}
}
}
namespace FluentValidation.Mvc.MetadataExtensions
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public static class MetadataExtensions
{
public static IRuleBuilder<T, TProperty> HiddenInput<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new HiddenInputAttribute()));
}
public static IRuleBuilder<T, TProperty> HiddenInput<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, bool displayValue)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new HiddenInputAttribute { DisplayValue = displayValue }));
}
public static IRuleBuilder<T, TProperty> UIHint<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, string hint)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new UIHintAttribute(hint)));
}
public static IRuleBuilder<T, TProperty> UIHint<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, string hint, string presentationLayer)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new UIHintAttribute(hint, presentationLayer)));
}
public static IRuleBuilder<T, TProperty> Scaffold<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, bool scaffold)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new ScaffoldColumnAttribute(scaffold)));
}
public static IRuleBuilder<T, TProperty> DataType<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, DataType dataType)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new DataTypeAttribute(dataType)));
}
public static IRuleBuilder<T, TProperty> DataType<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, string customDataType)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new DataTypeAttribute(customDataType)));
}
public static IRuleBuilder<T, TProperty> DisplayName<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, string name)
{
#if NET4
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new DisplayAttribute { Name = name }));
#else
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new DisplayNameAttribute(name)));
#endif
}
public static IDisplayFormatBuilder<T, TProperty> DisplayFormat<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder)
{
return new DisplayFormatBuilder<T, TProperty>(ruleBuilder);
}
public static IRuleBuilder<T, TProperty> ReadOnly<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, bool readOnly)
{
return ruleBuilder.SetValidator(new AttributeMetadataValidator(new ReadOnlyAttribute(readOnly)));
}
public interface IDisplayFormatBuilder<T, TProperty> : IRuleBuilder<T, TProperty>
{
IDisplayFormatBuilder<T, TProperty> NullDisplayText(string text);
IDisplayFormatBuilder<T, TProperty> DataFormatString(string text);
IDisplayFormatBuilder<T, TProperty> ApplyFormatInEditMode(bool apply);
IDisplayFormatBuilder<T, TProperty> ConvertEmptyStringToNull(bool convert);
}
private class DisplayFormatBuilder<T, TProperty> : IDisplayFormatBuilder<T, TProperty>
{
readonly IRuleBuilder<T, TProperty> builder;
readonly DisplayFormatAttribute attribute = new DisplayFormatAttribute();
public DisplayFormatBuilder(IRuleBuilder<T, TProperty> builder)
{
this.builder = builder;
builder.SetValidator(new AttributeMetadataValidator(attribute));
}
public IRuleBuilderOptions<T, TProperty> SetValidator(IPropertyValidator validator)
{
return builder.SetValidator(validator);
}
public IRuleBuilderOptions<T, TProperty> SetValidator(IValidator<TProperty> validator)
{
return builder.SetValidator(validator);
}
public IDisplayFormatBuilder<T, TProperty> NullDisplayText(string text)
{
attribute.NullDisplayText = text;
return this;
}
public IDisplayFormatBuilder<T, TProperty> DataFormatString(string text)
{
attribute.DataFormatString = text;
return this;
}
public IDisplayFormatBuilder<T, TProperty> ApplyFormatInEditMode(bool apply)
{
attribute.ApplyFormatInEditMode = apply;
return this;
}
public IDisplayFormatBuilder<T, TProperty> ConvertEmptyStringToNull(bool convert)
{
attribute.ConvertEmptyStringToNull = convert;
return this;
}
}
}
}