-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation reuse #327
Comments
That's a great idea - thanks @glen-84 ! I'll take a look at implementing that soon. |
Is this still planned? It could be useful. One thing to note is that the It might also make sense for the call to the IdValidator.IsValid(typeof(ArticleId), value);
// and/or
IdValidator.IsValid<ArticleId>(value); So that the validator can access information about the value object type, or access its (static abstract) properties. Ultimately, I'm looking for a way to "bake" some validation into a value object. For example, given the following value objects: public sealed class StringValueAttribute : ValueObjectAttribute<string>
{
public StringValueAttribute(Conversions conversions = Conversions.None) : base(conversions) { }
}
[StringValue]
public sealed partial class EmailAddress
{
public static int MinimumLength { get; } = 3;
public static int MaximumLength { get; } = 60;
}
[StringValue]
public sealed partial class Username
{
public static int MinimumLength { get; } = 2;
public static int MaximumLength { get; } = 20;
} ... a way to validate these objects while making use of their properties, without having to manually call a validation method within |
Thanks for the feedback @glen-84 - sorry that this feature has taken so long to implement. I haven't had much time lately. Re. your suggestion above: are you saying that the validator type somehow 'inspects' the type for known properties such as the length properties, and generates the associated code? I'm thinking, as I write this, that maybe an
... and this makes me think of FluentValidation. I think there's definitely an opportunity to improve validation, but I'm also thinking that it might be better as a completely separate nuget package and just have Vogen provide 'hooks' for this new package... |
Nope. The validator class would be manually written. Something like this: public interface IStringValue
{
public static abstract int MinimumLength { get; }
public static abstract int MaximumLength { get; }
}
public static class MyStringValidator
{
public static Validation IsValid<TValueObject>(string value) where TValueObject : IStringValue
{
var minimumLength = TValueObject.MinimumLength;
var maximumLength = TValueObject.MaximumLength;
// validation here ...
}
}
public sealed class StringValueAttribute : ValueObjectAttribute<string>
{
public StringValueAttribute(
Conversions conversions = Conversions.None)
: base(conversions, validator: typeof(MyStringValidator)) { }
}
[StringValue]
public sealed partial class EmailAddress : IStringValue
{
public static int MinimumLength { get; } = 3;
public static int MaximumLength { get; } = 60;
}
[StringValue]
public sealed partial class Username : IStringValue
{
public static int MinimumLength { get; } = 2;
public static int MaximumLength { get; } = 20;
} I don't know if static abstract properties are the best solution, but it's just an idea. |
I was looking for this exact feature right now. When it comes to strings the main use would be validation of lenghts, requiredness, and maybe some functional validation ( would it be an idea to mimic the attributes from [StringValue]
[MinLength(2)]
[MaxLength(20)]
public sealed partial class Username
{
} |
It certainly looks like this would be useful. I'll see if I can add it early next year. |
This feature would be quite nice indeed. [ValueObject<string>(toPrimitiveCasting: CastOperator.Implicit)] // legacy support
public partial class MyEntityNameId
{
private static Validation Validate(string id) => MongoObjectIdValidator.IsObjectId(id);
} Would be nice if this could be reduced to: [MongoIdValueObject] // custom attribute that wraps/inherits from ValueObjectAttribute<string> and sets toPrimitiveCasting and defines a custom validator like typeof(MongoObjectIdValidator)
public partial class MyEntityNameId { } Even for VOs that don't have custom validators it would be valuable to support custom attributes like [StringValueObject] that allows one to skip the toPrimitiveCasting default setting that I for example have for all the IDs, but not all VOs. |
Doesn't this already work? public sealed class StringValueObjectAttribute()
: ValueObjectAttribute<string>(toPrimitiveCasting: CastOperator.Implicit); |
Describe the feature
It's currently possible to do something like this:
But that line of code seems unnecessary. What about a parameter to specify the type of a static validation class?
A generic overload could also work, but if I'm not mistaken it would require you to always specify the underlying type.
I think that would be worth it though.
The text was updated successfully, but these errors were encountered: