Skip to content

Options Validation: Support DataAnnotations #3385

Closed
@HaoK

Description

@HaoK

From Experience review with @ajcvickers @DamianEdwards @Eilon @davidfowl :

We should support DataAnnotations on the POCO for 2.2:

for example:

public class MyOptions 
{
        [Required]
        public string Key { get; set; }
   
        [Range(18,99, ErrorMessage="Age should be between 18 and 99")] 
        public int Age { get; set; } 
 
        [DataType(DataType.PhoneNumber)] 
        [Phone] 
        Public string PhoneNumber { get; set; } 
 
        [DataType(DataType.EmailAddress)] 
        [EmailAddress] 
        Public string Email { get; set; } 
}

Need to figure out how to opt in, via new default overload?
services.AddOptions<MyOptions>.[Auto]Validate()

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedenhancementThis issue represents an ask for new feature or an enhancement to an existing one

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions