Array validation getting failed because of the "schema" key inside the Schema #12480
Closed
2 tasks done
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Prerequisites
Mongoose version
6.2.2
Node.js version
16.14.2
MongoDB server version
4.4.6
Description
Hi Team,
I have observed a bug in the schema class/validator provided by mongoose. When a schema definition contains a key named:
schema
and another key containing an array of documents, the validation gets failed even with completely valid documents.Steps to Reproduce
Step 1. Defining a schema that contains a key named
schema
and another key that embeds an array.Step 2. Insert a document in the collection
Step 3. Which throws the following error:
Note that the validation error does not occur when:
The validation error occurs only when both the schema field AND the array field exist in the schema
Step 4. Now if we update the schema by uppercasing the key
Schema
from lowercasedschema
Step 5. It will work completely fine if schema was spelled differently (e.g. with an uppercase S, i.e.:
Schema
)Expected Behavior
I understand that
schema
is a reserved keyword that can be used to fetch the document's schema.But in that case, it should throw a warning(compile-time) while we are specifying the
schema
key in our schema definition. Also, the validation is only throwing the error, when we are specifying an embedded array of documents in our definition. In all the other cases, having theschema
key in our definition doesn't throw any validation error at the time of insert/update.The expected behavior IMO would be a consistent error irrespective of the embedded array.
The text was updated successfully, but these errors were encountered: