Giter VIP home page Giter VIP logo

Comments (2)

xrebelox avatar xrebelox commented on May 22, 2024 2

Can't really say much regarding the first issue, but in regards to the second issue where all properties are presented as being optional, this can be solved by setting strictNullChecks to true in your tsconfig.json file.

Update: Actually zod's documentation requires you to set strict: true in your tsconfig.json, you can read it here.

from nestjs-zod.

nolawnchairs avatar nolawnchairs commented on May 22, 2024 2

Thanks, @xrebelox - I changed the strict level and the second issue is gone. I never really ever had the need to enforce strict mode before, although it is recommended. I'm just stubborn.

As for the first issue, I've found a fix:

In the src/pipe.ts module, within the transform method, there's this line:

return validate(value, metatype.schema, createValidationException)

As expected, if the validation passed, the object is returned. However, I kind of hacked the JS code directly, and I changed it to:

const result = validate(value, metatype.schema, createValidationException)
return Object.assign(new metatype(), result)

This creates a class instance of the same shape as the object. Since the DTO classes are created on-the-fly, this is safe to do. However, with a public library the choice on whether or not to transform the object to a class instance should be left to the end user, as it is in Nest's native SerializerInterceptor implementation (which uses class-transformer under the hood)

The metatype value will always be the class constructor reference to whatever the end-user adds as the DTO type.

This begs the question on WHERE to set this user configurable value. The best option would be to add an options object directly to the interceptor's constructor like so:

{
  provide: APP_INTERCEPTOR,
  useValue: new ZodSerializerInterceptor({ transform: true }),
}

Of course, the interceptor's constructor has an @Inject for the Reflector class so there are ways around this, but it should be noted that the Reflector class doesn't always need to be injected, and can be instantiated normally, as it's just a utility class with no DI of its own.

const reflector = new Reflector()

If you still prefer to inject it, there are ways to do that as well:

{
  provide: APP_INTERCEPTOR,
  inject: [Reflector],
  useFactory: (reflector: Reflector) => 
     new ZodSerializerInterceptor(reflector, { transform: true }),
}

I should note that for many use cases, the object is just fine and classes are not needed. However, this would be more of a semantic choice. If the type is defined as a certain class type, then the resulting value should conform to the class API specification including all the methods and accessors. This is why it should be the choice of the implementing party.

I also notice there is no top-level module to initialize. This is also a good way to introduce user-specific configuration for all the moving parts to have access to.

There's no contributing guide, so I'm curious as to the author's preferred protocols regarding PR's and such. I have loads of experience building NestJS libraries (all in-house libraries, but experience none the less), so I can make meaningful contributions to this project if it's welcome.

Cheers.

from nestjs-zod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.