Permalink
Browse files

Merge pull request #844 from prabirshrestha/BindAndValidate

added BindAndValidate module extensions
  • Loading branch information...
2 parents 4e33292 + a719eb4 commit 1a7679e84e92c39dd546188cd617a96f8c870710 @thecodejunkie thecodejunkie committed Dec 20, 2012
Showing with 64 additions and 0 deletions.
  1. +64 −0 src/Nancy/ModelBinding/ModuleExtensions.cs
@@ -1,5 +1,7 @@
namespace Nancy.ModelBinding
{
+ using Nancy.Validation;
+
public static class ModuleExtensions
{
/// <summary>
@@ -38,6 +40,21 @@ public static TModel Bind<TModel>(this NancyModule module, params string[] black
}
/// <summary>
+ /// Bind the incoming request to a model and validate
+ /// </summary>
+ /// <typeparam name="TModel">Model type</typeparam>
+ /// <param name="module">Current module</param>
+ /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
+ /// <returns>Bound model instance</returns>
+ /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
+ public static TModel BindAndValidate<TModel>(this NancyModule module, params string[] blacklistedProperties)
+ {
+ var model = module.Bind<TModel>(blacklistedProperties);
+ module.Validate(model);
+ return model;
+ }
+
+ /// <summary>
/// Bind the incoming request to a model
/// </summary>
/// <typeparam name="TModel">Model type</typeparam>
@@ -51,6 +68,22 @@ public static TModel Bind<TModel>(this NancyModule module, BindingConfig configu
}
/// <summary>
+ /// Bind the incoming request to a model and validate
+ /// </summary>
+ /// <typeparam name="TModel">Model type</typeparam>
+ /// <param name="module">Current module</param>
+ /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
+ /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
+ /// <returns>Bound model instance</returns>
+ /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
+ public static TModel BindAndValidate<TModel>(this NancyModule module, BindingConfig configuration, params string[] blacklistedProperties)
+ {
+ var model = module.Bind<TModel>(configuration, blacklistedProperties);
+ module.Validate(model);
+ return model;
+ }
+
+ /// <summary>
/// Bind the incoming request to an existing instance
/// </summary>
/// <typeparam name="TModel">Model type</typeparam>
@@ -63,6 +96,21 @@ public static TModel BindTo<TModel>(this NancyModule module, TModel instance, pa
}
/// <summary>
+ /// Bind the incoming request to an existing instance and validate
+ /// </summary>
+ /// <typeparam name="TModel">Model type</typeparam>
+ /// <param name="module">Current module</param>
+ /// <param name="instance">The class instance to bind properties to</param>
+ /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
+ /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
+ public static TModel BindToAndValidate<TModel>(this NancyModule module, TModel instance, params string[] blacklistedProperties)
+ {
+ var model = module.BindTo<TModel>(instance, blacklistedProperties);
+ module.Validate(model);
+ return model;
+ }
+
+ /// <summary>
/// Bind the incoming request to an existing instance
/// </summary>
/// <typeparam name="TModel">Model type</typeparam>
@@ -77,5 +125,21 @@ public static TModel BindTo<TModel>(this NancyModule module, TModel instance, Bi
return adapter;
}
+
+ /// <summary>
+ /// Bind the incoming request to an existing instance and validate
+ /// </summary>
+ /// <typeparam name="TModel">Model type</typeparam>
+ /// <param name="module">Current module</param>
+ /// <param name="instance">The class instance to bind properties to</param>
+ /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
+ /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
+ /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
+ public static TModel BindToAndValidate<TModel>(this NancyModule module, TModel instance, BindingConfig configuration, params string[] blacklistedProperties)
+ {
+ var model = module.BindTo<TModel>(instance, configuration, blacklistedProperties);
+ module.Validate(model);
+ return model;
+ }
}
}

0 comments on commit 1a7679e

Please sign in to comment.