-
Notifications
You must be signed in to change notification settings - Fork 31
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
Behaviour of __removeRequired when using selection #24
Comments
@maritter , hi, The motivation is:
So, if you need the "required", could you describe your use case. It can help to create more suitable solution (actually to implement proposed behavior could be such solution). |
Hi, I'll try to explain our use case: While writing this down another point came to my mind: Example: const BookSchema = new Schema({
title: { type: String, required: true },
year: { type: Number },
createdBy: { type: ObjectId, required: true }
});
const Book = mongoose.model('Book', BookSchema);
Book.jsonSchema(['title', 'year']); schema for POST request {
title: 'Book',
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' }
},
required: [ 'title' ]
} schema for PATCH request {
title: 'Book',
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' }
}
} any ideas on this? Maybe we have to alter the json schema after the creation. |
@maritter Hi, yes, I have an idea. But you don't like it ... You have different (at least) three different messages in you API:
So, the correct solution is to define three schemas for these messages and then get their json schemas. You can use some mongoose practices to re-use schemas to avoid code repeating. However, ) probable I will consider something like: |
@DScheglov Anyhow, I think a different implementation for the required handling would be good, maybe as suggested by you within a new method. |
When using the jsonSchema function on the mongoose model together with a selection, the required property is completely removed. Wouldn't it be better to check, which properties are going to be included and only delete those from the required field, that are excluded?
Example:
Response would look like:
instead of
The text was updated successfully, but these errors were encountered: