|
| 1 | +import type { SchemaReport } from '../types/report' |
| 2 | +import type { BranchCheckable, BranchCheckableImport, BuildableSchema, EvaluableSchema } from '../types/schema' |
| 3 | + |
| 4 | +export async function buildEvaluableUnionSchema<TSchemas extends readonly BuildableSchema<any, any, any>[], TOutput>( |
| 5 | + schemas: TSchemas, |
| 6 | + optionalityBranchCheckableImport: BranchCheckableImport<any> | undefined, |
| 7 | +): Promise<EvaluableSchema<TOutput>> { |
| 8 | + if (schemas.length === 0) { |
| 9 | + throw new Error('Union requires at least one schema') |
| 10 | + } |
| 11 | + |
| 12 | + const optionalityPromise = optionalityBranchCheckableImport?.() ?? undefined |
| 13 | + const evaluableSchemasPromise = Promise.all(schemas.map(schema => schema['~build']())) |
| 14 | + |
| 15 | + const [optionalityBranchCheckable, evaluableSchemas] = await Promise.all([optionalityPromise, evaluableSchemasPromise]) |
| 16 | + |
| 17 | + return buildUnionSchema<TOutput>(evaluableSchemas, optionalityBranchCheckable) |
| 18 | +} |
| 19 | + |
| 20 | +export async function buildEvaluableUnionSchemaWithTransform< |
| 21 | + TSchemas extends readonly BuildableSchema<any, any, any>[], |
| 22 | + TOutput, |
| 23 | + TTransformOutput, |
| 24 | +>( |
| 25 | + schemas: TSchemas, |
| 26 | + optionalityBranchCheckableImport: BranchCheckableImport<any> | undefined, |
| 27 | + transformFn: (input: TOutput) => TTransformOutput, |
| 28 | +): Promise<EvaluableSchema<TTransformOutput>> { |
| 29 | + const baseSchema = await buildEvaluableUnionSchema<TSchemas, TOutput>(schemas, optionalityBranchCheckableImport) |
| 30 | + |
| 31 | + return { |
| 32 | + safeParse: (input: unknown) => { |
| 33 | + const report = baseSchema.safeParse(input) as SchemaReport<TOutput> |
| 34 | + if (report.passed) { |
| 35 | + return { |
| 36 | + ...report, |
| 37 | + value: transformFn(report.value), |
| 38 | + } as SchemaReport<TTransformOutput> |
| 39 | + } |
| 40 | + |
| 41 | + return report as SchemaReport<TTransformOutput> |
| 42 | + }, |
| 43 | + parse: (input: unknown) => { |
| 44 | + const value = baseSchema.parse(input) |
| 45 | + return transformFn(value) |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export function buildUnionSchema<TOutput>( |
| 51 | + evaluableSchemas: readonly EvaluableSchema<any>[], |
| 52 | + optionalityBranchCheckable: BranchCheckable<any> | undefined, |
| 53 | +): EvaluableSchema<TOutput> { |
| 54 | + if (evaluableSchemas.length === 0 && !optionalityBranchCheckable) { |
| 55 | + throw new Error('Union requires at least one schema') |
| 56 | + } |
| 57 | + |
| 58 | + function safeParse(input: unknown): SchemaReport<TOutput> { |
| 59 | + const reports: SchemaReport<TOutput>[] = [] |
| 60 | + |
| 61 | + for (const schema of evaluableSchemas) { |
| 62 | + const report = schema.safeParse(input) as SchemaReport<TOutput> |
| 63 | + if (!report.passed) { |
| 64 | + delete (report as any).value |
| 65 | + } |
| 66 | + reports.push(report) |
| 67 | + } |
| 68 | + |
| 69 | + if (optionalityBranchCheckable) { |
| 70 | + const optionalityReport = optionalityBranchCheckable['~c'](input) as SchemaReport<TOutput> |
| 71 | + if (!optionalityReport.passed) { |
| 72 | + delete optionalityReport.value |
| 73 | + } |
| 74 | + reports.push(optionalityReport) |
| 75 | + } |
| 76 | + |
| 77 | + if (reports.length === 0) { |
| 78 | + throw new Error('Union requires at least one schema') |
| 79 | + } |
| 80 | + |
| 81 | + let bestReport: SchemaReport<TOutput> | undefined |
| 82 | + for (const report of reports) { |
| 83 | + bestReport = bestReport ? selectBetterReport(bestReport, report) : report |
| 84 | + } |
| 85 | + |
| 86 | + if (!bestReport) { |
| 87 | + throw new Error('Union requires at least one schema') |
| 88 | + } |
| 89 | + |
| 90 | + const remainingReports = reports.filter(report => report !== bestReport) |
| 91 | + if (remainingReports.length > 0) { |
| 92 | + const existingUnionReports = bestReport.unionReports ?? [] |
| 93 | + bestReport.unionReports = existingUnionReports.length > 0 |
| 94 | + ? [...existingUnionReports, ...remainingReports] |
| 95 | + : remainingReports |
| 96 | + } |
| 97 | + |
| 98 | + return bestReport |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + safeParse, |
| 103 | + parse: (input: unknown) => { |
| 104 | + const report = safeParse(input) |
| 105 | + if (report.passed) { |
| 106 | + return report.value as TOutput |
| 107 | + } |
| 108 | + throw new Error('Input did not match any union branch') |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function selectBetterReport<TOutput>( |
| 114 | + currentBest: SchemaReport<TOutput>, |
| 115 | + candidate: SchemaReport<TOutput>, |
| 116 | +): SchemaReport<TOutput> { |
| 117 | + if (candidate.passed) { |
| 118 | + if (!currentBest.passed) { |
| 119 | + return candidate |
| 120 | + } |
| 121 | + return candidate.score > currentBest.score ? candidate : currentBest |
| 122 | + } |
| 123 | + |
| 124 | + if (currentBest.passed) { |
| 125 | + return currentBest |
| 126 | + } |
| 127 | + |
| 128 | + return candidate.score > currentBest.score ? candidate : currentBest |
| 129 | +} |
0 commit comments