Validating the address

Our address validation is going to use the MinLengthValidator and RegularExpressionValidator validators:

export class AddressValidation implements IValidation {
private readonly minLengthValidator : MinLengthValidator = new MinLengthValidator(5);
private readonly zipCodeValidator : RegularExpressionValidator
= new RegularExpressionValidator("^[0-9]{5}(?:-[0-9]{4})?$");
}

The minimum length validation is simple enough, but the regular expression can be intimidating if you have never seen this type of syntax before. Before we look at our validation code, we will break down what the regular expression is doing. 

The first character, ^, tells us that our validation is going to start at the very beginning of the string. If we left this character out, it would mean that our match could occur anywhere in the text. The use of [0-9] tells the regular expression engine that we want to match against a number. Strictly speaking, as US zip codes start with five numbers, we need to tell the validator that we want to match against five numbers, which we do by telling the engine how many we want: [0-9]{5}. If we only wanted to match against major area codes such as 10023, we could almost end our expression here. Zip codes, however, have an optional four-digit portion as well that is separated from the main part by a hyphen. Therefore, we have to tell the regular expression engine that we have an optional part that we want to apply.

We know that the format of the optional part of the zip code is a hyphen with four digits. This means that the next part of the regular expression has to consider the test as being one test. This means that we cannot test for a hyphen and then separately test for the numbers; we either have the -1234 format or we don't have anything. This tells us that we want to group the items we want to test together. The way that we group things together in a regular expression is to put the expression inside brackets. So, if we apply the same logic that we had before, we would probably think that this part of the validation was (-[0-9]{4}). As a first pass, that is pretty close to what we want. The rule, here, is to treat this as a group where the first character must be a hyphen and then there must be four numbers. There are two things that we need to sort out with this part of the expression. The first thing is that this test is not optional at the moment. In other words, the input 10012-1234 is valid, while 10012 is no longer valid. The second problem is that we have created something called a capture group in our expression, which we do not need.

A capture group is a numbered group that represents the number of the match. This can be useful if we want to match the same text in a number of places in a document; however, as we only want one match, it is something we can avoid.

We will fix both issues with the optional part of the validation now. The first thing we are going to do is remove the capture group. This is done by using an ?: operator that tells the engine that this group is a non-capture group. The next thing we are going to take care of is applying a ? operator that says that we want this match to happen zero times or one time only. In other words, we have made this an optional test. At this point, we can successfully test both 10012 and 10012-1234, but we do have one more thing that we need to take care of. We need to make sure that the input only matches this input. In other words, we don't want to allow any stray characters at the end; otherwise, the user would be able to type in 10012-12345 and the engine would think that we had a valid input. What we need to do is add the $ operator at the end of the expression, which states that the expression is expecting the end of the line at that point. At this point, our regular expression is ^[0-9]{5}(?:-[0-9]{4})?$, which matches the validation that we are expecting to apply to the zip code.

I have chosen to explicitly specify that a number is represented as [0-9] because it is a clear indicator for someone new to regular expressions that this represents a number between 0 and 9. There is an equivalent shorthand that can be used to represent a single digit, and that is to use d in its place. With this, we can rewrite this rule to ^d{5}(?:-d{4})?$. The use of d in this represents a single American Standard Code for Information Interchange (ASCII) digit.

Going back to our address validation, the actual validation itself is extremely straightforward because we took the time to write validators that did the hard work for us. All we need to do is apply the minimum length validator against the first line of the address, the town, and the county, and the regular expression validator is applied to the zip code. Each failing validation item is added to the list of errors:

public Validate(state: IPersonState, errors: string[]): void {
if (!this.minLengthValidator.IsValid(state.Address1)) {
errors.push("Address line 1 must be greater than 5 characters");
}
if (!this.minLengthValidator.IsValid(state.Town)) {
errors.push("Town must be greater than 5 characters");
}
if (!this.minLengthValidator.IsValid(state.County)) {
errors.push("County must be greater than 5 characters");
}
if (!this.zipCodeValidator.IsValid(state.Postcode)) {
errors.push("The postal/zip code is invalid");
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.193.232