You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some scenarios, it might be good to validate the input value even if the underlying type supports the actual value. For example, I might want to use an int, but don't want to support negative numbers or 0, or maybe the value has to be greater than 1000.
Or I want to use a string as underlying type, but want to have some constraints on which characters can be used, etc.
If I could declare a partial method or similar that would be called whenever the parsed value is about to be stored in Value and return false for TryParse and throw on constructor or Parse. That could solve this, but not sure if there are other options available already?
Example idea with int:
[StronglyTypedId(Template.Int)]
public partial struct MyIntId
{
private partial bool IsValid(int value)
{
return value > 1000;
}
}
Example idea with string (only allowing A-Z and 0-9):
The text was updated successfully, but these errors were encountered:
pergardebrink
changed the title
Proposal: Add custom validation to constructor/Parse/TryParse
[Proposal]: Add custom validation to constructor/Parse/TryParse
Jan 8, 2025
In some scenarios, it might be good to validate the input value even if the underlying type supports the actual value. For example, I might want to use an int, but don't want to support negative numbers or 0, or maybe the value has to be greater than 1000.
Or I want to use a string as underlying type, but want to have some constraints on which characters can be used, etc.
If I could declare a partial method or similar that would be called whenever the parsed value is about to be stored in Value and return false for TryParse and throw on constructor or Parse. That could solve this, but not sure if there are other options available already?
Example idea with int:
Example idea with string (only allowing A-Z and 0-9):
The text was updated successfully, but these errors were encountered: