Lets say for example I have a class created for my domain entity User and repository interface for it , then I implemented the repository to match the interface.
class User {
constructor(
public id: string,
public name: string,
public email: string
) {}
static create(data: userDto) {
const { id, name, email } = data;
return new User(id, name, email);
}
}
interface IUserRepository {
save(user: User): Promise<void>;
findById(id: string): Promise<User>;
findAll(): Promise<User[]>;
}
interface UserDto {
id: string;
name: string;
email: string;
}```
```ts
import UserEntity from '../domain/entities/user'
class UserRepository implements IUserRepository {
async findById(id: string): Promise<User> {
const result: UserDto = await objectionModel.query().findById(id);
if (!result) {
throw new Error('User not found');
}
// do I put a zod schema here?
const user: User = User.create(result);
return user;
}
}
How do I make sure the result returned by my orm model query matches the data needed for UserEntity.create ? Is this a good place to use runtime validation like a zod schema, or am I missing something else entirely here. For context, I'm learning typescript and proper splitting of code for proper backend architecture and still confused by some aspects.