Azure / azure-webjobs-sdk

Azure WebJobs SDK
MIT License
738 stars 358 forks source link

Validations in azure function #1848

Open rpundlik opened 6 years ago

rpundlik commented 6 years ago

Hi All,

We are trying to consume azure functions in web applications.

However, we were thinking of can we use the azure functions for validation purpose in web application like POCO validation with attributes, DB validation etc.

If yes then how can we use it.

Regards, Rohit Pundlik

grahamehorner commented 6 years ago

I'm using validation in azure functions; the model is sent to the function as json to validate via a http post or a queue; it is then deserialized into a POCO that has attributes and using the models static extension method of Validate it validates returning a json of the error messages

rpundlik commented 6 years ago

@grahamehorner Can you pls give me example of this ? I am looking this very badly and from long days.

Thanks for the help !

grahamehorner commented 6 years ago
        /// <summary>
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static List<ValidationResult> Validate(EntityType instance)
            => EntityType .Validate(instance, new ValidationContext(instance, null, null));

        /// <summary>
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static List<ValidationResult> Validate(EntityType instance, ValidationContext context)
        {
            var results = new List<ValidationResult>();
            Validator.TryValidateObject(instance, context, results, true);
            return results;
        }
rpundlik commented 6 years ago

@grahamehorner Thanks for the help ! But how did u called this? can u give me full example of this ? It will be really good for me !

rpundlik commented 6 years ago

We are calling web api from angular js and in web api we are having all the models. So basically we need to send the response to angularJS pages back after validation.

grahamehorner commented 6 years ago

@rpundlik you simply handle the json returned the same way that you would any other payload in the SPA framework of choice as the ValidationResult list is fully serialize, you dont need to do anythinv different because it is an azure function. However you do need to remember CORS

rpundlik commented 6 years ago

@grahamehorner So, as per my understanding I need to write validation logic in azure function and pass entire model/query string parameters from angular to azure function and return response.

grahamehorner commented 6 years ago

@rpundlik yes; the view model that your angular application uses can be sent to the azure function, as per any web service, the response from the azure function can be the same model with error notifications; and then your angular application simply uses the response as the updated view model.

Sorry for the delay in reply, as I’m currently on holiday with family; I’ll attempt to get a sample across on my return