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.

Custom properties on schema property configuration object

See original GitHub issue

Is there a way to add custom properties to schema property’s configuration object?

I would like to have the ability to denote my schema properties as “cloud only” denoting properties that I don’t want the client to be able to change (but I still want to be able to update them). I was hoping to be able to use Document.schema to pull up all the properties that are cloud only during validation to ensure they aren’t changed by the client. Right now I am having to hardcode a list of cloud only properties in an array, but this poses a problem in the event I forget to update that array and inadvertently allow the property to get updated by the client. Of course testing should resolve this but this feature would make for readable/maintainble code.

I tried to add cloudOnly: true just to try it but run into TypeError: Undefined type undefinedattokens.bru.cloudOnly``. This was expected. Do you have any solutions for this?

Here’s an example schema:

const userSchema = new mongoose.Schema({
  userId: {
    type: mongoose.ObjectId,
    required: true,
    cloudOnly: true,
  }
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Dec 13, 2018

@govindrai I can’t repro your issue, the below script works fine for me. @mhombach 's explanation is unfortunately incorrect.

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh7303`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);
  await mongoose.connection.dropDatabase();

  const userSchema = new mongoose.Schema({
    userId: {
      type: mongoose.ObjectId,
      required: true,
      cloudOnly: true,
    }
  });
}

In general, there’s no reason why you can’t add custom properties on your schema types. There are plenty of plugins that rely on custom schematype options, like mongoose-autopopulate. Try using User.schema.eachPath() to iterate through every path in the user model’s schema. Here’s an example

0reactions
vkarpov15commented, Dec 22, 2018

Good idea, we will improve this error message

Read more comments on GitHub >

github_iconTop Results From Across the Web

object — Understanding JSON Schema 2020-12 documentation
The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key...
Read more >
Using Custom Properties - Oracle Help Center
On your data catalog instance Home tab, click Custom Properties to access the Custom Properties tab. · Click Create Custom Property. · (Optional) ......
Read more >
User-defined custom properties - Hackolade
User-defined custom properties. For each object in Hackolade, we've defined a set of standard properties that appear in the properties pane.
Read more >
Custom Configuration Properties Provider
You can use the Mule SDK and Mule API to create a custom configuration properties provider that enables an app to discover configuration...
Read more >
Add a custom property - Foundry Rules - Palantir
Object properties must be backed by a column in the input dataset. In the case of an empty, auto-generated input dataset, edit the...
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