0.0.11 • Published 8 months ago
@autonomize/shared-dto-models v0.0.11
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:
- Create a new branch for your changes
- Make your code changes
- Add a changeset to document your changes:
npm run changeset
- Commit and push your changes
- 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
- Create new DTO in appropriate directory
- Export in src/index.ts
- 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:
validateAndTransform
: Combines transformation and validation in one stepvalidate
: Validates an existing DTO instancetransform
: Transforms a plain object to a DTO instance without validation
Types Exported
ValidationResult<T>
: Type containing validation resultClassConstructor
: Type for class constructorsValidationError
: Type for validation errors
Important Notes
- Remember to use @Expose() for properties
- Validation is asynchronous
- Use
validateAndTransform
for the most straightforward validation flow - All validation utilities work with any object-based DTO
Scripts
npm run build
: Build TypeScript to JavaScriptnpm run test
: Run tests (when implemented)npm run prepare
: Automatically run build before npm installnpm run changeset
: Create a changeset for your changesnpm run version
: Update versions and changelogs