Azure function validate request with Fluent validation & Ignore null handling

by Admin 11. November 2019 06:10
 
[FunctionName(nameof(Potal))]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "v1/potal")]
          HttpRequest request)
        {
            var jsonSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };
            var validator = new PotalRequestValidator();
            var json = await request.ReadAsStringAsync();
            var potalRequest = JsonConvert.DeserializeObject<PotalRequest>(json, jsonSettings);

            var validationResult = validator.Validate(potalRequest);

            if (!validationResult.IsValid)
            {
                return new BadRequestObjectResult(validationResult.Errors.Select(e => new
                {
                    Field = e.PropertyName,
                    Error = e.ErrorMessage
                }));
            }

            return await RunAsync(request);
        }
 

Tags:

Comments are closed

TextBox

Tag cloud

    RecentPosts