Comments (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.
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)
- Error with Exported Variable and Type Inference HOT 1
- A few ideas HOT 3
- Is it possible to have transformed values in the Controllers after the DTOs pass validation? HOT 1
- Enum type issue HOT 1
- Fix: dateString regex issue
- Union type issue HOT 5
- Asking for Binary zod type
- NestJS Interceptor receives a Promise instead of resolved data HOT 1
- Zod + nestjs + prisma (all optional fields) HOT 2
- Support setErrorMap HOT 1
- Add support to `.brand` schemas HOT 1
- Ghost Dependency: RxJS
- ZodSerializerDto isn't used for OpenAPI HOT 1
- z.string().datetime() is not generated correctly the format is missed in the openapi spec HOT 2
- Date is not shown in Swagger doc.
- ZodDate does not generate type and format for OpenApi HOT 1
- How do I show the validation errors when using a global pipe? HOT 1
- ZodSerializerInterceptor does not work, because there is no name attribute
- How can `z.dateString()` be used for date-revival ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-zod.