Effect CommunityEC
Effect Community17mo ago
9 replies
Guido

TypeScript Validator Function: Type Definition and Implementation

i need some help with a type definition for a validator function

import { Schema as S } from '@effect/schema';
import * as Sentry from '@sentry/serverless';
import { Effect, pipe } from 'effect';

import { effectToPromise } from './effectToPromise';

export const validateSchemaAsPromise =
  <T>(schema: S.Schema<T>) =>
  (data: unknown): Promise<T> =>
    pipe(
      Effect.succeed(data),
      Effect.tryMap({
        try: (_) => S.decodeUnknownSync(schema)(_, { errors: 'all' }),
        catch: (e) => {
          console.error(e);
          Sentry.captureException(e, {
            tags: {
              schema: schema.name,
            },
          });
          return new Error('Invalid schema');
        },
      }),
      effectToPromise,
    );


I want to be able to tag the sentry error with the name of the class schema used for validation like this:
export class Foo extends S.Class<Foo>('Foo')({
  bar: S.String
}) {}


S.decodeUnknownSync is supposed to accept classes (it works) but when trying to access schema.name I get TS2339: Property name does not exist on type Schema<T, T, never>

what would be the proper type definition for
schema
so I can safely access
name
?
Was this page helpful?