Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        public static void ValidateModel <T>(this NancyModule module, T input) where T : class
        {
            var validation = module.Validate(input);

            if (!validation.IsValid)
            {
                throw new BadRequestException(validation);
            }
        }
Exemplo n.º 4
0
        public static void ValidateModel <T>(this NancyModule module, T input) where T : class
        {
            var validation = module.Validate(input);

            if (validation.IsValid)
            {
                return;
            }

            throw new BadRequestException(validation.GetFlattenedErrors());
        }
Exemplo n.º 5
0
 private static void InternalValidate <TModel>(NancyModule module, TModel model)
 {
     if (model != null)
     {
         var modelValidationResult = module.Validate(model);
         if (!modelValidationResult.IsValid)
         {
             throw new ValidationServiceErrorException(modelValidationResult);
         }
     }
 }
        public static T BindAndValidate <T>(this NancyModule module) where T : ApiRequest
        {
            var request = module.Bind <T>();

            var requestValidation = module.Validate(request);

            if (requestValidation.IsValid == false)
            {
                throw new HypermediaEngineException(new BadRequestResponse(module.Context, requestValidation.Errors));
            }

            return(request);
        }