vorteil/direktiv

View on GitHub
ui/src/pages/namespace/Explorer/Service/ServiceEditor/utils.ts

Summary

Maintainability
A
2 hrs
Test Coverage
import { ServiceFormSchema, ServiceFormSchemaType } from "./schema";
import { jsonToYaml, yamlToJsonOrNull } from "../../utils";

import { ZodError } from "zod";

type SerializeReturnType =
  | [ServiceFormSchemaType, undefined]
  | [undefined, ZodError<ServiceFormSchemaType>];

export const serializeServiceFile = (yaml: string): SerializeReturnType => {
  const json = yamlToJsonOrNull(yaml);

  const jsonParsed = ServiceFormSchema.safeParse(json);
  if (jsonParsed.success) {
    return [jsonParsed.data, undefined];
  }

  return [undefined, jsonParsed.error];
};

const defaultServiceFileJson: ServiceFormSchemaType = {
  direktiv_api: "service/v1",
};

export const defaultServiceYaml = jsonToYaml(defaultServiceFileJson);