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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  EncryptedPatchValue,
  EncryptedPatchValue$inboundSchema,
} from "./encryptedpatchvalue.js";

/**
 * The value to use for the operation. A string to append to the existing value, or an EncryptedPatchValue wrapper when encryption is applied.
 */
export type JSONPatchAppendValue = EncryptedPatchValue | string;

export type JSONPatchAppend = {
  /**
   * A JSON Pointer (RFC 6901) identifying the target location within the document. Can be a string path (e.g., '/foo/bar'), '/', '', or an empty list [] for root-level operations.
   */
  path: string;
  /**
   * The value to use for the operation. A string to append to the existing value, or an EncryptedPatchValue wrapper when encryption is applied.
   */
  value: EncryptedPatchValue | string;
  /**
   * 'append' is an extension for efficient string concatenation in streaming scenarios.
   */
  op: "append";
};

/** @internal */
export const JSONPatchAppendValue$inboundSchema: z.ZodType<
  JSONPatchAppendValue,
  unknown
> = smartUnion([EncryptedPatchValue$inboundSchema, z.string()]);

export function jsonPatchAppendValueFromJSON(
  jsonString: string,
): SafeParseResult<JSONPatchAppendValue, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => JSONPatchAppendValue$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'JSONPatchAppendValue' from JSON`,
  );
}

/** @internal */
export const JSONPatchAppend$inboundSchema: z.ZodType<
  JSONPatchAppend,
  unknown
> = z.object({
  path: z.string(),
  value: smartUnion([EncryptedPatchValue$inboundSchema, z.string()]),
  op: z.literal("append"),
});

export function jsonPatchAppendFromJSON(
  jsonString: string,
): SafeParseResult<JSONPatchAppend, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => JSONPatchAppend$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'JSONPatchAppend' from JSON`,
  );
}
