-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.validators.ts
More file actions
94 lines (83 loc) · 3.15 KB
/
Copy pathnote.validators.ts
File metadata and controls
94 lines (83 loc) · 3.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { NoteService } from '../services/note.service';
export class NoteValidators {
static noWhitespace(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
return value.trim().length === 0 ? { noWhitespace: true } : null;
}
static noConsecutiveSpaces(
control: AbstractControl,
): ValidationErrors | null {
const value = control.value || '';
return /\s{2,}/.test(value) ? { noConsecutiveSpaces: true } : null;
}
static maxLength(max: number): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = control.value || '';
return value.length > max
? { maxLength: { max, actual: value.length } }
: null;
};
}
static onlyAlphabets(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
const pattern = /^[a-zA-Z\s]*$/;
return !pattern.test(value) ? { onlyAlphabets: true } : null;
}
static noOnlyRepeatingChars(
control: AbstractControl,
): ValidationErrors | null {
const value = (control.value || '').trim();
if (!value) return null;
const allSame = value.split('').every((c: string) => c === value[0]);
return allSame ? { noOnlyRepeatingChars: true } : null;
}
static firstLetterCapital(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
if (!value) return null;
return value[0] !== value[0].toUpperCase()
? { firstLetterCapital: true }
: null;
}
static noDuplicateTitle(
noteService: NoteService,
currentId?: string,
): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = (control.value || '').trim().toLowerCase();
const isDuplicate = noteService
.getAllNotes()
.some(
(note) =>
note.title.trim().toLowerCase() === value && note.id !== currentId,
);
return isDuplicate ? { noDuplicateTitle: true } : null;
};
}
static minWordCount(min: number): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = control.value || '';
const wordCount = value.trim().split(/\s+/).filter(Boolean).length;
return wordCount < min
? { minWordCount: { min, actual: wordCount } }
: null;
};
}
static wordLimit(max: number): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = control.value || '';
const wordCount = value.trim().split(/\s+/).filter(Boolean).length;
return wordCount > max ? { wordLimit: { max, actual: wordCount } } : null;
};
}
static noUrl(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
const urlPattern = /(https?:\/\/|www\.)\S+/gi;
return urlPattern.test(value) ? { noUrl: true } : null;
}
static noHtmlTags(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
const htmlPattern = /<[^>]*>/g;
return htmlPattern.test(value) ? { noHtmlTags: true } : null;
}
}