1
1
import { isNumber } from 'lodash' ;
2
- import { robotToHuman } from '../../robotToHumanFilter/robotToHuman.filter' ;
3
2
3
+ import { SETTINGS } from '../../../config/settings' ;
4
+ import { robotToHuman } from '../../robotToHumanFilter/robotToHuman.filter' ;
4
5
import type { IValidator } from './validation' ;
5
6
6
7
const THIS_FIELD = 'This field' ;
@@ -9,13 +10,33 @@ const VALID_EMAIL_REGEX = new RegExp(
9
10
'^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$' ,
10
11
) ;
11
12
13
+ const urlPattern = SETTINGS . cdevents ?. validUrlPattern ?? '^https?://.+$' ;
14
+ const VALID_URL = new RegExp ( urlPattern ) ;
15
+
16
+ const cdeventPattern = SETTINGS . cdevents ?. validCDEvent ?? '^dev\\.cdevents\\.[^.]+\\.[^.]+$' ;
17
+ const VALID_CDEVENT_REGEX = new RegExp ( cdeventPattern ) ;
18
+
12
19
const emailValue = ( message ?: string ) : IValidator => {
13
20
return function emailValue ( val : string , label = THIS_FIELD ) {
14
21
message = message || `${ label } is not a valid email address.` ;
15
22
return val && ! VALID_EMAIL_REGEX . test ( val ) && message ;
16
23
} ;
17
24
} ;
18
25
26
+ const urlValue = ( message ?: string ) : IValidator => {
27
+ return function urlValue ( val : string , label = THIS_FIELD ) {
28
+ message = message || `${ label } is not a valid URL.` ;
29
+ return val && ! VALID_URL . test ( val ) && message ;
30
+ } ;
31
+ } ;
32
+
33
+ const cdeventsTypeValue = ( message ?: string ) : IValidator => {
34
+ return function cdeventsTypeValue ( val : string , label = THIS_FIELD ) {
35
+ message = message || `${ label } is not a valid CDEvents Type.` ;
36
+ return val && ! VALID_CDEVENT_REGEX . test ( val ) && message ;
37
+ } ;
38
+ } ;
39
+
19
40
const isRequired = ( message ?: string ) : IValidator => {
20
41
return function isRequired ( val : any , label = THIS_FIELD ) {
21
42
message = message || `${ label } is required.` ;
@@ -138,6 +159,8 @@ export const Validators = {
138
159
arrayNotEmpty,
139
160
checkBetween,
140
161
emailValue,
162
+ cdeventsTypeValue,
163
+ urlValue,
141
164
isNum,
142
165
isRequired,
143
166
isValidJson,
0 commit comments