Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: serverSide validation error markers #190

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/otelbin/src/components/monaco-editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PanelLeftOpen } from "lucide-react";
import { IconButton } from "~/components/icon-button";
import { Tooltip, TooltipContent, TooltipTrigger } from "~/components/tooltip";
import { track } from "@vercel/analytics";
import { useServerSideValidation } from "../validation/useServerSideValidation";

const firaCode = Fira_Code({
display: "swap",
Expand All @@ -43,6 +44,7 @@ export default function Editor({ locked, setLocked }: { locked: boolean; setLock
const [{ config }, getLink] = useUrlState([editorBinding]);
const [currentConfig, setCurrentConfig] = useState<string>(config);
const clerk = useClerk();
const serverSideValidationResult = useServerSideValidation();

const onWidthChange = useCallback((newWidth: number) => {
localStorage.setItem("width", String(newWidth));
Expand All @@ -60,11 +62,16 @@ export default function Editor({ locked, setLocked }: { locked: boolean; setLock

const totalValidationErrors = useMemo((): IError => {
if (editorRef && monacoRef) {
return validateOtelCollectorConfigurationAndSetMarkers(currentConfig, editorRef, monacoRef);
return validateOtelCollectorConfigurationAndSetMarkers(
currentConfig,
editorRef,
monacoRef,
serverSideValidationResult
);
} else {
return {};
}
}, [currentConfig, editorRef, monacoRef]);
}, [currentConfig, editorRef, monacoRef, serverSideValidationResult]);

const isValidConfig =
totalValidationErrors.jsYamlError == null && (totalValidationErrors.ajvErrors?.length ?? 0) === 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { useEffect, useState } from "react";
import { ChevronDown, XCircle, AlertTriangle } from "lucide-react";
import { type NextFont } from "next/dist/compiled/@next/font";
import { useServerSideValidation } from "../validation/useServerSideValidation";

export interface IAjvError {
message: string;
Expand All @@ -18,20 +17,27 @@ export interface IJsYamlError {
reason: string | null;
}

export interface IServerSideError {
message: string;
error: string;
line: number | null;
path?: string[];
}

export interface IError {
jsYamlError?: IJsYamlError;
ajvErrors?: IAjvError[];
customErrors?: string[];
customWarnings?: string[];
serverSideError?: IServerSideError;
}

export default function ValidationErrorConsole({ errors, font }: { errors?: IError; font: NextFont }) {
const serverSideValidationResult = useServerSideValidation();
const errorCount =
(errors?.ajvErrors?.length ?? 0) +
(errors?.jsYamlError != null ? 1 : 0) +
(errors?.customErrors?.length ?? 0) +
(serverSideValidationResult.result?.error ? 1 : 0);
(errors?.serverSideError?.error ? 1 : 0);

const warningsCount = errors?.customWarnings?.length ?? 0;
const [isOpenErrorConsole, setIsOpenErrorConsole] = useState(false);
Expand Down Expand Up @@ -71,14 +77,7 @@ export default function ValidationErrorConsole({ errors, font }: { errors?: IErr
errors.customWarnings.map((warning: string, index: number) => {
return <ErrorMessage key={index} customWarnings={warning} font={font} />;
})}
{serverSideValidationResult.result?.error && (
<ErrorMessage
serverSideError={
serverSideValidationResult.result?.message + " - " + serverSideValidationResult.result?.error
}
font={font}
/>
)}
{errors?.serverSideError?.error && <ErrorMessage serverSideError={errors.serverSideError} font={font} />}
{errors?.ajvErrors &&
errors.ajvErrors?.length > 0 &&
errors.ajvErrors.map((error: IAjvError, index: number) => {
Expand Down Expand Up @@ -107,7 +106,7 @@ export function ErrorMessage({
}: {
ajvError?: IAjvError;
jsYamlError?: IJsYamlError;
serverSideError?: string;
serverSideError?: IServerSideError;
customErrors?: string;
customWarnings?: string;
font: NextFont;
Expand Down Expand Up @@ -140,7 +139,9 @@ export function ErrorMessage({
)}
{serverSideError ? (
<div className={`${font.className} ${errorsStyle}`}>
<p>{`Server-side: ${serverSideError}`}</p>
<p>{`${serverSideError.message} - ${serverSideError.error} ${
(serverSideError.line ?? 0) > 1 ? `(Line ${serverSideError.line})` : ""
}`}</p>
</div>
) : (
<></>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,12 @@ describe("findErrorElement", () => {

expect(result).toEqual(expectedOutput);
});

it("with both empty parsed yaml doc and empty error path should return undefined", () => {
const result = findErrorElement([], []);

const expectedOutput = undefined;

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
findLineAndColumn,
parseYaml,
} from "./parseYaml";
import type { ValidationState } from "../validation/useServerSideValidation";

type EditorRefType = RefObject<editor.IStandaloneCodeEditor | null>;
type MonacoRefType = RefObject<Monaco | null>;
Expand All @@ -28,7 +29,8 @@ let serviceItemsData: IValidateItem | undefined = {};
export function validateOtelCollectorConfigurationAndSetMarkers(
configData: string,
editorRef: EditorRefType,
monacoRef: MonacoRefType
monacoRef: MonacoRefType,
serverSideValidationResult?: ValidationState
) {
const ajv = new Ajv({ allErrors: true });
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -47,6 +49,7 @@ export function validateOtelCollectorConfigurationAndSetMarkers(
docElements.filter((item: IItem) => item.key?.source === "service")[0],
serviceItemsData
);
const serverSideValidationPath = serverSideValidationResult?.result?.path ?? [];

try {
const jsonData = JsYaml.load(configData);
Expand Down Expand Up @@ -99,6 +102,23 @@ export function validateOtelCollectorConfigurationAndSetMarkers(
}
if (!totalErrors.jsYamlError) {
customValidate(mainItemsData, serviceItemsData, errorMarkers, totalErrors, configData);
const serverSideErrorElement = findErrorElement(serverSideValidationPath, parsedYamlConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add tests for this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the functions that used for serverSide markers are already have tests. (used the same functions as for ajv error markers) @bripkens

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if that is the case, does that mean that this new code can be deleted and there will be a red test? :)

const { line, column } = findLineAndColumn(configData, serverSideErrorElement?.offset);
totalErrors.serverSideError = {
message: serverSideValidationResult?.result?.message ?? "",
error: serverSideValidationResult?.result?.error ?? "",
line: line,
path: serverSideValidationPath,
};
serverSideValidationPath.length > 0 &&
errorMarkers.push({
startLineNumber: line ?? 0,
endLineNumber: 0,
startColumn: column ?? 0,
endColumn: column ?? 0,
severity: 8,
message: serverSideValidationResult?.result?.message + " - " + serverSideValidationResult?.result?.error,
});
model && monacoRef?.current?.editor.setModelMarkers(model, "json", errorMarkers);
}
return totalErrors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { editorBinding } from "~/components/monaco-editor/editorBinding";
import { useEffect, useMemo, useState } from "react";
import { type ServerSideValidationResult } from "~/types";

interface ValidationState {
export interface ValidationState {
isLoading: boolean;
// the config that was/is being validated
config: string;
Expand Down
1 change: 1 addition & 0 deletions packages/otelbin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export interface Release {
export interface ServerSideValidationResult {
message: string;
error: string;
path?: string[];
}