C
C#2w ago
KevS

Immediate.Validations conditional validators and delegates

Trying to reimplement some of my FluentValidation rules in IV and a lot of my rules depend on .When() and Must(). I see that AdditionalValidators is something I can define but the example in the docs makes it look like a lot of boilerplate in comparison
13 Replies
viceroypenguin
keep in mind that you can create your own attribute validators for use as well
KevS
KevSOP2w ago
If I do and it feels like a common thing, such as "Valid File Path", should I submit a PR?
viceroypenguin
in your case, if you want to validate an IPAddress, then you could do something like:
public sealed class IpAddressAttribute : ValidatorAttribute
{
public static bool ValidateProperty(string target)
{
// check whether `target` is an ip address
return true;
}

public static string DefaultMessage => "Invalid IP Address";
}
public sealed class IpAddressAttribute : ValidatorAttribute
{
public static bool ValidateProperty(string target)
{
// check whether `target` is an ip address
return true;
}

public static string DefaultMessage => "Invalid IP Address";
}
then you can apply it like:
[IpAddress]
public required string Address { get; init; }
[IpAddress]
public required string Address { get; init; }
KevS
KevSOP2w ago
Oh I see, that's less work than I thought it would be
viceroypenguin
valid file path probably would not make it in, since that's actually fairly application specific (unix vs windows, is there an abstraction layer of some kind that changes what a "valid" file path is, etc.)
KevS
KevSOP2w ago
Fair. What about conditional validation? I.e. only care about property B if property A is true
viceroypenguin
haven't really run into that very often, and i usually just do that via AdditionalValidations
KevS
KevSOP2w ago
Yeah I have a lot of that in my startup options
viceroypenguin
you could write a multi-rgument validator
public sealed class NotEmptyIfAttribute(
[TargetType]
object isTrue
) : ValidatorAttribute
{
public static bool ValidateProperty(string target, bool isTrue)
{
if (!isTrue)
return true;

return !string.IsNullOrWhitespace(target);
}

public static string DefaultMessage => "Property is null or whitespace";
}
public sealed class NotEmptyIfAttribute(
[TargetType]
object isTrue
) : ValidatorAttribute
{
public static bool ValidateProperty(string target, bool isTrue)
{
if (!isTrue)
return true;

return !string.IsNullOrWhitespace(target);
}

public static string DefaultMessage => "Property is null or whitespace";
}
KevS
KevSOP2w ago
Like I can pass other properties into a validator?

public bool BoolProp { get; set; } = false;
[NotEmptyIf(BoolProp)]
public string TargetProp { get; set; } = string.Empty;

public bool BoolProp { get; set; } = false;
[NotEmptyIf(BoolProp)]
public string TargetProp { get; set; } = string.Empty;
viceroypenguin
public required bool Enabled { get; init; }
[NotEmptyIf(nameof(Enabled))]
public required string Value { get; init; }
public required bool Enabled { get; init; }
[NotEmptyIf(nameof(Enabled))]
public required string Value { get; init; }
KevS
KevSOP2w ago
Ah nameof okay got it Okay awesome well that's super nice Thanks!

Did you find this page helpful?