https://github.com/thenetworg/azure-functions-model-validator
https://github.com/thenetworg/azure-functions-model-validator
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/thenetworg/azure-functions-model-validator
- Owner: TheNetworg
- Created: 2018-12-11T12:27:55.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-13T22:34:09.000Z (over 7 years ago)
- Last Synced: 2025-02-16T00:28:49.040Z (over 1 year ago)
- Language: C#
- Size: 9.77 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Azure Functions model validator
Easy to use fluent API for validating modesl that arrived via HttpTrigger. Define what should happen when the model is ok. Optionally you can define on-failiure callback. The validator is also in Async version.
## Example
```cs
public class Request
{
[Required]
public string Name { get; set; }
}
//...
[FunctionName(nameof(Sync))]
public static IActionResult Sync(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
Request req,
ILogger log)
{
return AzureFunctionFluentValidator.Init
.OnSuccess(dto => new OkObjectResult($"Hello {dto.Name}"))
.OnFailure((dto, results) =>
{
results.ForEach(x => log.LogWarning(x.ErrorMessage));
})
.ValidateAndReturn(req);
}
[FunctionName(nameof(Async))]
public static Task Async(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
Request req,
ILogger log)
{
return AzureFunctionFluentAsyncValidator.Init
.OnSuccess(async dto =>
{
log.LogInformation($"Waiting for one second");
await Task.Delay(1000);
return new OkObjectResult($"Hello {dto.Name}");
})
.OnFailure(async (dto, results) =>
{
log.LogInformation($"Waiting for one second");
await Task.Delay(1000);
results.ForEach(x => log.LogWarning(x.ErrorMessage));
})
.ValidateAndReturnAsync(req);
}
```