'Trouble implementing validator for MongoDB Schema
platform: [{
validate: {
validator: array => { //our custom validator, array is the provided array to be validated
const filtered = array.filter((obj, index, self) => self.findIndex(el => el.platformName === obj.platformName) === index); //this removes any duplicates based on object key
return array.length === filtered.length //returns true if the lengths are the same; if the lengths aren't the same that means there was a duplicate key and validation fails
},
message: 'Detected duplicate keys in {VALUE}!',
},
platformName: {
type: String,
enum: Platform, //category must be in this enum
},
link: {
type: String,
},
}],
I'm using the above method to get something like
platform: [{"platformName": "FACEBOOK", "link": "DJNgJD"}, {"platformName": "MEETUP", "link": "DJNgJD"}]
and trying to use this validator I got from stack overflow it self to prevent duplicate key values for platformName key, but getting the error below. link to stack overflow answer i got this code from How to validate object keys and values in Mongoose Schema?
throw new Error('`' + firstPieceOfPath + '` may not be used as a schema pathname');
Using Ts btw, someone help me figure out what I'm doing wrong
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
