Refactor validation
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -23,10 +23,6 @@ const isRgbColor = (color: string) => {
|
||||
return /^rgb\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)$/i.test(color);
|
||||
};
|
||||
|
||||
const isValidColor = (color: string) => {
|
||||
return isHexColor(color) || isRgbColor(color);
|
||||
};
|
||||
|
||||
const rgbToHex = (rgb: string) => {
|
||||
const [r, g, b] = rgb
|
||||
.replaceAll(/[^\d,]/g, '')
|
||||
@@ -37,10 +33,22 @@ const rgbToHex = (rgb: string) => {
|
||||
};
|
||||
|
||||
const generateRandomColor = () => {
|
||||
const r = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const g = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const b = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const r = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
const g = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
const b = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
return `#${r}${g}${b}`;
|
||||
};
|
||||
|
||||
export { getContrastColor, isHexColor, isRgbColor, isValidColor, rgbToHex, generateRandomColor };
|
||||
export {
|
||||
getContrastColor,
|
||||
isHexColor,
|
||||
isRgbColor,
|
||||
rgbToHex,
|
||||
generateRandomColor,
|
||||
};
|
||||
|
@@ -1,5 +1,5 @@
|
||||
const multipleIdQuery = (ids: string[]) => {
|
||||
return `id in {${ids.map(id => `uuid(${id})`).join(',')}}`;
|
||||
return `id IN {${ids.map(id => `uuid(${id})`).join(',')}}`;
|
||||
};
|
||||
|
||||
export { multipleIdQuery };
|
||||
|
@@ -2,7 +2,6 @@ export {
|
||||
getContrastColor,
|
||||
isHexColor,
|
||||
isRgbColor,
|
||||
isValidColor,
|
||||
rgbToHex,
|
||||
generateRandomColor,
|
||||
} from './color';
|
||||
@@ -16,3 +15,10 @@ export {
|
||||
} from './filesystem';
|
||||
export { isPermissionForPath, clearPermissions } from './permissions';
|
||||
export { getSortIcon, getViewIcon } from './icon';
|
||||
export {
|
||||
type StringValidationResult,
|
||||
validateMemeTitle,
|
||||
validateMemeDescription,
|
||||
validateTagName,
|
||||
validateColor,
|
||||
} from './validation';
|
||||
|
85
src/utilities/validation.ts
Normal file
85
src/utilities/validation.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { isHexColor, isRgbColor } from './color';
|
||||
|
||||
interface StringValidationResult {
|
||||
valid: boolean;
|
||||
raw: string;
|
||||
parsed: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
const validateMemeTitle = (title: string): StringValidationResult => {
|
||||
const parsedTitle = title.trim();
|
||||
|
||||
if (parsedTitle.length === 0) {
|
||||
return {
|
||||
valid: false,
|
||||
raw: title,
|
||||
parsed: parsedTitle,
|
||||
error: 'Title cannot be empty',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
raw: title,
|
||||
parsed: parsedTitle,
|
||||
};
|
||||
};
|
||||
|
||||
const validateMemeDescription = (
|
||||
description: string,
|
||||
): StringValidationResult => {
|
||||
const parsedDescription = description.trim();
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
raw: description,
|
||||
parsed: parsedDescription,
|
||||
};
|
||||
};
|
||||
|
||||
const validateTagName = (name: string): StringValidationResult => {
|
||||
const parsedName = name.trim();
|
||||
|
||||
if (parsedName.length === 0) {
|
||||
return {
|
||||
valid: false,
|
||||
raw: name,
|
||||
parsed: parsedName,
|
||||
error: 'Name cannot be empty',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
raw: name,
|
||||
parsed: parsedName,
|
||||
};
|
||||
};
|
||||
|
||||
const validateColor = (color: string): StringValidationResult => {
|
||||
const parsedColor = color.trim().toLowerCase();
|
||||
|
||||
if (!isHexColor(parsedColor) && !isRgbColor(parsedColor)) {
|
||||
return {
|
||||
valid: false,
|
||||
raw: color,
|
||||
parsed: parsedColor,
|
||||
error: 'Color must be a valid hex or rgb value',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
raw: color,
|
||||
parsed: parsedColor,
|
||||
};
|
||||
};
|
||||
|
||||
export {
|
||||
type StringValidationResult,
|
||||
validateMemeTitle,
|
||||
validateMemeDescription,
|
||||
validateTagName,
|
||||
validateColor,
|
||||
};
|
Reference in New Issue
Block a user