C
C#Core

Can different validation attributed be applied to a field based on the active profile?

Hello, In Development I would need to apply a different validation attribute to a DTO field, without impacting performance. For example, having an extra if statement in the validation that checks the current profile and applies different validation is not the greatest idea . Gemini gave vague solution, like this:
c#
#if DEBUG
// Code specific to development environment (e.g., relaxed validation)
#else
// Code specific to production environment (e.g., stricter validation)
#endif
c#
#if DEBUG
// Code specific to development environment (e.g., relaxed validation)
#else
// Code specific to production environment (e.g., stricter validation)
#endif
Can the following be done?
c#
#if DEVELOPMENT
[DevelopmentIsValidHostnameFormat]
#else
[IsValidHostnameFormat]
#endif

public string? Hostname{ get; set; }
c#
#if DEVELOPMENT
[DevelopmentIsValidHostnameFormat]
#else
[IsValidHostnameFormat]
#endif

public string? Hostname{ get; set; }
P
Pobiega42d ago
Yes, that works its not very pretty, but it works. the preprocessor runs before anything else
C
Core42d ago
Thank you, as I see there is no DEVELOPMENT
No description
C
Core42d ago
only DEBUG is it the same?
P
Pobiega42d ago
No. DEBUG is the debug profile in Vs/rider Development is an environment name, and is not per compilation but per execution (via env var) And that can not be used for conditional compilation like this You'll need to do it with code instead if you want to support it at runtime
C
Core42d ago
so if it is not compiled within an IDE, will the else statement be applied?
P
Pobiega42d ago
Sure, if you specified the profile dotnet build -c Debug In most setups, that's the default profile Oh sorry, misread your question It doesn't matter if you use an IDE or not, DEBUG is true if the Debug configuration profile was used at compilation
C
Core42d ago
Thank you, now I got it
Want results from more Discord servers?
Add your server
More Posts
Android Emulatorhow to fix this guys please help me.AS2 over http C# Send message with signature to mendelson server.Hi guys) I am new in this channel. Can you please help me with AS2 over http PKCS#7 .p7s file) Yeah.Persistent error with dotnet cli and installing maui workspaceI've tried time and again now without joy. There is precious little info on this issue out there (onneed tips to improve my logical skills and .net c# skills as I'm a intermediate dev.Hi,I want to improve my logical and .net c# skills so i want please suggest me or guide me about to Issue with Azure AD B2C Custom Domains: Unexpected Redirect Behavior and Domain DependencyHey everyone, I'm hopeful that there are experienced individuals with Azure AD B2C expertise who canNode editor, many operations - how to make it maintainableHi, I'm trying to develop an app with node editor that ideally will provide no code openCV functionacannot close window...Release date of 8.0.3?Hey, I have been struggling with an issue that from what I understand will be solved in 8.0.3. httpRedirecting the native standard output streamI need to call into native code the problem is that it writes some of it's output using stdout. Is iWhere can I find a working library for making a LLVM compiler?I found this library (https://github.com/dotnet/LLVMSharp) that provides extern references for the LCheck value type of extended class instanceI have `BaseClass` and `MyClass : BaseClass`. Using `val is BaseClass` will return true for any obj✅ Am I using NumberFormatInfo properly?I have a list of ca. 400 numbers which I want to convert to percentage strings as part of an endpoin✅ .NET MAUI LayoutI am trying to create a password field with the show icon to the right of the password field, when iGetting an error for an undefined variableHello, I'm a CS major working on a project so I'm still pretty new. I'm getting a CS0165 error on liEnemy following after the player (MONOGAME)Im making a simple game using Monogame in visual studio. I have two sprites drawn on the screen (pinIm having an issue checking for collisions when comparing DateTime objects.I have this function that's supposed to check for collisions with bookings. I am using TimeSpans andclamping how fast a input (as a float) can change.Let’s say my Input X float changes in a amount of time from -1 to 1 (-1, -0.5, 0, 0.5, 1) so as an eIValueResolver won't work with AutoMapperHi guys , hope all of you doing well, I'm recently working on a project where I need to convert a spHelp with concurrency blocking issueI have a small app that runs CPU bound on multiple threads (amount of processors) On one PC it workFrontend not workingI dont really keen on frontend but I think have some knowledge about it would be nice, I'm learning