-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCustomerOrderValidator.cs
More file actions
104 lines (89 loc) · 3.87 KB
/
CustomerOrderValidator.cs
File metadata and controls
104 lines (89 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;
using VirtoCommerce.CoreModule.Core.Common;
using VirtoCommerce.OrdersModule.Core;
using VirtoCommerce.OrdersModule.Core.Model;
using VirtoCommerce.Platform.Core.Settings;
namespace VirtoCommerce.OrdersModule.Data.Validators;
public class CustomerOrderValidator : AbstractValidator<CustomerOrder>
{
private readonly ISettingsManager _settingsManager;
public CustomerOrderValidator(
ISettingsManager settingsManager,
IEnumerable<IValidator<LineItem>> lineItemValidators,
IEnumerable<IValidator<Shipment>> shipmentValidators,
IValidator<PaymentIn> paymentInValidator,
IEnumerable<IValidator<IOperation>> operationValidators)
{
_settingsManager = settingsManager;
SetDefaultRules();
if (lineItemValidators.Any())
{
RuleForEach(order => order.Items).SetValidator(lineItemValidators.Last(), "default");
}
if (shipmentValidators.Any())
{
RuleForEach(order => order.Shipments).SetValidator(shipmentValidators.Last(), "default");
}
RuleForEach(order => order.InPayments).SetValidator(paymentInValidator);
// Apply all operation-level validators (e.g., document count limits)
foreach (var operationValidator in operationValidators)
{
Include(operationValidator);
}
}
public override ValidationResult Validate(ValidationContext<CustomerOrder> context)
{
// Check if validation is enabled (synchronous version)
var isValidationEnabled = _settingsManager.GetValueAsync<bool>(
ModuleConstants.Settings.General.CustomerOrderValidation)
.GetAwaiter()
.GetResult();
if (!isValidationEnabled)
{
// Skip validation if disabled
return new ValidationResult();
}
// Perform validation if enabled
return base.Validate(context);
}
public override async Task<ValidationResult> ValidateAsync(ValidationContext<CustomerOrder> context, CancellationToken cancellation = default)
{
// Check if validation is enabled
var isValidationEnabled = await _settingsManager.GetValueAsync<bool>(
ModuleConstants.Settings.General.CustomerOrderValidation);
if (!isValidationEnabled)
{
// Skip validation if disabled
return new ValidationResult();
}
// Perform validation if enabled
return await base.ValidateAsync(context, cancellation);
}
protected void SetDefaultRules()
{
#pragma warning disable S109
RuleFor(order => order.Number).NotEmpty().MaximumLength(64);
RuleFor(order => order.CustomerId).NotNull().NotEmpty().MaximumLength(64);
RuleFor(order => order.CustomerName).NotEmpty().MaximumLength(255);
RuleFor(order => order.StoreId).NotNull().NotEmpty().MaximumLength(64);
RuleFor(order => order.StoreName).MaximumLength(255);
RuleFor(order => order.ChannelId).MaximumLength(64);
RuleFor(order => order.OrganizationId).MaximumLength(64);
RuleFor(order => order.OrganizationName).MaximumLength(255);
RuleFor(order => order.EmployeeId).MaximumLength(64);
RuleFor(order => order.EmployeeName).MaximumLength(255);
RuleFor(order => order.SubscriptionId).MaximumLength(64);
RuleFor(order => order.SubscriptionNumber).MaximumLength(64);
RuleFor(order => order.LanguageCode).MaximumLength(16)
.Matches("^[a-z]{2}-[A-Z]{2}$")
.When(order => !string.IsNullOrEmpty(order.LanguageCode));
RuleFor(order => order.ShoppingCartId).MaximumLength(128);
RuleFor(order => order.PurchaseOrderNumber).MaximumLength(128);
#pragma warning restore S109
}
}