mirror of https://github.com/lukechilds/ow.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
749 B
37 lines
749 B
7 years ago
|
import { Predicate, Context } from './predicate';
|
||
|
|
||
|
export class StringPredicate extends Predicate {
|
||
|
|
||
|
constructor(context?: Context) {
|
||
|
super('string', context);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test a string to have a minimum length.
|
||
|
*
|
||
|
* @param number The minimum length of the string.
|
||
|
*/
|
||
|
minLength(number: number) {
|
||
|
this.context.validators.push(value => {
|
||
|
if (value.length < number) {
|
||
|
return `Expected string length to be minimum ${number}`;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test a string to be alphanumeric.
|
||
|
*/
|
||
|
get alphanumeric() {
|
||
|
this.context.validators.push(value => {
|
||
|
if (!/^[a-z\d]+$/i.test(value)) {
|
||
|
return `Expected string to contain only alphanumeric characters but received \`${value}\``;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return this;
|
||
|
}
|
||
|
}
|