@autonomize/shared-dto-models

v0.0.10

Published

Central repository for shared Data Transfer Objects (DTOs) used across different services and repositories. This repository contains both JavaScript (TypeScript) and Python implementations of our data models.

Downloads

1,134

Readme

TypeScript DTOs

TypeScript implementation of shared DTOs using class-transformer and class-validator.

Setup Development Environment

# Install dependencies
npm install

Project Structure

src/
├── copilots/        # Copilot-related DTOs
├── agents/          # Agent-related DTOs
├── utils/          # Utility functions for validation and transformation

Usage Example

There are several ways to use the validation utilities:

import { 
  validateAndTransform, 
  validate, 
  transform,
  CopilotDto 
} from '@autonomize-ai/shared-dto-models';

// Method 1: Validate and transform in one step (Recommended)
const result = await validateAndTransform(CopilotDto, {
    id: '123e4567-e89b-12d3-a456-426614174000',
    name: 'Test Copilot',
    metadata: {
        version: '1.0.0'
    }
});

if (result.isValid && result.data) {
    console.log('Valid copilot:', result.data);
} else {
    console.error('Validation errors:', result.errors);
}

// Method 2: Transform first, then validate separately
const copilotDto = transform(CopilotDto, {
    id: '123e4567-e89b-12d3-a456-426614174000',
    name: 'Test Copilot',
    metadata: {
        version: '1.0.0'
    }
});

const validationResult = await validate(copilotDto);
if (validationResult.isValid) {
    console.log('Valid copilot:', validationResult.data);
} else {
    console.error('Validation errors:', validationResult.errors);
}

Development

This project uses changesets for version management and package publishing.

For Developers

When making changes:

  1. Create a new branch for your changes
  2. Make your code changes
  3. Add a changeset to document your changes:
npm run changeset
  1. Commit and push your changes
  2. Create a Pull Request

For detailed information about the development workflow, versioning guidelines, and publishing process, please refer to .changeset/README.md.

Adding New DTOs

  1. Create new DTO in appropriate directory
  2. Export in src/index.ts
  3. Build and test

Building

npm run build

Common Decorators Used

class-transformer

  • @Type(() => SubDto): Specify nested DTO types

class-validator

  • @IsUUID(): UUID validation
  • @IsString(): String validation
  • @IsObject(): Object validation
  • @IsOptional(): Optional field
  • @IsBoolean(): Boolean validation

Validation Utilities

The package provides three main validation utilities:

  1. validateAndTransform: Combines transformation and validation in one step
  2. validate: Validates an existing DTO instance
  3. transform: Transforms a plain object to a DTO instance without validation

Types Exported

  • ValidationResult<T>: Type containing validation result
  • ClassConstructor: Type for class constructors
  • ValidationError: Type for validation errors

Important Notes

  1. Remember to use @Expose() for properties
  2. Validation is asynchronous
  3. Use validateAndTransform for the most straightforward validation flow
  4. All validation utilities work with any object-based DTO

Scripts

  • npm run build: Build TypeScript to JavaScript
  • npm run test: Run tests (when implemented)
  • npm run prepare: Automatically run build before npm install
  • npm run changeset: Create a changeset for your changes
  • npm run version: Update versions and changelogs