Giter VIP home page Giter VIP logo

Comments (4)

DScheglov avatar DScheglov commented on September 21, 2024

@maritter , hi,
Thanks for the issue.
I considered such behavior on early stages and decided to remove "required" from the model, when using "selection".

The motivation is:

  • when we specify fields to be selected we want to build schema of object that we return to api consumers. So, we should not check if it contain all required field. That's why "required" is absent on the selection.

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).

from mongoose-schema-jsonschema.

maritter avatar maritter commented on September 21, 2024

Hi,
thx for your fast reply.

I'll try to explain our use case:
We want to use your library to generate our api documentation using swagger/openapi. We have some required properties on the model that are generated on the server, such as 'createdBy'. Those should not be present in the schema for the POST/PATCH requests.

While writing this down another point came to my mind:
In addition to above a property that is required in the POST request might not be needed in the PATCH request

Example:
model schema

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.

from mongoose-schema-jsonschema.

DScheglov avatar DScheglov commented on September 21, 2024

@maritter Hi, yes, I have an idea. But you don't like it ...

You have different (at least) three different messages in you API:

  • CreateBookInput
  • Book
  • PatchBookInput

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: inputJsonSchema that will work with required fields in more strict ways

from mongoose-schema-jsonschema.

maritter avatar maritter commented on September 21, 2024

@DScheglov
Hi, thanks for your feedback.
Guess you are right, maybe we have to give our implementation a second thought.

Anyhow, I think a different implementation for the required handling would be good, maybe as suggested by you within a new method.

from mongoose-schema-jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.