C
C#3y ago
Hulkstance

❔ Data Annotations Configuration Validation

Pretty self explanatory, how do I validate the configuration I'm getting from the AWS section based on Data Annotations?
public static IServiceCollection AddDynamoDb(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);

services.AddSingleton<IAmazonDynamoDB>(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();

// TODO: Validate
var dbConfiguration = configuration.GetSection("AWS:DynamoDB").Get<DynamoDbConfiguration>();

if (dbConfiguration is null)
{
throw new ArgumentNullException(nameof(dbConfiguration), "Please configure DynamoDB");
}

return new AmazonDynamoDBClient(
new BasicAWSCredentials(dbConfiguration.AccessKey, dbConfiguration.SecretKey),
new AmazonDynamoDBConfig
{
ServiceURL = dbConfiguration.ServiceUrl,
AuthenticationRegion = dbConfiguration.Region
});
});

return services;
}

public class DynamoDbConfiguration
{
[Required]
public required string AccessKey { get; init; }

[Required]
public required string SecretKey { get; init; }

[Required]
[RegularExpression(@"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)")]
public required string ServiceUrl { get; init; }

[Required]
[RegularExpression(@"(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d+")]
public required string Region { get; init; }
}
public static IServiceCollection AddDynamoDb(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);

services.AddSingleton<IAmazonDynamoDB>(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();

// TODO: Validate
var dbConfiguration = configuration.GetSection("AWS:DynamoDB").Get<DynamoDbConfiguration>();

if (dbConfiguration is null)
{
throw new ArgumentNullException(nameof(dbConfiguration), "Please configure DynamoDB");
}

return new AmazonDynamoDBClient(
new BasicAWSCredentials(dbConfiguration.AccessKey, dbConfiguration.SecretKey),
new AmazonDynamoDBConfig
{
ServiceURL = dbConfiguration.ServiceUrl,
AuthenticationRegion = dbConfiguration.Region
});
});

return services;
}

public class DynamoDbConfiguration
{
[Required]
public required string AccessKey { get; init; }

[Required]
public required string SecretKey { get; init; }

[Required]
[RegularExpression(@"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)")]
public required string ServiceUrl { get; init; }

[Required]
[RegularExpression(@"(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d+")]
public required string Region { get; init; }
}
4 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
do you really need both [Required] and required?
Hulkstance
HulkstanceOP3y ago
@Ero as far as I understand required (from .NET 7) is compile-time check whereas [Required] is runtime @Divi, I think its more like
// Microsoft.Extensions.Options.DataAnnotations
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOptions<DynamoDbConfiguration>()
.Bind(builder.Configuration.GetSection("AWS:DynamoDB"))
.ValidateDataAnnotations()
.ValidateOnStart();
// Microsoft.Extensions.Options.DataAnnotations
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOptions<DynamoDbConfiguration>()
.Bind(builder.Configuration.GetSection("AWS:DynamoDB"))
.ValidateDataAnnotations()
.ValidateOnStart();
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server