-
Notifications
You must be signed in to change notification settings - Fork 4
Request Validation
ericpopivker edited this page Jun 5, 2015
·
16 revisions
- Do Request Validation before execute
- Should be able to do simple validation like Required, MaxSize
- Also complex validation like Unique name
- Request.Error.Code will be "Invalid Arguments" see Evengrid
- Request.ArgumentErrors will include list of ArgumentError(ArgumentName, ErrorCode, ErrorDescription)
- Some Argument ErrorCodes will be common/shared, like Required, DoesNotExist, etc... But some will be specific to module. Like "Product name is not unique". So each module may have its ow list of ErrorCodes specifc to that module. Need to make sure it doesn't conflict with other modules.
- Validate ProductCreateRequest
Id shouldn't be set
Product Name is required
Price > 0 and valid currency
Product Name must be unique in this Store
- Validate ProductUpdateRequest
Id is valid product Id
Reuse the rest of validation as ProductCreateRequest
- Validate ProductDeleteRequest
Id is valid
Approach 1
https://github.com/JeremySkinner/FluentValidation Need to check how unit testable Is it better then Data Annotations?
Approach 2
StoreCreateCommand
{
Execute()
{
ValidateRequest
ExecuteInternal
}
ValidateRequest()
{
ValidateDataAnnotations
ValidateCustom1
ValidateCustom2
ValidateCustom3
}
}
IValidator //May have dependencies on other services
{
Validate();
}
IRequestValidator
{
Validate();
}
DataAnnotationsRequestValidator : IRequestValidator
{
Validate();
}
SinglePropertyRequestValidator : IRequestValidator
{
DependentProperties
TargetProperty
Validate();
}
MultiPropertyRequestValidator : IRequestValidator
{
DependentProperties
TargetProperties
Validate();
}
RequestValidatorService
{
Validate(List of IRequestValidator);
}
ValidatorFactory
{
IValidator Create(); //Uses IOC COntainer
}