question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: discriminator

See original GitHub issue

Sorting

  • I’m submitting a …

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn’t already been submit

Description

I’m using mongoose discriminators for my models, so I have 1 principal model and 3 children. In my controllers, I have a PATCH method to update my models, in this PATCH method all properties are optionals and I can use this method to update any of my children models.

BUT, tsoa need to validate the right schema depending on which model I’m updating. It will be nice if we can have a solution to declare a pre-validation function to tell tsoa which schema to validate against.

Currently, I’m updating the handlebars template to call my custom method (see below) in the patch route instead of validating schema:

  public async validate (validate: (args: any, request: any, response: any) => any[], args: Args, req: express.Request, res: express.Response): Promise<any[]> {
    const dt = await req.models.Datasource.findOne({ _id: new ObjectId(req.params.id) }, 'mode').exec()
    if (!dt) {
      throw new httpErrors.DatasourceNotFound({ id: req.params.id })
    }
    switch (dt.mode) {
      case DatasourceMode.WORKER:
        args.body.ref = 'PatchWorkerDt'
        break
      case DatasourceMode.INGESTER:
        args.body.ref = 'PatchIngesterDt'
        break
      case DatasourceMode.PROXY:
        args.body.ref = 'PatchProxyDt'
        break
    }
    return validate(args, req, res)
  }

Possible Solution

Maybe a decorator to define a custom pre-validation function where we will need to return the type name we need to validate against?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sureyeaahcommented, Mar 9, 2022

I really need this to be fixed as well. I like the proposal by @devnev, is there any consensus on this?

1reaction
devnevcommented, Jan 24, 2022

Looking at the OAS again, I notice the line regarding discriminators:

When using the discriminator, inline schemas will not be considered.

However, the spec is also explicit that the discriminator serves as a hint for serialisation and validation but isn’t necessary for the proper use of oneOf when only one subschema is valid.

Based on that, TSOA could do two separate steps:

  1. Check if there’s a field that behaves like a discriminator, in which case use a oneOf instead of an anyOf
  2. If additionally that field has a string type and all the subschemas are references, also output a discriminator with a mapping
Read more comments on GitHub >

github_iconTop Results From Across the Web

GAN Feature Matching Explained | Papers With Code
The discriminator, and hence f ( x ) , are trained as with vanilla GANs. As with regular GAN training, the objective has...
Read more >
[1912.00789] Is Discriminator a Good Feature Extractor? - arXiv
The discriminator from generative adversarial nets (GAN) has been used by researchers as a feature extractor in transfer learning and appeared ...
Read more >
The Discriminator | Machine Learning - Google Developers
The discriminator in a GAN is simply a classifier. It tries to distinguish real data from the data created by the generator.
Read more >
Discriminator Feature-Based Inference by Recycling the ...
This paper proposes a novel acyclic discriminator feature based inference (DFI) algorithm that exceeds both accuracy and efficiency of inference ...
Read more >
Ways to improve GAN performance | by Jonathan Hui
Feature Matching. The generator tries to find the best image to fool the discriminator. The “best” image keeps changing when both networks counteract...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found