/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Http/samples/MinimalValidationSample/Program.cs
99 строк
3 KB
Copilot
Move unified validation APIs to separate package (#62071)
13 июн 2025, 04:18
Не верифицирован
13 июн 2025, 04:18
1ce2228
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.ComponentModel.DataAnnotations; using Microsoft.Extensions.Validation; var builder = WebApplication.CreateBuilder(args); builder.Services.AddValidation(); var app = builder.Build(); // ValidationEndpointFilterFactory is implicitly enabled on all endpoints app.MapGet("/customers/{id}", ([Range(1, int.MaxValue)] int id) => $"Getting customer with ID: {id}"); app.MapPost("/customers", (Customer customer) => TypedResults.Created($"/customers/{customer.Name}", customer)); app.MapPost("/orders", (Order order) => TypedResults.Created($"/orders/{order.OrderId}", order)); app.MapPost("/products", ([EvenNumber(ErrorMessage = "Product ID must be even")] int productId, [Required] string name) => TypedResults.Ok(new { productId, name })) .DisableValidation(); app.Run(); // Define validatable types with the ValidatableType attribute #pragma warning disable ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. [ValidatableType] #pragma warning restore ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. public class Customer { [Required] public required string Name { get; set; } [EmailAddress] public required string Email { get; set; } [Range(18, 120)] [Display(Name = "Customer Age")] public int Age { get; set; } // Complex property with nested validation public Address HomeAddress { get; set; } = new Address { Street = "123 Main St", City = "Anytown", ZipCode = "12345" }; } public class Address { [Required] public required string Street { get; set; } [Required] public required string City { get; set; } [StringLength(5)] public required string ZipCode { get; set; } } // Define a type implementing IValidatableObject for custom validation public class Order : IValidatableObject { [Range(1, int.MaxValue)] public int OrderId { get; set; } [Required] public required string ProductName { get; set; } public int Quantity { get; set; } // Custom validation logic using IValidatableObject public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if (Quantity <= 0) { yield return new ValidationResult( "Quantity must be greater than zero", [nameof(Quantity)]); } } } // Use a custom validation attribute public class EvenNumberAttribute : ValidationAttribute { public override bool IsValid(object? value) { if (value is int number) { return number % 2 == 0; } return false; } }