make custom fields case sensitive (#1242)

* remove custom field lowercasing

* don't allow custom field form to submit duplicate field

* remove unused

* update custom field tests

* also keep upper case when editing

* show key in UI

* also kep upper case when creating a new field

* fix test

* allow old forced case keys to stay as is

* allow space

* add extension to import

* supply initial key to CustomFieldForm

* refactor: improve element contrast

* refactor: style and composition

* consistent use of `customFieldLabelToKey`

* fix CopyTag

* remove log

---------

Co-authored-by: arc-alex <ac@omnivox.dk>
Co-authored-by: Carlos Valente <carlosvalente@pm.me>
This commit is contained in:
2024-10-18 11:53:23 -05:00
committed by GitHub
parent b73b529c00
commit c5870452e3
15 changed files with 161 additions and 59 deletions
@@ -279,7 +279,7 @@ describe('sanitiseCustomFields()', () => {
const customFields: CustomFields = {
test: { label: 'test', type: 'string', colour: 'red' },
test2: { label: 'test2', type: 'string', colour: 'green' },
test3: { label: 'Test3', type: 'string', colour: '' },
Test3: { label: 'Test3', type: 'string', colour: '' },
};
const sanitationResult = sanitiseCustomFields(customFields);
expect(sanitationResult).toStrictEqual(customFields);
@@ -328,10 +328,32 @@ describe('sanitiseCustomFields()', () => {
it('enforce name cohesion', () => {
const customFields: CustomFields = {
test: { label: 'New Name', type: 'string', colour: 'red' },
test: { label: 'NewName', type: 'string', colour: 'red' },
};
const expectedCustomFields: CustomFields = {
'new name': { label: 'New Name', type: 'string', colour: 'red' },
NewName: { label: 'NewName', type: 'string', colour: 'red' },
};
const sanitationResult = sanitiseCustomFields(customFields);
expect(sanitationResult).toStrictEqual(expectedCustomFields);
});
it('allow old keys', () => {
const customFields: CustomFields = {
test: { label: 'Test', type: 'string', colour: 'red' },
};
const expectedCustomFields: CustomFields = {
test: { label: 'Test', type: 'string', colour: 'red' },
};
const sanitationResult = sanitiseCustomFields(customFields);
expect(sanitationResult).toStrictEqual(expectedCustomFields);
});
it('labels with space', () => {
const customFields: CustomFields = {
Test_with_Space: { label: 'Test with Space', type: 'string', colour: 'red' },
};
const expectedCustomFields: CustomFields = {
Test_with_Space: { label: 'Test with Space', type: 'string', colour: 'red' },
};
const sanitationResult = sanitiseCustomFields(customFields);
expect(sanitationResult).toStrictEqual(expectedCustomFields);
@@ -342,12 +364,12 @@ describe('sanitiseCustomFields()', () => {
test: { label: 'test', type: 'string', colour: 'red' },
test2: { label: 'test2', type: 'string', colour: 'green' },
bad: { label: '', type: 'string', colour: '' },
test3: { label: 'Test3', type: 'string', colour: '' },
Test3: { label: 'Test3', type: 'string', colour: '' },
};
const expectedCustomFields: CustomFields = {
test: { label: 'test', type: 'string', colour: 'red' },
test2: { label: 'test2', type: 'string', colour: 'green' },
test3: { label: 'Test3', type: 'string', colour: '' },
Test3: { label: 'Test3', type: 'string', colour: '' },
};
const sanitationResult = sanitiseCustomFields(customFields);
expect(sanitationResult).toStrictEqual(expectedCustomFields);
+15 -4
View File
@@ -19,7 +19,13 @@ import {
isOntimeDelay,
isOntimeEvent,
} from 'ontime-types';
import { generateId, getErrorMessage, getLastEvent } from 'ontime-utils';
import {
customFieldLabelToKey,
generateId,
getErrorMessage,
getLastEvent,
isAlphanumericWithSpace,
} from 'ontime-utils';
import { dbModel } from '../models/dataModel.js';
import { block as blockDef, delay as delayDef } from '../models/eventsDefinition.js';
@@ -305,13 +311,18 @@ export function parseCustomFields(data: Partial<DatabaseModel>, emitError?: Erro
export function sanitiseCustomFields(data: object): CustomFields {
const newCustomFields: CustomFields = {};
for (const [_key, field] of Object.entries(data)) {
for (const [originalKey, field] of Object.entries(data)) {
if (!isValidField(field)) {
continue;
}
// make a new key to avoid mismatches
const key = field.label.toLowerCase();
if (!isAlphanumericWithSpace(field.label)) {
continue;
}
const keyFromLabel = customFieldLabelToKey(field.label);
//Test label and key cohesion, but allow old lowercased keys to stay
const key = originalKey.toLocaleLowerCase() === keyFromLabel.toLocaleLowerCase() ? originalKey : keyFromLabel;
if (key in newCustomFields) {
continue;
}