For example, if I'm not requesting for a Item, but a CreateItemDto, that does not have only the Id propertie, should I use DataAnnotations like [Required] in the Title propertie of the Item class? Cause it will be passed in CreatedItemDto, that uses [Required] data annotaiton.
Item Record:
public record Item
{
public Guid Id { get; init; }
public string? Title { get; init; }
}
CreateItemDto Record:
public record CreateItemDto
{
public Guid Id { get; init; }
[Required]
[Range(1, 20)]
public string? Title { get; init; }
}
In my controller, something like that would take it:
[HttpPost]
ActionResult<ItemDto> Create(CreateItemDto item)
{
var newItem = new Item() { Guid = Guid.NewGuid(), Title = item.Title };
return CreatedAtAction(nameof(Get), new { id = newItem.Id }, newItem );
}