-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTrainingAddressValidator.cs
More file actions
65 lines (57 loc) · 2.48 KB
/
Copy pathTrainingAddressValidator.cs
File metadata and controls
65 lines (57 loc) · 2.48 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
using System;
using ActiveCommerce.Validation;
using Sitecore.Diagnostics;
namespace ActiveCommerce.Training.AddressValidator
{
public class TrainingAddressValidator : IAddressValidator
{
public Sitecore.Ecommerce.DomainModel.Addresses.AddressInfo Validate(Sitecore.Ecommerce.DomainModel.Addresses.AddressInfo address)
{
ValidateRequired(address.Address, "Address Line 1");
ValidateRequired(address.City, "City");
ValidateRequired(address.Country.Code, "Country");
/**
* After doing basic validations, you could call an address validation service or API here.
* Instead, we're going to do some custom validation. The builtin default address validator is
* definitely more thorough -- this is for example purposes only.
*/
if (address.Country.Code.Equals("US", StringComparison.InvariantCultureIgnoreCase))
{
ValidateRequired(address.State, "State");
ValidateRequired(address.Zip, "Postal Code");
//zip or zip plus 4
if (address.Zip.Length != 5 && address.Zip.Length != 10)
{
throw new AddressValidationException("Not a valid U.S. zip code");
}
}
/**
* To emphasize that this is for example only :)
*
*/
if (address.Address.Contains("hubert"))
{
throw new AddressValidationException("You taste like soot and...");
}
/**
* We can make simple corrections to the address as well. Interactive address
* corrections (e.g. with user acceptance of corrected address) would require
* customization of the checkout UX.
*/
address.Address = address.Address.ToUpper();
address.Address2 = address.Address2 != null ? address.Address2.ToUpper() : null;
address.City = address.City.ToUpper();
address.State = address.State.ToUpper();
address.Zip = address.Zip.ToUpper();
return address;
}
protected void ValidateRequired(string required, string name)
{
Assert.ArgumentNotNullOrEmpty(name, "name");
if (string.IsNullOrWhiteSpace(required))
{
throw new AddressValidationException(string.Format("{0} is required", name));
}
}
}
}