Skip to content

Commit f7ac17f

Browse files
Validation attributes (#65)
Add OpenAPI schema validations
1 parent f06d915 commit f7ac17f

20 files changed

+81
-20
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -50,8 +51,8 @@ public ActionResult<CategoryDto[]> GetAll()
5051
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5152
[HttpGet("GetCategoriesWithSkip")]
5253
public ActionResult<PagedResultDto<CategoryDto>> GetCategoriesWithSkip(
53-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
54-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5556
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5657
{
5758
try

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -50,8 +51,8 @@ public ActionResult<CustomerDto[]> GetAll()
5051
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5152
[HttpGet("GetCustomersWithSkip")]
5253
public ActionResult<PagedResultDto<CustomerDto>> GetCustomersWithSkip(
53-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
54-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5556
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5657
{
5758
try

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -68,8 +69,8 @@ public ActionResult<OrderDto[]> GetAllAuthorized()
6869
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
6970
[HttpGet("GetEmployeesWithSkip")]
7071
public ActionResult<PagedResultDto<EmployeeDto>> GetPagedEmployees(
71-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
72-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
72+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
73+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
7374
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
7475
{
7576
try

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -56,8 +57,8 @@ public ActionResult<OrderDto[]> GetAll()
5657
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5758
[HttpGet("GetPagedOrders")]
5859
public ActionResult<PagedResultDto<OrderDto>> GetAllOrders(
59-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
60-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
60+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
61+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
6162
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
6263
{
6364
try

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -70,8 +71,8 @@ public ActionResult<OrderDto[]> GetAllAuthorized()
7071
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
7172
[HttpGet("GetPagedProducts")]
7273
public ActionResult<PagedResultDto<ProductDto>> GetAllProducts(
73-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
74-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
74+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
75+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
7576
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
7677
{
7778
try
@@ -267,7 +268,7 @@ public ActionResult<ProductDto> Create(ProductDto model)
267268
}
268269
catch (InvalidOperationException exception)
269270
{
270-
return StatusCode(400, exception.Message);
271+
return StatusCode(400, exception.Message);
271272
}
272273
catch (Exception error)
273274
{

NorthwindCRUD/Controllers/RegionsController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -50,8 +51,8 @@ public ActionResult<RegionDto[]> GetAll()
5051
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5152
[HttpGet("GetPagedRegions")]
5253
public ActionResult<PagedResultDto<RegionDto>> GetAllRegions(
53-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
54-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5556
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5657
{
5758
try

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -50,8 +51,8 @@ public ActionResult<ShipperDto[]> GetAll()
5051
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5152
[HttpGet("GetPagedShippersWithSkip")]
5253
public ActionResult<PagedResultDto<ShipperDto>> GetPagedShippersWithSkip(
53-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
54-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5556
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5657
{
5758
try

NorthwindCRUD/Controllers/SuppliersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -50,8 +51,8 @@ public ActionResult<SupplierDto[]> GetAll()
5051
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5152
[HttpGet("GetPagedSuppliers")]
5253
public ActionResult<PagedResultDto<SupplierDto>> GetAllSuppliers(
53-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
54-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5556
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5657
{
5758
try

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -52,8 +53,8 @@ public ActionResult<TerritoryDto[]> GetAll()
5253
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5354
[HttpGet("GetPagedTerritories")]
5455
public ActionResult<PagedResultDto<TerritoryDto>> GetAllTerritories(
55-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
56-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
56+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
57+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5758
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5859
{
5960
try

NorthwindCRUD/Models/Dtos/AddressDto.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ namespace NorthwindCRUD.Models.Dtos
55
{
66
public class AddressDto : IAddress
77
{
8+
[StringLength(100, ErrorMessage = "Street cannot exceed 100 characters.")]
89
public string Street { get; set; }
910

11+
[StringLength(50, ErrorMessage = "City cannot exceed 50 characters.")]
1012
public string City { get; set; }
1113

14+
[StringLength(50, ErrorMessage = "Region cannot exceed 50 characters.")]
1215
public string Region { get; set; }
1316

17+
[StringLength(20, ErrorMessage = "Postal code cannot exceed 20 characters.")]
1418
public string PostalCode { get; set; }
1519

1620
[Required(ErrorMessage = "Country is required.")]
21+
[StringLength(50, ErrorMessage = "Country cannot exceed 50 characters.")]
1722
public string Country { get; set; }
1823

24+
[RegularExpression(@"^\+?[1-9]\d{1,14}$", ErrorMessage = "Phone number is not valid.")]
1925
public string? Phone { get; set; }
2026
}
2127
}

0 commit comments

Comments
 (0)