Effect CommunityEC
Effect Community3y ago
5 replies
Dimitris

Understanding Schema String and Parsing in TypeScript

1. Is there an email option for the Schema string? Any implementations that anyone can share?
2. I'm a bit confused (coming from Zod) what's the difference between parsing & encoding. For example, in the following code is parse the right one?

import * as Schema from '@effect/schema/Schema';
import * as Effect from 'effect/Effect';
import {v4 as uuidv4} from 'uuid';

import {UUIDGenerationError} from '../errors.server';

const UuidBrand = Symbol.for('UuidBrand');
export const Uuid = Schema.string.pipe(Schema.brand(UuidBrand)); // todo: more validations here
export type Uuid = Schema.Schema.To<typeof Uuid>;

export function parse(value: unknown) {
  return Schema.parseEither(Uuid)(value);
}

export function generate() {
  return Effect.gen(function* (_) {
    const id = yield* _(Effect.sync(() => uuidv4()));
    return yield* _(parse(id));
    // no reason for this to fail the parsing
  }).pipe(Effect.catchAll(() => Effect.fail(new UUIDGenerationError())));
}
Was this page helpful?