How to pass an other data with value to validation in mongoose schema?
See original GitHub issuePass 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:
- Created 10 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the best way to do this might be to just have a function which constructs your validator. IE.
Pretty sure this should cover your use case here. If this doesn’t work for you, let me know and we can reopen this.
See the docs for how to pass custom error messages to validation functions.