Skip to content

Commit b5d17bd

Browse files
committed
feat: add validations for display name and semantic versioning
1 parent b46d24c commit b5d17bd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/Shared/validations.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ export const MESSAGES = {
2929
VALID_POSITIVE_NUMBER: 'This field should be a valid positive number',
3030
VALID_POSITIVE_INTEGER: 'This field should be a valid positive integer',
3131
MAX_SAFE_INTEGER: `Maximum allowed value is ${Number.MAX_SAFE_INTEGER}`,
32+
INVALID_SEMANTIC_VERSION: 'Please follow semantic versioning',
3233
}
3334

3435
const MAX_DESCRIPTION_LENGTH = 350
36+
const DISPLAY_NAME_CONSTRAINTS = {
37+
MAX_LIMIT: 50,
38+
MIN_LIMIT: 3,
39+
}
3540

3641
export const requiredField = (value: string): ValidationResponseType => {
3742
if (!value?.trim()) {
@@ -248,3 +253,45 @@ export const validateUniqueKeys = (keys: string[]) => {
248253
message: `Duplicate variable name: ${duplicateKeys.join(', ')}`,
249254
}
250255
}
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-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-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

Comments
 (0)