123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { TopicMessageSchema } from 'generated-sources';
- import Ajv from 'ajv/dist/2020';
- import { upperFirst } from 'lodash';
- const validateBySchema = (
- value: string,
- schema: string | undefined,
- type: 'key' | 'content'
- ) => {
- let errors: string[] = [];
- if (!value || !schema) {
- return errors;
- }
- let parcedSchema;
- let parsedValue;
- try {
- parcedSchema = JSON.parse(schema);
- } catch (e) {
- return [`Error in parsing the "${type}" field schema`];
- }
- if (parcedSchema.type === 'string') {
- return [];
- }
- try {
- parsedValue = JSON.parse(value);
- } catch (e) {
- return [`Error in parsing the "${type}" field value`];
- }
- try {
- const validate = new Ajv().compile(parcedSchema);
- validate(parsedValue);
- if (validate.errors) {
- errors = validate.errors.map(
- ({ schemaPath, message }) =>
- `${schemaPath.replace('#', upperFirst(type))} - ${message}`
- );
- }
- } catch (e) {
- return [`${upperFirst(type)} ${e.message}`];
- }
- return errors;
- };
- const validateMessage = (
- key: string,
- content: string,
- messageSchema: TopicMessageSchema | undefined
- ): string[] => [
- ...validateBySchema(key, messageSchema?.key?.schema, 'key'),
- ...validateBySchema(content, messageSchema?.value?.schema, 'content'),
- ];
- export default validateMessage;
|