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.

How to pass an other data with value to validation in mongoose schema?

See original GitHub issue

Pass an other data with value to validation function.

The following will be code for maxlength validation

maxLength = function  (v) {
    if (v && v.length) {
    return v.length <= 100;
    }
},

The schema field

desc: { type: String, validate: [maxLength, 'Exceed max length of 100 chars'] } 

Can we pass a data with validate?

i.e Like below code

desc: { type: String, validate: [maxLength, 100, 'Exceed max length of 100 chars'] } 


maxLength = function  (v, length) {
    if (v && v.length) {
    return v.length <= length;  // length will be 100
    }
},

Here validate second array element(length of desc) is passed to validation function. Later validate with this passed length.

How it can be accomplished?

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
ebensingcommented, Aug 2, 2013

I think the best way to do this might be to just have a function which constructs your validator. IE.

function getMaxValidator(val) {
  return function  (v) {
    if (v && v.length) {
      return v.length <= val;
    }
  }
}
// To get a validator for length 100, you'd do something like this
...
validate : getMaxValidator(100),
...

Pretty sure this should cover your use case here. If this doesn’t work for you, let me know and we can reopen this.

0reactions
ebensingcommented, Aug 5, 2013

See the docs for how to pass custom error messages to validation functions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose v6.8.2: Validation
If the built-in validators aren't enough, you can define custom validators to suit your needs. Custom validation is declared by passing a validation...
Read more >
How to do field validations in a mongoose schema based on ...
Custom validation is declared by passing a validation function. You can find detailed instructions on how to do this in the SchemaType#validate ......
Read more >
Data Validation With Mongoose in Node.js
In this story, we'll learn how to validate our data before storing it into MongoDB with mongoose library. ; Create a new file...
Read more >
How To Use Schema Validation in MongoDB - DigitalOcean
Step 2 — Validating String Fields. In MongoDB, schema validation works on individual collections by assigning a JSON Schema document to the ...
Read more >
Mongoose Schema Types, Validation & Queries Tutorial
Use built-in Mongoose Schema validators. In the previous step, We saved John's data in the MongoDB database by using the Mongoose save() method....
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