UNPKG

voyageai

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fvoyage-ai%2Ftypescript-sdk) [![npm shield](

41 lines (40 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.undiscriminatedUnion = void 0; const Schema_1 = require("../../Schema"); const maybeSkipValidation_1 = require("../../utils/maybeSkipValidation"); const schema_utils_1 = require("../schema-utils"); function undiscriminatedUnion(schemas) { const baseSchema = { parse: (raw, opts) => { return validateAndTransformUndiscriminatedUnion((schema, opts) => schema.parse(raw, opts), schemas, opts); }, json: (parsed, opts) => { return validateAndTransformUndiscriminatedUnion((schema, opts) => schema.json(parsed, opts), schemas, opts); }, getType: () => Schema_1.SchemaType.UNDISCRIMINATED_UNION, }; return Object.assign(Object.assign({}, (0, maybeSkipValidation_1.maybeSkipValidation)(baseSchema)), (0, schema_utils_1.getSchemaUtils)(baseSchema)); } exports.undiscriminatedUnion = undiscriminatedUnion; function validateAndTransformUndiscriminatedUnion(transform, schemas, opts) { const errors = []; for (const [index, schema] of schemas.entries()) { const transformed = transform(schema, Object.assign(Object.assign({}, opts), { skipValidation: false })); if (transformed.ok) { return transformed; } else { for (const error of transformed.errors) { errors.push({ path: error.path, message: `[Variant ${index}] ${error.message}`, }); } } } return { ok: false, errors, }; }