@@ -29,9 +29,14 @@ export const MESSAGES = {
29
29
VALID_POSITIVE_NUMBER : 'This field should be a valid positive number' ,
30
30
VALID_POSITIVE_INTEGER : 'This field should be a valid positive integer' ,
31
31
MAX_SAFE_INTEGER : `Maximum allowed value is ${ Number . MAX_SAFE_INTEGER } ` ,
32
+ INVALID_SEMANTIC_VERSION : 'Please follow semantic versioning' ,
32
33
}
33
34
34
35
const MAX_DESCRIPTION_LENGTH = 350
36
+ const DISPLAY_NAME_CONSTRAINTS = {
37
+ MAX_LIMIT : 50 ,
38
+ MIN_LIMIT : 3 ,
39
+ }
35
40
36
41
export const requiredField = ( value : string ) : ValidationResponseType => {
37
42
if ( ! value ?. trim ( ) ) {
@@ -248,3 +253,45 @@ export const validateUniqueKeys = (keys: string[]) => {
248
253
message : `Duplicate variable name: ${ duplicateKeys . join ( ', ' ) } ` ,
249
254
}
250
255
}
256
+
257
+ /**
258
+ * Rules for valid semantic version:
259
+ * 1. version.length < 128 and not empty
260
+ * 2. version should follow semantic versioning regex from https://semver.org/
261
+ */
262
+ export const validateSemanticVersioning = ( version : string ) : ValidationResponseType => {
263
+ if ( ! version ) {
264
+ return {
265
+ isValid : false ,
266
+ message : 'Please provide a version' ,
267
+ }
268
+ }
269
+
270
+ if ( version . length > 128 ) {
271
+ return {
272
+ isValid : false ,
273
+ message : MESSAGES . getMaxCharMessage ( 128 ) ,
274
+ }
275
+ }
276
+
277
+ if (
278
+ ! / ^ ( 0 | [ 1 - 9 ] \d * ) \. ( 0 | [ 1 - 9 ] \d * ) \. ( 0 | [ 1 - 9 ] \d * ) (?: - ( (?: 0 | [ 1 - 9 ] \d * | \d * [ a - z A - Z - ] [ 0 - 9 a - z A - Z - ] * ) (?: \. (?: 0 | [ 1 - 9 ] \d * | \d * [ a - z A - Z - ] [ 0 - 9 a - z A - Z - ] * ) ) * ) ) ? (?: \+ ( [ 0 - 9 a - z A - Z - ] + (?: \. [ 0 - 9 a - z A - Z - ] + ) * ) ) ? $ / . test (
279
+ version ,
280
+ )
281
+ ) {
282
+ return {
283
+ isValid : false ,
284
+ message : MESSAGES . INVALID_SEMANTIC_VERSION ,
285
+ }
286
+ }
287
+
288
+ return {
289
+ isValid : true ,
290
+ }
291
+ }
292
+
293
+ /**
294
+ * A valid display name should be between 3 and 50 characters
295
+ */
296
+ export const validateDisplayName = ( name : string ) : ValidationResponseType =>
297
+ validateStringLength ( name , DISPLAY_NAME_CONSTRAINTS . MAX_LIMIT , DISPLAY_NAME_CONSTRAINTS . MIN_LIMIT )
0 commit comments