Skip to content

carbonteq/refined-type-effect

Repository files navigation

Carbonteq - Refined Types Utils (with Effect)

TypeScript Linted with Biome npm version license

🛡️ Type-safe, runtime-validated, and nominally-typed primitives for TypeScript, leveraging the power of Zod and Effect.

Overview

This library builds on the foundation established by @carbonteq/refined-type, enhancing it with Effect's robust error handling capabilities.

Usage Examples

Basic Type Validation

import { createRefinedType } from '@carbonteq/refined-type-effect';
import * as z from "zod/v4";
import { Effect } from 'effect';

// Define a refined type for email addresses
const Email = createRefinedType('Email', z.string().email());
type Email = typeof Email.$infer;

// Validate an email address
const validationProgram = Email.create('[email protected]').pipe(
Effect.match(validationProgram, {
  onSuccess: (email) => email,
  onFailure: (error) => (error.message),
})
);

// Run the validation
Effect.runPromise(validationProgram).then(console.log);

// Type-safe function that only accepts validated emails
function sendNotification(email: Email) {
  return Effect.succeed(`Notification sent to ${email}`);
}

// This won't compile - type safety at work
// sendNotification("[email protected]"); // Error!


// Proper validation flow
const notificationProgram = Email.create('[email protected]').pipe(
  Effect.flatMap(sendNotification)
);

Custom Error Types

import { createRefinedType, RefinedValidationError } from '@carbonteq/refined-type-effect';
import * as z from "zod/v4";
import { Effect } from "effect";


// Define custom error types
class InvalidAgeError extends RefinedValidationError {
  constructor(err: z.core.$ZodError) {
    super(err);
    this.name = "InvalidAgeError";
    this.message = "Age must be between 18 and 120 years";
  }
}

// Create a refined type with a custom error handler
const Age = createRefinedType(
  "Age",
  z.number().int().min(18).max(120),
  (data, err) => new InvalidAgeError(err),
);
type Age = typeof Age.$infer;

// Using the refined type with custom error handling
function validateUserAge(input: unknown) {
  return Age.create(input).pipe(
    Effect.match({
      onSuccess: (age) => `Age validation passed: ${age}`,
      onFailure: (error) => {
        return error.message;
      },
    }),
  );
}


// Usage examples
Effect.runPromise(validateUserAge(25)).then(console.log);
// Output: "Age validation passed: 25"

Effect.runPromise(validateUserAge(10)).then(console.log);
// Output: "Age must be between 18 and 120 years"

Installation

  • effect and zod are dependencies thus need to be installed
# NPM
npm install @carbonteq/refined-type-effect effect zod

# Yarn
yarn add @carbonteq/refined-type-effect effect zod

# PNPM
pnpm add @carbonteq/refined-type-effect effect zod

Requirements

  • Node.js >= 18
  • TypeScript >= 4.9
  • Zod >= 3.25.0
  • Effect >= 2.0.0

License

MIT

About

Refined types with Zod v4 and Effect

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •