samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.81k stars 94 forks source link

(Off topic) how hard is to use nestia capabilities outside nestjs controller? #944

Closed hikinine closed 1 month ago

hikinine commented 3 months ago

On my domain layer I usually adapt controller inputs into aggregate/entities/value object/etc

I do have a schema that ensure every entity props to match the desired type. Lets say


const schema = {
  isActive: z.boolean(),
  auth: z.instanceOf(Authentication),
  profile: instanceOf(Profile),
  email: z.string()
}

export interface UserProps {
  isActive: boolean
  auth: Authentication
  profile: Profile
  email: string
  //...
}
export class User extends Entity<User> {
  static schema = schema
}

const user = new User({
  isActive: true,
  profile: new Profile({...}),
  email: 'whatever@gmail.com',
  ...
})

When I do construct an entity object, it auto runs an type validation based on schema checking if props match schema.

Question

How hard is to just pass an interface and validation be done? Without an schema, pure interface.

If an primitive was provided, check if matchs If an class was provided, check by instanceof

Thanks in advance

samchon commented 1 month ago

You can use below typia functions:

typia.is<T>() function auto-casts to the T type when valid. typia.assertGuard<T>() function auto-casts when no type error.

Related documents: