feat(messages): Allow Custom Fields in Messages (#32224)
parent
d134a4d714
commit
c47a8e3514
@ -0,0 +1,7 @@ |
||||
--- |
||||
"@rocket.chat/meteor": minor |
||||
"@rocket.chat/core-typings": minor |
||||
"@rocket.chat/i18n": minor |
||||
--- |
||||
|
||||
Allow Custom Fields in Messages. API-only feature. It can be enabled and configured in Workspace Settings. |
||||
@ -0,0 +1,44 @@ |
||||
import Ajv from 'ajv'; |
||||
import mem from 'mem'; |
||||
|
||||
const ajv = new Ajv(); |
||||
|
||||
const customFieldsValidate = mem( |
||||
(customFieldsSetting: string) => { |
||||
const schema = JSON.parse(customFieldsSetting); |
||||
|
||||
if (schema.type && schema.type !== 'object') { |
||||
throw new Error('Invalid custom fields config'); |
||||
} |
||||
|
||||
return ajv.compile({ |
||||
...schema, |
||||
type: 'object', |
||||
additionalProperties: false, |
||||
}); |
||||
}, |
||||
{ maxAge: 1000 * 60 }, |
||||
); |
||||
|
||||
export const validateCustomMessageFields = ({ |
||||
customFields, |
||||
messageCustomFieldsEnabled, |
||||
messageCustomFields, |
||||
}: { |
||||
customFields: Record<string, any>; |
||||
messageCustomFieldsEnabled: boolean; |
||||
messageCustomFields: string; |
||||
}) => { |
||||
// get the json schema for the custom fields of the message and validate it using ajv
|
||||
// if the validation fails, throw an error
|
||||
// if there are no custom fields, the message object remains unchanged
|
||||
|
||||
if (messageCustomFieldsEnabled !== true) { |
||||
throw new Error('Custom fields not enabled'); |
||||
} |
||||
|
||||
const validate = customFieldsValidate(messageCustomFields); |
||||
if (!validate(customFields)) { |
||||
throw new Error('Invalid custom fields'); |
||||
} |
||||
}; |
||||
@ -0,0 +1,86 @@ |
||||
import { expect } from 'chai'; |
||||
|
||||
import { validateCustomMessageFields } from '../../../../../../app/lib/server/lib/validateCustomMessageFields'; |
||||
|
||||
describe('validateCustomMessageFields', () => { |
||||
describe('When not enabled', () => { |
||||
it('should not allow to pass custom fields', () => { |
||||
const customFields = { |
||||
test: 'test', |
||||
}; |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: false, messageCustomFields: '' })).to.throw( |
||||
'Custom fields not enabled', |
||||
); |
||||
}); |
||||
}); |
||||
|
||||
describe('When enabled', () => { |
||||
it('should not allow to pass invalid custom fields config', () => { |
||||
const customFields = { |
||||
test: 'test', |
||||
}; |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: true, messageCustomFields: '' })).to.throw( |
||||
'Unexpected end of JSON input', |
||||
); |
||||
}); |
||||
|
||||
it('should not allow to pass a property not present in config', () => { |
||||
const customFields = { |
||||
test: 'test', |
||||
}; |
||||
const messageCustomFields = JSON.stringify({ |
||||
properties: { |
||||
priority: { |
||||
type: 'string', |
||||
}, |
||||
}, |
||||
additionalProperties: true, |
||||
}); |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: true, messageCustomFields })).to.throw( |
||||
'Invalid custom fields', |
||||
); |
||||
}); |
||||
|
||||
it('should not allow to pass an invalid custom field value', () => { |
||||
const customFields = { |
||||
test: 123, |
||||
}; |
||||
const messageCustomFields = JSON.stringify({ |
||||
properties: { |
||||
priority: { |
||||
type: 'string', |
||||
}, |
||||
}, |
||||
additionalProperties: true, |
||||
}); |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: true, messageCustomFields })).to.throw( |
||||
'Invalid custom fields', |
||||
); |
||||
}); |
||||
|
||||
it('should not allow to pass anything different from an object', () => { |
||||
const customFields = [1, 2]; |
||||
const messageCustomFields = JSON.stringify({ |
||||
type: 'array', |
||||
items: [{ type: 'integer' }, { type: 'integer' }], |
||||
}); |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: true, messageCustomFields })).to.throw( |
||||
'Invalid custom fields config', |
||||
); |
||||
}); |
||||
|
||||
it('should allow to pass a valid custom fields config', () => { |
||||
const customFields = { |
||||
test: 'test', |
||||
}; |
||||
const messageCustomFields = JSON.stringify({ |
||||
properties: { |
||||
test: { |
||||
type: 'string', |
||||
}, |
||||
}, |
||||
}); |
||||
expect(() => validateCustomMessageFields({ customFields, messageCustomFieldsEnabled: true, messageCustomFields })).to.not.throw(); |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue