- Advanced TypeScript Programming Projects
- Peter O'Hanlon
- 639字
- 2021-06-24 13:27:13
Validating user inputs and the use of validators
At this stage, we really should think about validating inputs from the user. We are going to introduce two types of validation in our code. The first is the minimum length validation. In other words, we are going to ensure that some of the entries have to have a minimum number of entries before they can be considered to be valid. The second type of validation uses something called a regular expression to validate it. What this means is that it takes the input and compares it against a set of rules to see whether there is a match; the expressions can look a little bit odd if you are new to regular expressions, so we will break them down to see exactly what rules we are applying.
We are going to break our validation down into three parts:
- The classes that provide the checking features, such as applying a regular expression. We will call these validators.
- The classes that apply the validation items to the different parts of the state. We will call these classes validations.
- The component that will call the validation items and update the UI with the details of a failed validation. This will be a new component called FormValidation.tsx.
We will start by creating an interface called IValidator. This interface is going to accept a generic parameter so that we can apply it to pretty much anything that we want. As a validation will tell us whether the input is valid, it will have a single method called IsValid that accepts the relevant input and then returns a boolean value:
interface IValidator<T> {
IsValid(input : T) : boolean;
}
The first validator that we are going to write checks to see whether a string has a minimum number of characters, which we will set through the constructor. We will also guard against situations where the user fails to supply an input, by returning false from IsValid when the input is null:
export class MinLengthValidator implements IValidator<StringOrNull> {
private minLength : number;
constructor(minLength : number) {
this.minLength = minLength;
}
public IsValid(input : StringOrNull) : boolean {
if (!input) {
return false;
}
return input.length >= this.minLength;
}
}
The other validator that we are going to create is slightly more complicated. This validator accepts a string, which it uses to create something called a regular expression. A regular expression is effectively a mini language that provides a set of rules to test our input string against. In this case, the rules that form our regular expression are passed into our constructor. The constructor will then instantiate an instance of the JavaScript regular expression engine (RegExp). In a similar way to the minimum length validation, we ensure that we return false if there is no input. If we have an input, then we return the result of our regular expression test:
import { StringOrNull } from 'src/Types';
export class RegularExpressionValidator implements IValidator<StringOrNull> {
private regex : RegExp;
constructor(expression : string) {
this.regex = new RegExp(expression);
}
public IsValid (input : StringOrNull) : boolean {
if (!input) {
return false;
}
return this.regex.test(input);
}
}
Now that we have our validators, we are going to examine how we are going to apply them. It probably will not come as a surprise that the first thing that we are going to do is define an interface that forms the contract of what we want our validation to do. Our Validate method is going to accept the IPersonState state from our component, validate items from this, and then return an array of validation failures:
export interface IValidation {
Validate(state : IPersonState, errors : string[]) : void;
}
I have decided to break the validation down into the following three areas:
- Validating the address
- Validating the name
- Validating the phone number
- Mastering ElasticSearch
- Puppet實戰
- Google系統架構解密:構建安全可靠的系統
- Ubuntu Linux操作系統
- 8051軟核處理器設計實戰
- 巧學活用Windows 7
- 突破平面3ds Max動畫設計與制作
- 蘋果OS X Mavericks 10.9應用大全
- AutoCAD 2014中文版從入門到精通
- Linux命令行大全(第2版)
- Distributed Computing with Go
- Linux網絡操作系統項目教程(RHEL 7.4/CentOS 7.4)(第3版)(微課版)
- 大規模分布式系統架構與設計實戰
- 大學計算機應用基礎實踐教程(Windows 7+MS Office 2010)
- Linux指令從初學到精通