init commit
This commit is contained in:
commit
222077f6c2
68
fieldChecks.ts
Normal file
68
fieldChecks.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
export class FieldCheck {
|
||||||
|
error = "";
|
||||||
|
|
||||||
|
constructor(error: string) {
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
check(val: string) {
|
||||||
|
throw new Error("check() not implemented");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FieldCheckRegex extends FieldCheck {
|
||||||
|
regex = /none/;
|
||||||
|
|
||||||
|
constructor(regex: RegExp, error: string) {
|
||||||
|
super(error);
|
||||||
|
this.regex = regex;
|
||||||
|
}
|
||||||
|
|
||||||
|
check(val: string) {
|
||||||
|
if (!this.regex.test(val)) return this.error;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FieldCheckMaxLength extends FieldCheck {
|
||||||
|
maxLength = 0;
|
||||||
|
|
||||||
|
constructor(maxLength: number) {
|
||||||
|
super(`Максимальная длина равна ${maxLength}.`);
|
||||||
|
this.maxLength = maxLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
check(val: string) {
|
||||||
|
if (val.length > this.maxLength) return this.error;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FieldCheckMinLength extends FieldCheck {
|
||||||
|
minLength = 0;
|
||||||
|
|
||||||
|
constructor(minLength: number) {
|
||||||
|
super(`Минимальная длина равна ${minLength}.`);
|
||||||
|
this.minLength = minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
check(val: string) {
|
||||||
|
if (val.length < this.minLength) return this.error;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class FieldChecks {
|
||||||
|
static nonEmpty = new FieldCheckRegex(/\S+/, "Поле не может быть пустым.");
|
||||||
|
static onlyLatinLettersWhitespaces = new FieldCheckRegex(/^[A-Za-z\s]+$/, "Разрешены только Латинские буквы и пробелы.");
|
||||||
|
static onlyLettersWhitespaces = new FieldCheckRegex(/^[A-Za-zА-Яа-я\s]+$/, "Разрешены только буквы и пробелы.");
|
||||||
|
static onlyLatinLettersNumbersUnderscores = new FieldCheckRegex(
|
||||||
|
/^[A-Za-z0-9_\s]+$/,
|
||||||
|
"Разрешены только Латинские буквы, цифры и нижнее подчёркивание."
|
||||||
|
);
|
||||||
|
static email = new FieldCheckRegex(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, "Проверьте почтовый адрес.");
|
||||||
|
static noSpecialChars = new FieldCheckRegex(/^[A-Za-z0-9\s.,()!%]+$/, "Специальные символы не разрешены.");
|
||||||
|
static maxLength = (maxLength: number) => new FieldCheckMaxLength(maxLength);
|
||||||
|
static minLength = (minLength: number) => new FieldCheckMinLength(minLength);
|
||||||
|
}
|
45
formFields.ts
Normal file
45
formFields.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { FieldCheck } from "./fieldChecks";
|
||||||
|
|
||||||
|
export class FieldsContainer {
|
||||||
|
fields = new Map<string, Field>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add field to container.
|
||||||
|
* @param {string} name - The field name.
|
||||||
|
* @param {string} value - The field value.
|
||||||
|
* @param {Array} checks - Array of necessary FieldCheck objects.
|
||||||
|
*/
|
||||||
|
addField(name: string, value: string, checks: FieldCheck[]) {
|
||||||
|
this.fields.set(name, new Field(value, checks));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Field {
|
||||||
|
value: string;
|
||||||
|
checks: FieldCheck[];
|
||||||
|
|
||||||
|
constructor(value: string, checks: FieldCheck[]) {
|
||||||
|
this.value = value;
|
||||||
|
this.checks = checks;
|
||||||
|
}
|
||||||
|
|
||||||
|
tryGetFirstError() {
|
||||||
|
let result = "";
|
||||||
|
|
||||||
|
this.checks.every((fieldCheck) => {
|
||||||
|
result = fieldCheck.check(this.value);
|
||||||
|
return result === ""; //break or continue every()
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
tryGetAllErrors() {
|
||||||
|
const result = this.checks.flatMap((fieldCheck) => {
|
||||||
|
const err = fieldCheck.check(this.value);
|
||||||
|
return err === "" ? [] : [err];
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.join(", ");
|
||||||
|
}
|
||||||
|
}
|
4
index.d.ts
vendored
Normal file
4
index.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import { FieldChecks } from "./fieldChecks";
|
||||||
|
import { Field, FieldsContainer } from "./formFields";
|
||||||
|
|
||||||
|
export { FieldChecks, Field, FieldsContainer };
|
Loading…
Reference in New Issue
Block a user