function serializeBasicTypes()

in sdk/core/core-client/src/serializer.ts [408:447]


function serializeBasicTypes(typeName: string, objectName: string, value: any): any {
  if (value !== null && value !== undefined) {
    if (typeName.match(/^Number$/i) !== null) {
      if (typeof value !== "number") {
        throw new Error(`${objectName} with value ${value} must be of type number.`);
      }
    } else if (typeName.match(/^String$/i) !== null) {
      if (typeof value.valueOf() !== "string") {
        throw new Error(`${objectName} with value "${value}" must be of type string.`);
      }
    } else if (typeName.match(/^Uuid$/i) !== null) {
      if (!(typeof value.valueOf() === "string" && isValidUuid(value))) {
        throw new Error(
          `${objectName} with value "${value}" must be of type string and a valid uuid.`,
        );
      }
    } else if (typeName.match(/^Boolean$/i) !== null) {
      if (typeof value !== "boolean") {
        throw new Error(`${objectName} with value ${value} must be of type boolean.`);
      }
    } else if (typeName.match(/^Stream$/i) !== null) {
      const objectType = typeof value;
      if (
        objectType !== "string" &&
        typeof value.pipe !== "function" && // NodeJS.ReadableStream
        typeof value.tee !== "function" && // browser ReadableStream
        !(value instanceof ArrayBuffer) &&
        !ArrayBuffer.isView(value) &&
        // File objects count as a type of Blob, so we want to use instanceof explicitly
        !((typeof Blob === "function" || typeof Blob === "object") && value instanceof Blob) &&
        objectType !== "function"
      ) {
        throw new Error(
          `${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, ReadableStream, or () => ReadableStream.`,
        );
      }
    }
  }
  return value;
}