github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/models/utils.ts (about)

     1  import type { ZodError, ZodType } from 'zod';
     2  import { Result } from '@webapp/util/fp';
     3  
     4  /**
     5   * modelToResult converts a (most likely) zod model into a Result
     6   */
     7  // eslint-disable-next-line import/prefer-default-export
     8  export function modelToResult<T>(
     9    s: ZodType<T>,
    10    data: unknown
    11  ): Result<T, ZodError> {
    12    const result = s.safeParse(data);
    13  
    14    // TODO check why this is failing
    15    if (!result.success) {
    16      return Result.err((result as ShamefulAny).error);
    17    }
    18  
    19    return Result.ok(result.data);
    20  }