refactor: extract regex utilities

This commit is contained in:
Carlos Valente
2025-07-20 07:14:54 +02:00
committed by Carlos Valente
parent 44508ce8b4
commit 5bc286145d
10 changed files with 82 additions and 46 deletions
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { CustomField } from 'ontime-types';
import { customFieldLabelToKey, isAlphanumericWithSpace } from 'ontime-utils';
import { checkRegex, customFieldLabelToKey } from 'ontime-utils';
import { maybeAxiosError } from '../../../../../common/api/utils';
import Button from '../../../../../common/components/buttons/Button';
@@ -117,7 +117,7 @@ export default function CustomFieldForm({
onChange: () => setValue('key', customFieldLabelToKey(getValues('label')) ?? 'N/A'),
validate: (value) => {
if (value.trim().length === 0) return 'Required field';
if (!isAlphanumericWithSpace(value)) return 'Only alphanumeric characters and space are allowed';
if (!checkRegex.isAlphanumericWithSpace(value)) return 'Only alphanumeric characters and space are allowed';
if (!isEditMode) {
if (isEditMode && Object.keys(data).includes(value)) return 'Custom fields must be unique';
}
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { useFieldArray, useForm } from 'react-hook-form';
import { IoAdd, IoTrash } from 'react-icons/io5';
import { ImportMap, isAlphanumericWithSpace } from 'ontime-utils';
import { checkRegex, ImportMap } from 'ontime-utils';
import Button from '../../../../../../common/components/buttons/Button';
import IconButton from '../../../../../../common/components/buttons/IconButton';
@@ -200,7 +200,7 @@ export default function ImportMapForm({
placeholder='Name of the field as shown in Ontime'
{...register(`custom.${index}.ontimeName`, {
validate: (value) => {
if (!isAlphanumericWithSpace(value))
if (!checkRegex.isAlphanumericWithSpace(value))
return 'Only alphanumeric characters and space are allowed';
return true;
},