-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
required
validator on single nested subdoc not working after Schema#clone()
#8819
Comments
const mySchema = new Schema({
....
text: String,
banner: { type: new Schema(mediaSkeleton), validate: requiredObj } // <-- see `new Schema()` here
....
}) This is an unfortunate rough patch in Mongoose's API, we will fix this with #7181 |
@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 const authorSchema = new mongoose.Schema({
name: String,
pseudonym: String
});
const bookSchema = new mongoose.Schema({
author: {
type: authorSchema,
required: true,
validate: {
validator: a => (a.name && a.name.length > 0) || (a.pseudonym && a.pseudonym.length > 0)
}
}
});
const Book = mongoose.model('Book', bookSchema);
const book = new Book({});
await book.save(); // return Book instance WITHOUT throwing validation error. But calling await book.validate(); // thow validation error Did I missed something or is there a workaround to achieve what I want? |
@Jule- the below script throws a "Path 'use strict';
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const authorSchema = new mongoose.Schema({
name: String,
pseudonym: String
});
const bookSchema = new mongoose.Schema({
author: {
type: authorSchema,
required: true,
validate: {
validator: a => (a.name && a.name.length > 0) || (a.pseudonym && a.pseudonym.length > 0)
}
}
});
const Book = mongoose.model('Book', bookSchema);
const book = new Book({});
await book.save(); // return Book instance WITHOUT throwing validation error.
console.log('Done');
} Can you please clarify what version of Mongoose you're using, and modify the above script to demonstrate your issue? |
@vkarpov15 sorry for the delay, I am back! Thank you, you helped me spotting what is messing things up! 👍 mongoose.set('cloneSchemas', true); 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! 🙂 |
And I am using Mongoose version |
Just in order to be clear, the full repro script is: 'use strict';
const mongoose = require('mongoose');
mongoose.set('cloneSchemas', true);
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const authorSchema = new mongoose.Schema({
name: String,
pseudonym: String
});
const bookSchema = new mongoose.Schema({
author: {
type: authorSchema,
required: true,
validate: {
validator: a => (a.name && a.name.length > 0) || (a.pseudonym && a.pseudonym.length > 0)
}
}
});
const Book = mongoose.model('Book', bookSchema);
const book = new Book({});
await book.save(); // return Book instance WITHOUT throwing validation error.
console.log('Done');
} |
required
validator on single nested subdoc not working after Schema#clone()
@vkarpov15 Thank you! 🙏🙂 |
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:
I am calling save method on model like following:
requiredObj
function doesn't call at all. Please help me to fix it.The text was updated successfully, but these errors were encountered: