-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for 'ref' in zUUID, and 'refPath' in zId and zUUID #27
Comments
Oi! Sounds good, but keep in mind that UUIDs are poorly supported by mongoose itself.
There are several edge-cases though: Ah, and different MongoDB ORMs are using different UUID versions, so please keep that in mind if you are looking into interoperating with other languages / services |
Hi @bebrasmell, thank you for your quick response! 😊 The two improvements you mentioned sound great and would definitely solve the current limitation. Regarding the UUID observation, I reviewed the StackOverflow references you mentioned, and I believe they might be a bit outdated. As for validations, you’re right that third-party libraries may be necessary in some cases. Fortunately, z.string().uuid().unique().default(uuidv4); Additionally, the example schemas I shared are based on real-world use cases we have in production with mongoose@7, where we use dynamic references (refPath) and arrays of uuid without any problems. Everything works smoothly! Thank you again for your support! |
@tserdeiro Will do it asap to unblock your development, thanks for reaching out! |
@tserdeiro Please check out version |
Hi @bebrasmell, thank you for the update! 😊 I've updated to version However, I'm encountering the following error:
Here’s the export const auditLogSchema = z.object({
_id: z.string().uuid()
.unique()
.default(uuidv4),
account: zUUID(Subject.ACCOUNT),
changes: z.any(),
issuer: zUUID(Subject.ACCOUNT),
ref: zId().refPath('subject')
.or(
zUUID().refPath('subject')
),
refs: z.array(
zId().refPath('subject')
.or(
zUUID(Subject.ACCOUNT).refPath('subject')
)
),
subject: z.enum(Object.values(Subject) as [Subject, ...Subject[]])
})
const schema = zodSchema(auditLogSchema, { // error here
collection: 'audit_logs',
timestamps: true,
versionKey: false
}) And the generated Mongoose schema: {
"_id": {
"default": "ffa04949-9992-4c17-92fb-2fcaf5c6ba11",
"required": true,
"unique": true
},
"account": {
"required": true,
"unique": false,
"ref": "Account"
},
"changes": {
"required": true
},
"issuer": {
"required": true,
"unique": false,
"ref": "Account"
},
"ref": {
"required": true,
"unique": false,
"refPath": "subject"
},
"refs": {
"type": [
{
"required": true,
"unique": false,
"refPath": "subject"
}
],
"required": true
},
"subject": {
"unique": false,
"enum": [
"Account",
"... other subjects ..."
],
"required": true
}
} I also have a quick question: "changes": {
"required": true,
"type": "Schema.Types.Mixed"
} Instead of: "changes": {
"required": true
} If so, it seems that the schema is not being generated correctly. Thank you again for your support and the improvements! 😊 |
While implementing
@zodyac/zod-mongoose
in my project, I encountered limitations in the following use cases:uuid
instead ofObjectId
.uuid
orObjectId
(depending on the model).refPath
for dynamic population based on another document property (Mongoose documentation).Currently,
zUUID
does not support theref
option, and neitherzId
norzUUID
acceptrefPath
. This makes it difficult to work with dynamic references anduuid
identifiers.Would it be possible to add support for these features?
Expected Usage Example
Thank you for your attention and for the excellent work on this library. Looking forward to your response! 🖖
The text was updated successfully, but these errors were encountered: