-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathvalidation.ts
116 lines (95 loc) · 3.94 KB
/
validation.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
export const MIN_NAME_CHARACTERS = 5;
export const MAX_NAME_CHARACTERS = 50;
// This regex pattern support MIN to MAX character limit, capital and lowercase letters,
// numbers 0-9, hyphens, spaces, and underscores.
export const NAME_REGEX = new RegExp(/^[a-zA-Z0-9 _-]{5,50}$/);
// This regex pattern support MIN to MAX character limit for detection rule name
export const RULE_NAME_REGEX = new RegExp(/^.{1,256}$/);
export const LOG_TYPE_NAME_REGEX = new RegExp(/^[a-z0-9_-]{2,50}$/);
// This regex pattern support MIN to MAX character limit, capital and lowercase letters,
// numbers 0-9, hyphens, dot, and underscores.
export const DETECTION_NAME_REGEX = new RegExp(/^[a-zA-Z0-9_.-]{1,50}$/);
export const DETECTION_CONDITION_REGEX = new RegExp(
/^((not )?.+)?( (and|or|and not|or not|not) ?(.+))*(?<!and|or|not)$/
);
// This regex pattern support MIN to MAX character limit.
export const AUTHOR_REGEX = new RegExp(/^.{0,256}$/);
/**
* Validates a string against NAME_REGEX.
* @param name: The string to validate.
* @param regex
* @return TRUE if valid; else FALSE.
*/
export function validateName(
name: string,
regex: RegExp = NAME_REGEX,
shouldTrimName: boolean = true
): boolean {
const toValidate = shouldTrimName ? name.trim() : name;
return toValidate.match(regex) !== null;
}
export function validateDetectionFieldName(
name: string,
regex: RegExp = DETECTION_NAME_REGEX
): boolean {
return name.trim().match(regex) !== null;
}
export function validateCondition(
name: string,
regex: RegExp = DETECTION_CONDITION_REGEX
): boolean {
return name.trim().match(regex) !== null;
}
const nameOrTitleErrorBase = `should only consist of upper and lowercase letters, numbers 0-9, hyphens, spaces, and underscores.
Use between ${MIN_NAME_CHARACTERS} and ${MAX_NAME_CHARACTERS} characters.`;
export const nameErrorString = `Name ${nameOrTitleErrorBase}`;
export const titleErrorString = `Title ${nameOrTitleErrorBase}`;
export const authorErrorString = `Author name should only consist of upper and lowercase letters, numbers 0-9, hyphens, commas, spaces, and underscores.
Use between ${MIN_NAME_CHARACTERS} and ${MAX_NAME_CHARACTERS} characters.`;
export function getNameErrorMessage(
name: string,
nameIsInvalid: boolean,
nameFieldTouched: boolean
) {
const trimmedName = name.trim();
if (nameFieldTouched) {
if (trimmedName.length === 0) return 'Enter a name.';
if (nameIsInvalid) return nameErrorString;
}
}
// This regex pattern support MIN to MAX character limit, capital and lowercase letters,
// numbers 0-9, hyphens, periods, spaces, and underscores.
export const DESCRIPTION_REGEX = new RegExp(/^[a-zA-Z0-9 _.,-]{0,500}$/);
export const MAX_RULE_DESCRIPTION_LENGTH = 65535;
// This regex pattern support MIN to MAX character limit for detection rule description.
export const RULE_DESCRIPTION_REGEX = new RegExp(`^.{0,${MAX_RULE_DESCRIPTION_LENGTH}}$`);
/**
* Validates a string against NAME_REGEX.
* @param name: The string to validate.
* @return TRUE if valid; else FALSE.
*/
export function validateDescription(
name: string,
descriptionRegex: RegExp = DESCRIPTION_REGEX
): boolean {
return name.trim().match(descriptionRegex) !== null;
}
export const descriptionError = `Description should only consist of upper and lowercase letters, numbers 0-9, commas, hyphens, periods, spaces, and underscores. Max limit of 500 characters.`;
export const detectionRuleDescriptionError = `Description has max limit of 65,535 characters.`;
export function getDescriptionErrorMessage(
_description: string,
descriptionIsInvalid: boolean,
descriptionFieldTouched: boolean
) {
if (descriptionFieldTouched && descriptionIsInvalid) {
return descriptionError;
}
}
export function validateField(hasSubmitted: boolean, fieldTouched: boolean) {
return hasSubmitted || fieldTouched;
}
export const detectionRuleNameError = 'Rule name can be max 256 characters.';