c# - How to get a list of the implement rules - FluentValidations -
using fluentvalidation webapi, attempting show rules implemented on property.
i using microsoft.aspnet.webapi.helppage assemblies generate pages api. able enumerate validation rules can display attributes required , length on pages.
previously, custom attribute following:
-- namespace myapi.areas.helppage.modeldescriptions private readonly idictionary<type, func<object, string>> annotationtextgenerator = new dictionary<type, func<object, string>> { { typeof(myattribute), => "my attribute documentation" } };
this add text required in additional information section of page. in event of using fluentvalidation, required type no longer set. rulefor(x => x.myproperty).notempty();
if helpful, code documenting annotations below:
-- namespace myapi.areas.helppage.modeldescriptions /// <summary> generates annotations. </summary> /// <param name="property"> property. </param> /// <param name="propertymodel"> property model. </param> private void generateannotations(memberinfo property, parameterdescription propertymodel) { var annotations = new list<parameterannotation>(); var attributes = property.getcustomattributes(); foreach (var attribute in attributes) { func<object, string> textgenerator; if (annotationtextgenerator.trygetvalue(attribute.gettype(), out textgenerator)) { annotations.add( new parameterannotation { annotationattribute = attribute, documentation = textgenerator(attribute) }); } } // rearrange annotations annotations.sort( (x, y) => { // special-case requiredattribute shows on top if (x.annotationattribute requiredattribute) { return -1; } if (y.annotationattribute requiredattribute) { return 1; } // sort rest based on alphabetic order of documentation return string.compare(x.documentation, y.documentation, stringcomparison.ordinalignorecase); }); foreach (var annotation in annotations) { propertymodel.annotations.add(annotation); } }
when calling generateannotations routine add logic @ property , fluent attributes assigned , add text.
maybe somehow use fluentvalidation.internal.propertyrules elegantly enumerate attributes?
Comments
Post a Comment