Closed
Description
Hi, I want to request a feature if it's not available that is a developer should be able to write custom validators for custom nested object that is not a schema.
I am creating a schema like following:
const mediaSkeleton = {
url: { type: String, required: true },
thumbUrl: { type: String, required: true },
mediaType: { type: Number, min: 0, max: 1, required: true },
aspectRatio: { type: Number, required: true }
}
function requiredObj(x) {
console.log(`requiredObj called for ${x}!`)
return x != undefined && x != null
}
const mySchema = new Schema({
....
text: String,
banner: { type: mediaSkeleton, validate: requiredObj }
....
})
I am calling save method on model like following:
const model = mongoose.model("MySchema", mySchema)
await new FaceOffModel(
{ text: "Testing requiredObj validator" }
).save()
requiredObj
function doesn't call at all. Please help me to fix it.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
vkarpov15 commentedon Apr 21, 2020
This is an unfortunate rough patch in Mongoose's API, we will fix this with #7181
Jule- commentedon May 29, 2020
@vkarpov15 are you sure it works?
I am trying to achieve something like that and I have issues triggering the validation on save on my nested schema pathes.
For me this code is not triggering
required
validator on pathauthor
nor my custom validator functionBut calling
validate
on it will throw validation errorDid I missed something or is there a workaround to achieve what I want?
vkarpov15 commentedon Jun 4, 2020
@Jule- the below script throws a "Path
author
is required" error, as expected.Can you please clarify what version of Mongoose you're using, and modify the above script to demonstrate your issue?
Jule- commentedon Jun 10, 2020
@vkarpov15 sorry for the delay, I am back!
Thank you, you helped me spotting what is messing things up! 👍
I have set this in my connection code, before creating my Model, of course.
From my point of vue it is a bug, can you confirm that? Or help me understand what can I do to achieve what I want?
Thank you again! 🙂
Jule- commentedon Jun 10, 2020
And I am using Mongoose version
5.8.9
Jule- commentedon Jun 11, 2020
Just in order to be clear, the full repro script is:
9 remaining items