From cee1cf0ce245f1e4e3ca7edebe28d7d0278f2c4b Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Mon, 13 May 2024 09:56:50 +0530 Subject: [PATCH] feat: Remove forced optionality of fields inside MermaidConfig --- .../scripts/create-types-from-json-schema.mts | 32 +- packages/mermaid/src/config.type.ts | 308 +++++++++--------- 2 files changed, 161 insertions(+), 179 deletions(-) diff --git a/packages/mermaid/scripts/create-types-from-json-schema.mts b/packages/mermaid/scripts/create-types-from-json-schema.mts index a300ef00f..82e21695c 100644 --- a/packages/mermaid/scripts/create-types-from-json-schema.mts +++ b/packages/mermaid/scripts/create-types-from-json-schema.mts @@ -10,13 +10,13 @@ /* eslint-disable no-console */ -import { readFile, writeFile } from 'node:fs/promises'; -import { join } from 'node:path'; import assert from 'node:assert'; import { execFile } from 'node:child_process'; +import { readFile, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; import { promisify } from 'node:util'; -import { load, JSON_SCHEMA } from 'js-yaml'; +import { JSON_SCHEMA, load } from 'js-yaml'; import { compile, type JSONSchema } from 'json-schema-to-typescript'; import prettier from 'prettier'; @@ -131,23 +131,6 @@ async function generateTypescript(mermaidConfigSchema: JSONSchemaType`. - * - * @todo TODO: Remove this function when Mermaid releases a new breaking change. - * @param schema - The input schema. - * @returns The schema with all required values removed. - */ - function removeRequired(schema: JSONSchemaType>) { - return { ...schema, required: [] }; - } - /** * This is a temporary hack to control the order the types are generated in. * @@ -161,7 +144,7 @@ async function generateTypescript(mermaidConfigSchema: JSONSchemaType>) { + function unrefSubschemas(schema: JSONSchemaType) { return { ...schema, properties: Object.fromEntries( @@ -194,17 +177,16 @@ async function generateTypescript(mermaidConfigSchema: JSONSchemaType { - return [key, removeRequired(replaceAllOfWithExtends(subSchema))]; + return [key, replaceAllOfWithExtends(subSchema as JSONSchemaType>)]; }) ), }; const typescriptFile = await compile( - modifiedSchema as JSONSchema, // json-schema-to-typescript only allows JSON Schema 4 as input type + modifiedSchema as unknown as JSONSchema, // json-schema-to-typescript only allows JSON Schema 4 as input type 'MermaidConfig', { additionalProperties: false, // in JSON Schema 2019-09, these are called `unevaluatedProperties` diff --git a/packages/mermaid/src/config.type.ts b/packages/mermaid/src/config.type.ts index 775d49fb5..8d12a0f79 100644 --- a/packages/mermaid/src/config.type.ts +++ b/packages/mermaid/src/config.type.ts @@ -81,27 +81,27 @@ export interface MermaidConfig { * See https://developer.mozilla.org/en-US/docs/Web/CSS/font-family * */ - fontFamily?: string; + fontFamily: string; altFontFamily?: string; /** * This option decides the amount of logging to be used by mermaid. * */ - logLevel?: 'trace' | 0 | 'debug' | 1 | 'info' | 2 | 'warn' | 3 | 'error' | 4 | 'fatal' | 5; + logLevel: 'trace' | 0 | 'debug' | 1 | 'info' | 2 | 'warn' | 3 | 'error' | 4 | 'fatal' | 5; /** * Level of trust for parsed diagram */ - securityLevel?: 'strict' | 'loose' | 'antiscript' | 'sandbox'; + securityLevel: 'strict' | 'loose' | 'antiscript' | 'sandbox'; /** * Dictates whether mermaid starts on Page load */ - startOnLoad?: boolean; + startOnLoad: boolean; /** * Controls whether or arrow markers in html code are absolute paths or anchors. * This matters if you are using base tag settings. * */ - arrowMarkerAbsolute?: boolean; + arrowMarkerAbsolute: boolean; /** * This option controls which `currentConfig` keys are considered secure and * can only be changed via call to `mermaidAPI.initialize`. @@ -145,24 +145,24 @@ export interface MermaidConfig { * */ deterministicIDSeed?: string; - flowchart?: FlowchartDiagramConfig; - sequence?: SequenceDiagramConfig; - gantt?: GanttDiagramConfig; - journey?: JourneyDiagramConfig; + flowchart: FlowchartDiagramConfig; + sequence: SequenceDiagramConfig; + gantt: GanttDiagramConfig; + journey: JourneyDiagramConfig; timeline?: TimelineDiagramConfig; - class?: ClassDiagramConfig; - state?: StateDiagramConfig; - er?: ErDiagramConfig; - pie?: PieDiagramConfig; - quadrantChart?: QuadrantChartConfig; - xyChart?: XYChartConfig; - requirement?: RequirementDiagramConfig; - mindmap?: MindmapDiagramConfig; - gitGraph?: GitGraphDiagramConfig; - c4?: C4DiagramConfig; - sankey?: SankeyDiagramConfig; - packet?: PacketDiagramConfig; - block?: BlockDiagramConfig; + class: ClassDiagramConfig; + state: StateDiagramConfig; + er: ErDiagramConfig; + pie: PieDiagramConfig; + quadrantChart: QuadrantChartConfig; + xyChart: XYChartConfig; + requirement: RequirementDiagramConfig; + mindmap: MindmapDiagramConfig; + gitGraph: GitGraphDiagramConfig; + c4: C4DiagramConfig; + sankey: SankeyDiagramConfig; + packet: PacketDiagramConfig; + block: BlockDiagramConfig; dompurifyConfig?: DOMPurifyConfiguration; wrap?: boolean; fontSize?: number; @@ -240,41 +240,41 @@ export interface C4DiagramConfig extends BaseDiagramConfig { * Margin to the right and left of the c4 diagram, must be a positive value. * */ - diagramMarginX?: number; + diagramMarginX: number; /** * Margin to the over and under the c4 diagram, must be a positive value. * */ - diagramMarginY?: number; + diagramMarginY: number; /** * Margin between shapes */ - c4ShapeMargin?: number; + c4ShapeMargin: number; /** * Padding between shapes */ - c4ShapePadding?: number; + c4ShapePadding: number; /** * Width of person boxes */ - width?: number; + width: number; /** * Height of person boxes */ - height?: number; + height: number; /** * Margin around boxes */ - boxMargin?: number; + boxMargin: number; /** * How many shapes to place in each row. */ - c4ShapeInRow?: number; + c4ShapeInRow: number; nextLinePaddingX?: number; /** * How many boundaries to place in each row. */ - c4BoundaryInRow?: number; + c4BoundaryInRow: number; /** * This sets the font size of Person shape for the diagram */ @@ -618,7 +618,7 @@ export interface GitGraphDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; diagramPadding?: number; nodeLabel?: NodeLabel; mainBranchName?: string; @@ -668,8 +668,8 @@ export interface RequirementDiagramConfig extends BaseDiagramConfig { * via the `definition` "MindmapDiagramConfig". */ export interface MindmapDiagramConfig extends BaseDiagramConfig { - padding?: number; - maxNodeWidth?: number; + padding: number; + maxNodeWidth: number; } /** * This interface was referenced by `MermaidConfig`'s JSON-Schema @@ -690,75 +690,75 @@ export interface QuadrantChartConfig extends BaseDiagramConfig { /** * Width of the chart */ - chartWidth?: number; + chartWidth: number; /** * Height of the chart */ - chartHeight?: number; + chartHeight: number; /** * Chart title top and bottom padding */ - titleFontSize?: number; + titleFontSize: number; /** * Padding around the quadrant square */ - titlePadding?: number; + titlePadding: number; /** * quadrant title padding from top if the quadrant is rendered on top */ - quadrantPadding?: number; + quadrantPadding: number; /** * Padding around x-axis labels */ - xAxisLabelPadding?: number; + xAxisLabelPadding: number; /** * Padding around y-axis labels */ - yAxisLabelPadding?: number; + yAxisLabelPadding: number; /** * x-axis label font size */ - xAxisLabelFontSize?: number; + xAxisLabelFontSize: number; /** * y-axis label font size */ - yAxisLabelFontSize?: number; + yAxisLabelFontSize: number; /** * quadrant title font size */ - quadrantLabelFontSize?: number; + quadrantLabelFontSize: number; /** * quadrant title padding from top if the quadrant is rendered on top */ - quadrantTextTopPadding?: number; + quadrantTextTopPadding: number; /** * padding between point and point label */ - pointTextPadding?: number; + pointTextPadding: number; /** * point title font size */ - pointLabelFontSize?: number; + pointLabelFontSize: number; /** * radius of the point to be drawn */ - pointRadius?: number; + pointRadius: number; /** * position of x-axis labels */ - xAxisPosition?: 'top' | 'bottom'; + xAxisPosition: 'top' | 'bottom'; /** * position of y-axis labels */ - yAxisPosition?: 'left' | 'right'; + yAxisPosition: 'left' | 'right'; /** * stroke width of edges of the box that are inside the quadrant */ - quadrantInternalBorderStrokeWidth?: number; + quadrantInternalBorderStrokeWidth: number; /** * stroke width of edges of the box that are outside the quadrant */ - quadrantExternalBorderStrokeWidth?: number; + quadrantExternalBorderStrokeWidth: number; } /** * This object contains configuration for XYChart axis config @@ -770,47 +770,47 @@ export interface XYChartAxisConfig { /** * Should show the axis labels (tick text) */ - showLabel?: boolean; + showLabel: boolean; /** * font size of the axis labels (tick text) */ - labelFontSize?: number; + labelFontSize: number; /** * top and bottom space from axis label (tick text) */ - labelPadding?: number; + labelPadding: number; /** * Should show the axis title */ - showTitle?: boolean; + showTitle: boolean; /** * font size of the axis title */ - titleFontSize?: number; + titleFontSize: number; /** * top and bottom space from axis title */ - titlePadding?: number; + titlePadding: number; /** * Should show the axis tick lines */ - showTick?: boolean; + showTick: boolean; /** * length of the axis tick lines */ - tickLength?: number; + tickLength: number; /** * width of the axis tick lines */ - tickWidth?: number; + tickWidth: number; /** * Show line across the axis */ - showAxisLine?: boolean; + showAxisLine: boolean; /** * Width of the axis line */ - axisLineWidth?: number; + axisLineWidth: number; } /** * This object contains configuration specific to XYCharts @@ -822,33 +822,33 @@ export interface XYChartConfig extends BaseDiagramConfig { /** * width of the chart */ - width?: number; + width: number; /** * height of the chart */ - height?: number; + height: number; /** * Font size of the chart title */ - titleFontSize?: number; + titleFontSize: number; /** * Top and bottom space from the chart title */ - titlePadding?: number; + titlePadding: number; /** * Should show the chart title */ - showTitle?: boolean; - xAxis?: XYChartAxisConfig; - yAxis?: XYChartAxisConfig; + showTitle: boolean; + xAxis: XYChartAxisConfig; + yAxis: XYChartAxisConfig; /** * How to plot will be drawn horizontal or vertical */ - chartOrientation?: 'vertical' | 'horizontal'; + chartOrientation: 'vertical' | 'horizontal'; /** * Minimum percent of space plots of the chart will take */ - plotReservedSpacePercent?: number; + plotReservedSpacePercent: number; } /** * The object containing configurations specific for entity relationship diagrams @@ -860,39 +860,39 @@ export interface ErDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; /** * The amount of padding around the diagram as a whole so that embedded * diagrams have margins, expressed in pixels. * */ - diagramPadding?: number; + diagramPadding: number; /** * Directional bias for layout of entities */ - layoutDirection?: 'TB' | 'BT' | 'LR' | 'RL'; + layoutDirection: 'TB' | 'BT' | 'LR' | 'RL'; /** * The minimum width of an entity box. Expressed in pixels. */ - minEntityWidth?: number; + minEntityWidth: number; /** * The minimum height of an entity box. Expressed in pixels. */ - minEntityHeight?: number; + minEntityHeight: number; /** * The minimum internal padding between text in an entity box and the enclosing box borders. * Expressed in pixels. * */ - entityPadding?: number; + entityPadding: number; /** * Stroke color of box edges and lines. */ - stroke?: string; + stroke: string; /** * Fill color of entity boxes */ - fill?: string; + fill: string; /** * Font size (expressed as an integer representing a number of pixels) */ @@ -908,7 +908,7 @@ export interface StateDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; arrowMarkerAbsolute?: boolean; dividerMargin?: number; sizeUnit?: number; @@ -935,7 +935,7 @@ export interface StateDiagramConfig extends BaseDiagramConfig { * Decides which rendering engine that is to be used for the rendering. * */ - defaultRenderer?: 'dagre-d3' | 'dagre-wrapper' | 'elk'; + defaultRenderer: 'dagre-d3' | 'dagre-wrapper' | 'elk'; } /** * This interface was referenced by `MermaidConfig`'s JSON-Schema @@ -945,7 +945,7 @@ export interface ClassDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; /** * Controls whether or arrow markers in html code are absolute paths or anchors. * This matters if you are using base tag settings. @@ -959,7 +959,7 @@ export interface ClassDiagramConfig extends BaseDiagramConfig { * Decides which rendering engine that is to be used for the rendering. * */ - defaultRenderer?: 'dagre-d3' | 'dagre-wrapper' | 'elk'; + defaultRenderer: 'dagre-d3' | 'dagre-wrapper' | 'elk'; nodeSpacing?: number; rankSpacing?: number; /** @@ -982,51 +982,51 @@ export interface JourneyDiagramConfig extends BaseDiagramConfig { * Margin to the right and left of the c4 diagram, must be a positive value. * */ - diagramMarginX?: number; + diagramMarginX: number; /** * Margin to the over and under the c4 diagram, must be a positive value. * */ - diagramMarginY?: number; + diagramMarginY: number; /** * Margin between actors */ - leftMargin?: number; + leftMargin: number; /** * Width of actor boxes */ - width?: number; + width: number; /** * Height of actor boxes */ - height?: number; + height: number; /** * Margin around loop boxes */ - boxMargin?: number; + boxMargin: number; /** * Margin around the text in loop/alt/opt boxes */ - boxTextMargin?: number; + boxTextMargin: number; /** * Margin around notes */ - noteMargin?: number; + noteMargin: number; /** * Space between messages. */ - messageMargin?: number; + messageMargin: number; /** * Multiline message alignment */ - messageAlign?: 'left' | 'center' | 'right'; + messageAlign: 'left' | 'center' | 'right'; /** * Prolongs the edge of the diagram downwards. * * Depending on css styling this might need adjustment. * */ - bottomMarginAdj?: number; + bottomMarginAdj: number; /** * Curved Arrows become Right Angles * @@ -1034,7 +1034,7 @@ export interface JourneyDiagramConfig extends BaseDiagramConfig { * right angles, rather than as curves. * */ - rightAngles?: boolean; + rightAngles: boolean; taskFontSize?: string | number; taskFontFamily?: string; taskMargin?: number; @@ -1060,52 +1060,52 @@ export interface TimelineDiagramConfig extends BaseDiagramConfig { * Margin to the right and left of the c4 diagram, must be a positive value. * */ - diagramMarginX?: number; + diagramMarginX: number; /** * Margin to the over and under the c4 diagram, must be a positive value. * */ - diagramMarginY?: number; + diagramMarginY: number; /** * Margin between actors */ - leftMargin?: number; + leftMargin: number; /** * Width of actor boxes */ - width?: number; + width: number; /** * Height of actor boxes */ - height?: number; + height: number; padding?: number; /** * Margin around loop boxes */ - boxMargin?: number; + boxMargin: number; /** * Margin around the text in loop/alt/opt boxes */ - boxTextMargin?: number; + boxTextMargin: number; /** * Margin around notes */ - noteMargin?: number; + noteMargin: number; /** * Space between messages. */ - messageMargin?: number; + messageMargin: number; /** * Multiline message alignment */ - messageAlign?: 'left' | 'center' | 'right'; + messageAlign: 'left' | 'center' | 'right'; /** * Prolongs the edge of the diagram downwards. * * Depending on css styling this might need adjustment. * */ - bottomMarginAdj?: number; + bottomMarginAdj: number; /** * Curved Arrows become Right Angles * @@ -1142,11 +1142,11 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; /** * The height of the bars in the graph */ - barHeight?: number; + barHeight: number; /** * The margin between the different activities in the gantt diagram */ @@ -1155,40 +1155,40 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * Margin between title and gantt diagram and between axis and gantt diagram. * */ - topPadding?: number; + topPadding: number; /** * The space allocated for the section name to the right of the activities * */ - rightPadding?: number; + rightPadding: number; /** * The space allocated for the section name to the left of the activities * */ - leftPadding?: number; + leftPadding: number; /** * Vertical starting position of the grid lines */ - gridLineStartPadding?: number; + gridLineStartPadding: number; /** * Font size */ - fontSize?: number; + fontSize: number; /** * Font size for sections */ - sectionFontSize?: string | number; + sectionFontSize: string | number; /** * The number of alternating section styles */ - numberSectionStyles?: number; + numberSectionStyles: number; /** * Date/time format of the axis * * This might need adjustment to match your locale and preferences. * */ - axisFormat?: string; + axisFormat: string; /** * axis ticks * @@ -1204,7 +1204,7 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * When this flag is set, date labels will be added to the top of the chart * */ - topAxis?: boolean; + topAxis: boolean; /** * Controls the display mode. * @@ -1214,7 +1214,7 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * On which day a week-based interval should start * */ - weekday?: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'; + weekday: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'; } /** * The object containing configurations specific for sequence diagrams @@ -1228,64 +1228,64 @@ export interface SequenceDiagramConfig extends BaseDiagramConfig { /** * Width of the activation rect */ - activationWidth?: number; + activationWidth: number; /** * Margin to the right and left of the sequence diagram */ - diagramMarginX?: number; + diagramMarginX: number; /** * Margin to the over and under the sequence diagram */ - diagramMarginY?: number; + diagramMarginY: number; /** * Margin between actors */ - actorMargin?: number; + actorMargin: number; /** * Width of actor boxes */ - width?: number; + width: number; /** * Height of actor boxes */ - height?: number; + height: number; /** * Margin around loop boxes */ - boxMargin?: number; + boxMargin: number; /** * Margin around the text in loop/alt/opt boxes */ - boxTextMargin?: number; + boxTextMargin: number; /** * Margin around notes */ - noteMargin?: number; + noteMargin: number; /** * Space between messages. */ - messageMargin?: number; + messageMargin: number; /** * Multiline message alignment */ - messageAlign?: 'left' | 'center' | 'right'; + messageAlign: 'left' | 'center' | 'right'; /** * Mirror actors under diagram * */ - mirrorActors?: boolean; + mirrorActors: boolean; /** * forces actor popup menus to always be visible (to support E2E testing). * */ - forceMenus?: boolean; + forceMenus: boolean; /** * Prolongs the edge of the diagram downwards. * * Depending on css styling this might need adjustment. * */ - bottomMarginAdj?: number; + bottomMarginAdj: number; /** * Curved Arrows become Right Angles * @@ -1293,51 +1293,51 @@ export interface SequenceDiagramConfig extends BaseDiagramConfig { * right angles, rather than as curves. * */ - rightAngles?: boolean; + rightAngles: boolean; /** * This will show the node numbers */ - showSequenceNumbers?: boolean; + showSequenceNumbers: boolean; /** * This sets the font size of the actor's description */ - actorFontSize?: string | number; + actorFontSize: string | number; /** * This sets the font family of the actor's description */ - actorFontFamily?: string; + actorFontFamily: string; /** * This sets the font weight of the actor's description */ - actorFontWeight?: string | number; + actorFontWeight: string | number; /** * This sets the font size of actor-attached notes */ - noteFontSize?: string | number; + noteFontSize: string | number; /** * This sets the font family of actor-attached notes */ - noteFontFamily?: string; + noteFontFamily: string; /** * This sets the font weight of actor-attached notes */ - noteFontWeight?: string | number; + noteFontWeight: string | number; /** * This sets the text alignment of actor-attached notes */ - noteAlign?: 'left' | 'center' | 'right'; + noteAlign: 'left' | 'center' | 'right'; /** * This sets the font size of actor messages */ - messageFontSize?: string | number; + messageFontSize: string | number; /** * This sets the font family of actor messages */ - messageFontFamily?: string; + messageFontFamily: string; /** * This sets the font weight of actor messages */ - messageFontWeight?: string | number; + messageFontWeight: string | number; /** * This sets the auto-wrap state for the diagram */ @@ -1368,12 +1368,12 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { /** * Margin top for the text over the diagram */ - titleTopMargin?: number; + titleTopMargin: number; /** * Defines a top/bottom margin for subgraph titles * */ - subGraphTitleMargin?: { + subGraphTitleMargin: { top?: number; bottom?: number; }; @@ -1383,12 +1383,12 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { * diagrams have margins, expressed in pixels. * */ - diagramPadding?: number; + diagramPadding: number; /** * Flag for setting whether or not a html tag should be used for rendering labels on the edges. * */ - htmlLabels?: boolean; + htmlLabels: boolean; /** * Defines the spacing between nodes on the same level * @@ -1396,7 +1396,7 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { * and the vertical spacing for LR as well as RL graphs. * */ - nodeSpacing?: number; + nodeSpacing: number; /** * Defines the spacing between nodes on different levels * @@ -1404,12 +1404,12 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { * and the vertical spacing for LR as well as RL graphs. * */ - rankSpacing?: number; + rankSpacing: number; /** * Defines how mermaid renders curves for flowcharts. * */ - curve?: 'basis' | 'linear' | 'cardinal'; + curve: 'basis' | 'linear' | 'cardinal'; /** * Represents the padding between the labels and the shape * @@ -1421,7 +1421,7 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { * Decides which rendering engine that is to be used for the rendering. * */ - defaultRenderer?: 'dagre-d3' | 'dagre-wrapper' | 'elk'; + defaultRenderer: 'dagre-d3' | 'dagre-wrapper' | 'elk'; /** * Width of nodes where text is wrapped. * @@ -1429,7 +1429,7 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig { * value sets the max width of a text before it continues on a new line. * */ - wrappingWidth?: number; + wrappingWidth: number; } /** * The object containing configurations specific for sankey diagrams.