Created
April 10, 2023 01:24
-
-
Save renatogroffe/f7fd5f730cf7742a793e03ffabb23806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
using System.Runtime.InteropServices; | |
using System.Text.Json; | |
using ConsoleAppAllowedValues.Models; | |
Console.WriteLine("***** Testes com .NET 8 | AllowedValuesAttribute *****"); | |
Console.WriteLine($"Versao do .NET em uso: {RuntimeInformation | |
.FrameworkDescription} - Ambiente: {Environment.MachineName} - Kernel: {Environment | |
.OSVersion.VersionString}"); | |
var estados = new Estado[] | |
{ | |
new() {CodRegiao = "SE", Nome = "Sao Paulo"}, | |
new() {CodRegiao = "NE", Nome = "Bahia"}, | |
new() {CodRegiao = "CO", Nome = "Mato Grosso"}, | |
new() {CodRegiao = "L", Nome = "Teste com Regiao invalida"}, | |
new() {CodRegiao = "S", Nome = "Rio Grande do Sul"}, | |
new() {CodRegiao = "N", Nome = "Para"} | |
}; | |
foreach (var estado in estados) | |
{ | |
Console.WriteLine(); | |
Console.WriteLine(JsonSerializer.Serialize(estado)); | |
var validationResults = new List<ValidationResult>(); | |
if (!Validator.TryValidateObject(estado, new ValidationContext(estado), | |
validationResults, validateAllProperties: true)) | |
{ | |
Console.WriteLine("Dados invalidos para essa instancia..."); | |
foreach (var validationResult in validationResults) | |
Console.WriteLine($"ErrorMessage = {validationResult.ErrorMessage}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment