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.

Validate is called on Model.get

See original GitHub issue

Summary:

Model.get() is calling validate on attributes but I don’t think it’s necessary (and it’s breaking our way to validate complex models). I’ve written a test showing this behavior.

Code sample:

const model = dynamoose.model('no-validate', new dynamoose.Schema(
      {
        id: {
          type: String,
          required: true,
          hashKey: true,
          validate: function (val, model) {
            if (!model.name){
              // Is called on get :(
              throw new Error('should not throw');
            }
            return true;
          }
        },
        projectId: {
          type: String,
          required: true,
          rangeKey: true,
          index: {
            global: true,
            name: 'projectId-index',
            project: true,
          },
        },
      }));


    let error, res;
    try {
      await model.create({id: 'idid', projectId:'project', name: 'Rob'});
      res = await model.get({id: 'idid', projectId: 'project'});
    } catch (e) {
      error = e;
    }
    should.not.exist(error);
    res.id.should.eql(1);
    res.name.should.eql('Rob');

Current output and behavior:

Validate is called when calling Model.get()

Expected output and behavior:

Validate shouldn’t be called when calling Model.get()

Environment:

Operating System: MacOS Mojave Operating System Version: latest Node.js version (node -v): 8.15.0 NPM version: (npm -v): 6.4.1 Dynamoose version: latest Dynamoose Plugins: none

Dynamoose Plugins:

  • Yes, I believe that one or multiple 3rd party Dynamoose plugins are effecting this issue
  • No, I believe this issue is independent of any 3rd party Dynamoose plugins I’m using
  • Unknown, I’m unsure if Dynamoose plugins are effecting this issue
  • I am not using any Dynamoose plugins

Other information (if applicable):

Type (select 1):

  • Bug report
  • Feature suggestion
  • Question
  • Other suggestion
  • Something not listed here

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
  • I have tested the code provided and am confident it doesn’t work as intended
  • I have ensured that all of my plugins that I’m using with Dynamoose are listed above
  • I have filled out all fields above
  • I am running the latest version of Dynamoose

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
hweekscommented, Feb 26, 2019

Let’s dig into the findOne method in mongoose, as you can see it inspired a lot of what is happening here. In that method no validation occurs at all, it just queries based on the conditions it builds. check this.

If we then take a look at the create method, we see that is the only place validation occurs. the method and the validation

This shows that mongoose only validates on create, never on fetch. That said, I know we’ve had a bit of push back on going for full parity with mongoose, and that is not even claimed as a goal in the project.

Personally, I’m all for it as it will help with performance to reduce the callback overhead on a per call basis a lot.

@fishcharlie

0reactions
dolsemcommented, Feb 11, 2020

I can see arguments for both. Personally, I would prefer it to be on by default, but more importantly, it would be nice to stick it into various schema examples throughout the docs, to make sure users have seen the configuration option and are aware of it. Generally speaking, I like defaults to have minimal overhead, but extra safeguards are worth it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate is called on Model.get · Issue #583 · dynamoose ...
Summary: Model.get() is calling validate on attributes but I don't think it's necessary (and it's breaking our way to validate complex models).
Read more >
Get method name that called validator in Rails 5
In my Passenger model I have my validation rules. Those rules depend on from which method the validator has been called.
Read more >
Active Record Validations and Callbacks
Model-level validations are the best way to ensure that only valid data is saved into your database. They are database agnostic, cannot be...
Read more >
Model validation in ASP.NET Core MVC and Razor Pages
Validation attributes let you specify validation rules for model properties. The following example from the sample app shows a model class that ...
Read more >
A Deep Dive into Active Record Validations
The "M", also called the model layer, is responsible for managing how ... Honeybadger is head and shoulders above the rest and somehow...
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