/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

export type PatchOperation = {
  /**
   * The type of operation to perform
   */
  op: string;
  /**
   * A JSON Pointer string specifying the part of the document to operate on
   */
  path: string;
  /**
   * A JSON value used in "add", "replace", and "test" operations
   */
  value?: any | undefined;
};

/** @internal */
export const PatchOperation$inboundSchema: z.ZodType<
  PatchOperation,
  z.ZodTypeDef,
  unknown
> = z.object({
  op: z.string(),
  path: z.string(),
  value: z.any().optional(),
});
/** @internal */
export type PatchOperation$Outbound = {
  op: string;
  path: string;
  value?: any | undefined;
};

/** @internal */
export const PatchOperation$outboundSchema: z.ZodType<
  PatchOperation$Outbound,
  z.ZodTypeDef,
  PatchOperation
> = z.object({
  op: z.string(),
  path: z.string(),
  value: z.any().optional(),
});

export function patchOperationToJSON(patchOperation: PatchOperation): string {
  return JSON.stringify(PatchOperation$outboundSchema.parse(patchOperation));
}
export function patchOperationFromJSON(
  jsonString: string,
): SafeParseResult<PatchOperation, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => PatchOperation$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'PatchOperation' from JSON`,
  );
}
