22import { promises as fs } from 'fs' ;
33import * as github from '@actions/github' ;
44import { Config , ToolType } from './config' ;
5+ import { z } from 'zod' ;
56
6- export interface BenchmarkResult {
7- name : string ;
8- value : number ;
9- range ?: string ;
10- unit : string ;
11- extra ?: string ;
12- }
7+ export const BenchmarkResult = z . object ( {
8+ name : z . coerce . string ( ) ,
9+ value : z . coerce . number ( ) ,
10+ range : z . coerce . string ( ) . optional ( ) ,
11+ unit : z . coerce . string ( ) ,
12+ extra : z . coerce . string ( ) . optional ( ) ,
13+ } ) ;
14+
15+ export type BenchmarkResult = z . infer < typeof BenchmarkResult > ;
16+
17+ export const BenchmarkResults = z . array ( BenchmarkResult ) ;
1318
1419interface GitHubUser {
1520 email ?: string ;
@@ -658,13 +663,11 @@ function extractBenchmarkDotnetResult(output: string): BenchmarkResult[] {
658663
659664function extractCustomBenchmarkResult ( output : string ) : BenchmarkResult [ ] {
660665 try {
661- const json : BenchmarkResult [ ] = JSON . parse ( output ) ;
662- return json . map ( ( { name, value, unit, range, extra } ) => {
663- return { name, value, unit, range, extra } ;
664- } ) ;
665- } catch ( err : any ) {
666+ return BenchmarkResults . parse ( JSON . parse ( output ) ) ;
667+ } catch ( err : unknown ) {
668+ const errMessage = err instanceof Error ? err . message : String ( err ) ;
666669 throw new Error (
667- `Output file for 'custom-(bigger|smaller)-is-better' must be JSON file containing an array of entries in BenchmarkResult format: ${ err . message } ` ,
670+ `Output file for 'custom-(bigger|smaller)-is-better' must be JSON file containing an array of entries in BenchmarkResult format: ${ errMessage } ` ,
668671 ) ;
669672 }
670673}
@@ -673,7 +676,6 @@ function extractLuauBenchmarkResult(output: string): BenchmarkResult[] {
673676 const lines = output . split ( / \n / ) ;
674677 const results : BenchmarkResult [ ] = [ ] ;
675678
676- output ;
677679 for ( const line of lines ) {
678680 if ( ! line . startsWith ( 'SUCCESS' ) ) continue ;
679681 const [ _0 , name , _2 , valueStr , _4 , range , _6 , extra ] = line . split ( / \s + / ) ;
0 commit comments