File
business-events
(Optional)
|
Type
|
literal type[]
|
deprecated
(Optional)
|
Type
|
string
|
design-approved
(Optional)
|
Type
|
string
|
validations
(Optional)
|
Type
|
Record<string | >
|
import { VALID_LOBS } from '../constants';
export interface BbMarkdownAttributes {
title: string;
channel: string[];
lob: string[];
validations?: Record<string, []>;
deprecated?: string;
'design-approved'?: string;
'business-events'?: {
id: string;
description: string;
}[];
}
const REQUIRED_MD_ATTRIBUTES: (keyof BbMarkdownAttributes)[] = ['title', 'channel', 'lob'];
const VALID_CHANNELS = ['expert', 'retail'];
export const getMissingAttributes = (
attrs: BbMarkdownAttributes
): (keyof BbMarkdownAttributes)[] => {
return REQUIRED_MD_ATTRIBUTES.filter((key) => !attrs[key]);
};
export const isValidLOB = (lob: string) => {
return VALID_LOBS.has(lob);
};
export const isValidChannel = (channel: string) => {
return VALID_CHANNELS.includes(channel);
};
export const validateMarkdownAttributes = (attrs: BbMarkdownAttributes) => {
const errors: { error: string; details?: string[] }[] = [];
const missingAttributes = getMissingAttributes(attrs);
if (missingAttributes.length) {
errors.push({
error: 'missing attributes',
details: missingAttributes
});
}
if (attrs.channel?.length) {
const invalidChannels = attrs.channel.filter((ch) => !isValidChannel(ch));
if (invalidChannels.length) {
errors.push({
error: 'invalid channels',
details: invalidChannels
});
}
} else if (attrs.channel) {
errors.push({
error: 'no channels defined'
});
}
if (attrs.lob?.length) {
const invalidLOBs = attrs.lob.filter((lob) => !isValidLOB(lob));
if (invalidLOBs.length) {
errors.push({
error: 'invalid LOBs',
details: invalidLOBs
});
}
} else if (attrs.lob) {
errors.push({
error: 'no LOB defined'
});
}
return errors;
};