mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-17 13:23:35 +00:00
Fix: custom fields with undefined colour is not accepted by parseCustomFields (#925)
This commit is contained in:
committed by
GitHub
parent
5e39c9b51f
commit
accdf2c396
@@ -1,5 +1,5 @@
|
|||||||
import { HttpSubscription, OscSubscription } from 'ontime-types';
|
import { CustomFields, HttpSubscription, OscSubscription } from 'ontime-types';
|
||||||
import { sanitiseHttpSubscriptions, sanitiseOscSubscriptions } from '../parserFunctions.js';
|
import { sanitiseCustomFields, sanitiseHttpSubscriptions, sanitiseOscSubscriptions } from '../parserFunctions.js';
|
||||||
|
|
||||||
describe('sanitiseOscSubscriptions()', () => {
|
describe('sanitiseOscSubscriptions()', () => {
|
||||||
it('returns an empty array if not an array', () => {
|
it('returns an empty array if not an array', () => {
|
||||||
@@ -71,3 +71,87 @@ describe('sanitiseHttpSubscriptions()', () => {
|
|||||||
expect(sanitationResult.length).toBe(0);
|
expect(sanitationResult.length).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('sanitiseCustomFields()', () => {
|
||||||
|
it('returns an empty array if not an array', () => {
|
||||||
|
expect(sanitiseCustomFields({})).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an object of valid entries', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
test: { label: 'test', type: 'string', colour: 'red' },
|
||||||
|
test2: { label: 'test2', type: 'string', colour: 'green' },
|
||||||
|
test3: { label: 'Test3', type: 'string', colour: '' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual(customFields);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('type is forced to be string', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
// @ts-expect-error intentional bad data
|
||||||
|
test: { label: 'test', type: 'another', colour: 'red' },
|
||||||
|
};
|
||||||
|
const expectedCustomFields: CustomFields = {
|
||||||
|
test: { label: 'test', type: 'string', colour: 'red' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual(expectedCustomFields);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('colour must be a string', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
// @ts-expect-error intentional bad data
|
||||||
|
test: { label: 'test', type: 'string', colour: 5 },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('label can not be empty', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
['']: { label: '', type: 'string', colour: 'red' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('remove extra stuff', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
// @ts-expect-error intentional bad data
|
||||||
|
test: { label: 'test', type: 'string', colour: 'red', extra: 'should be removed' },
|
||||||
|
};
|
||||||
|
const expectedCustomFields: CustomFields = {
|
||||||
|
test: { label: 'test', type: 'string', colour: 'red' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual(expectedCustomFields);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('enforece name cohesion', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
test: { label: 'New Name', type: 'string', colour: 'red' },
|
||||||
|
};
|
||||||
|
const expectedCustomFields: CustomFields = {
|
||||||
|
['new name']: { label: 'New Name', type: 'string', colour: 'red' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual(expectedCustomFields);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters invalid entries', () => {
|
||||||
|
const customFields: CustomFields = {
|
||||||
|
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: '' },
|
||||||
|
};
|
||||||
|
const expectedCustomFields: CustomFields = {
|
||||||
|
test: { label: 'test', type: 'string', colour: 'red' },
|
||||||
|
test2: { label: 'test2', type: 'string', colour: 'green' },
|
||||||
|
test3: { label: 'Test3', type: 'string', colour: '' },
|
||||||
|
};
|
||||||
|
const sanitationResult = sanitiseCustomFields(customFields);
|
||||||
|
expect(sanitationResult).toStrictEqual(expectedCustomFields);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -248,20 +248,24 @@ export const parseCustomFields = (data: Partial<DatabaseModel>): CustomFields =>
|
|||||||
if (typeof data.customFields !== 'object') {
|
if (typeof data.customFields !== 'object') {
|
||||||
return { ...dbModel.customFields };
|
return { ...dbModel.customFields };
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Found Custom Fields, importing...');
|
console.log('Found Custom Fields, importing...');
|
||||||
|
|
||||||
|
return sanitiseCustomFields(data.customFields);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const sanitiseCustomFields = (data: object): CustomFields => {
|
||||||
const newCustomFields: CustomFields = {};
|
const newCustomFields: CustomFields = {};
|
||||||
|
|
||||||
for (const fieldLabel in data.customFields) {
|
for (const fieldLabel in data) {
|
||||||
const field = data.customFields[fieldLabel];
|
const field = data[fieldLabel];
|
||||||
if (!field.label || !field.type || !field.colour) {
|
if (!('label' in field) || field.label === '' || !('colour' in field) || typeof field.colour != 'string') {
|
||||||
console.log('ERROR: missing required field, skipping');
|
console.log('ERROR: missing required field, skipping');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = field.label.toLowerCase();
|
const key = field.label.toLowerCase();
|
||||||
newCustomFields[key] = {
|
newCustomFields[key] = {
|
||||||
type: field.type,
|
type: 'string',
|
||||||
colour: field.colour,
|
colour: field.colour,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user