|
4 | 4 | CompositeEvaluationMetric, |
5 | 5 | CompositeEvaluationResultError, |
6 | 6 | CompositeEvaluationResultMetadata, |
| 7 | + EvaluationConfiguration, |
7 | 8 | EvaluationResultValue, |
8 | 9 | EvaluationType, |
9 | 10 | EvaluationV2, |
@@ -40,28 +41,15 @@ export const CompositeEvaluationSpecification = { |
40 | 41 | metrics: METRICS, |
41 | 42 | } |
42 | 43 |
|
43 | | -async function validate<M extends CompositeEvaluationMetric>( |
44 | | - { |
45 | | - metric, |
46 | | - uuid, |
47 | | - configuration, |
48 | | - evaluations, |
49 | | - ...rest |
50 | | - }: EvaluationMetricValidateArgs<EvaluationType.Composite, M> & { |
51 | | - metric: M |
52 | | - }, |
53 | | - db = database, |
54 | | -) { |
55 | | - const metricSpecification = METRICS[metric] |
56 | | - if (!metricSpecification) { |
57 | | - return Result.error(new BadRequestError('Invalid metric')) |
58 | | - } |
59 | | - |
60 | | - const parsing = metricSpecification.configuration.safeParse(configuration) |
61 | | - if (parsing.error) { |
62 | | - return Result.error(parsing.error) |
63 | | - } |
64 | | - |
| 44 | +async function validateSubEvaluations<M extends CompositeEvaluationMetric>({ |
| 45 | + uuid, |
| 46 | + configuration, |
| 47 | + evaluations, |
| 48 | +}: { |
| 49 | + uuid?: string |
| 50 | + configuration: EvaluationConfiguration<EvaluationType.Composite, M> |
| 51 | + evaluations: EvaluationV2[] |
| 52 | +}) { |
65 | 53 | if (configuration.evaluationUuids?.length === 0) { |
66 | 54 | return Result.error(new BadRequestError('Sub-evaluations are required')) |
67 | 55 | } |
@@ -115,6 +103,49 @@ async function validate<M extends CompositeEvaluationMetric>( |
115 | 103 | } |
116 | 104 | } |
117 | 105 |
|
| 106 | + return Result.ok(configuration) |
| 107 | +} |
| 108 | + |
| 109 | +async function validate<M extends CompositeEvaluationMetric>( |
| 110 | + { |
| 111 | + metric, |
| 112 | + uuid, |
| 113 | + configuration, |
| 114 | + evaluations, |
| 115 | + ...rest |
| 116 | + }: EvaluationMetricValidateArgs<EvaluationType.Composite, M> & { |
| 117 | + metric: M |
| 118 | + }, |
| 119 | + db = database, |
| 120 | +) { |
| 121 | + const metricSpecification = METRICS[metric] |
| 122 | + if (!metricSpecification) { |
| 123 | + return Result.error(new BadRequestError('Invalid metric')) |
| 124 | + } |
| 125 | + |
| 126 | + const parsing = metricSpecification.configuration.safeParse(configuration) |
| 127 | + if (parsing.error) { |
| 128 | + return Result.error(parsing.error) |
| 129 | + } |
| 130 | + |
| 131 | + const validating = await validateSubEvaluations({ uuid, configuration, evaluations }) // prettier-ignore |
| 132 | + if (validating.error) { |
| 133 | + if (validating.error instanceof BadRequestError) { |
| 134 | + return Result.error( |
| 135 | + new z.ZodError([ |
| 136 | + { |
| 137 | + code: 'custom', |
| 138 | + path: ['evaluationUuids'], |
| 139 | + message: validating.error.message, |
| 140 | + }, |
| 141 | + ]), |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + return Result.error(validating.error) |
| 146 | + } |
| 147 | + configuration = validating.value |
| 148 | + |
118 | 149 | if ( |
119 | 150 | configuration.minThreshold !== undefined && |
120 | 151 | (configuration.minThreshold < 0 || configuration.minThreshold > 100) |
|
0 commit comments