Unable to use Type or Collection in my Schema. Workaround?

Hi,

I am new here, but I am having a really hard time finding a workaround for using the keywords '“type” and “collection”. I am working on an NFT project that absolutely needs these keywords in the metadata standard. However I am getting validation errors using them. Is there a work around?

Hi @Bryson_N_A and welcome in the MongoDB Community :muscle: !

I don’t understand what you are trying to do. Are you trying to have a constraint on 2 fields named “collection” and “type” in your MongoDB documents?

What’s the “metadata standard”?

See the available keywords here:

And the omissions here:

I hope this helps.

Cheers,
Maxime.

Just in case, I would try to put all my literals (field names) in double quotes, in case they are reserved keywords.

The exact validation error message you get would be interesting to see.

1 Like

Hi!

Thanks for the response. Right now I have to conform to an NFT metadata standard for Metaplex. However there are reserved keywords that are causing validation errors. “type” and “collection” are throwing me some errors. I need to see if there is a workaround. I will show you the NFT standard I need to comply with.

So that’s the MongoDB document that you would store in MongoDB correct? And you need to ensure a constraint on the collection field (which is a sub doc == object) and the type field which is apparently a field in the array of subdocuments properties.files. Correct?

What kind of constaints are you chasing?

Could you please publish your document in JSON text rather than an image?

Please also share your schema definition.

This way we would be able to play with it.

2 Likes
const ticketSchema = new mongoose.Schema({
  name: String,
  symbol: String,
  description: String,
  seller_fee_basis_points: Number,
  image: String,
  attributes: [
    { trait_type: String, value: String, _id: false },
    { trait_type: String, value: String, _id: false },
    { trait_type: String, value: String, _id: false },
    { trait_type: String, value: Boolean, _id: false },
    { trait_type: String, value: String, _id: false },
    {
      trait_type: String,
      value: String,
      _id: false,
    },
    { trait_type: String, value: String, _id: false },
    { trait_type: String, value: Number, _id: false },
    { trait_type: String, value: Boolean, _id: false },
  ],
  properties: {
    creators: [
      { address: String, share: Number, _id: false },
      { address: String, share: Number, _id: false },
    ],

    files: [{ uri: String, type: String, _id: false }],
  },
  collection: { name: String, family: String, _id: false },
});

Yes. I just don’t know how to fix the error. If I change they words it works, but for some reason when I add collection and type it throws this error.

So this isn’t a schema validation related question actually. It’s a Mongoose related question which I have almost zero knowledge of.

I just don’t recommend using it and I think we have an example of why. No idea what’s wrong.

Hi @Bryson_N_A

In addition to what @MaBeuLux88 has mentioned, this is a known issue in Mongoose due to its reserved keywords: Using reserved keyword as schema key · Issue #1760 · Automattic/mongoose · GitHub and unfortunately I don’t know if it can be worked around, at least from Mongoose.

However, I’d like to circle back to one of your earlier post:

Right now I have to conform to an NFT metadata standard for Metaplex. However there are reserved keywords that are causing validation errors.

I’d like to understand more of the goal you’re trying to achieve here. Is it to:

  1. ensure that your app always conforms to the standard, or
  2. ensure that the data stored in the database always conform to the standard, or
  3. both 1 & 2 at the same time

To solve #1, you can use json schema which is a standard to describe the requirements of a JSON document. From the application side, there are node modules that can check the validity of a JSON document vs. a certain schema. The jsonschema module is but one example.

To solve #2, MongoDB supports JSON schema validation via the $jsonSchema validation directive. You can create a collection and specify that only documents conforming to the schema can be inserted into the collection. Please see the linked page for examples on how to do this from the MongoDB side.

To solve #3, well then you’ll have to do #1 and #2 together :slight_smile:

I looked around for a more concrete example and found their JSON schema definition from the Metaplex Github. It doesn’t appear to fully describe the example document you posted, but it may serve as a starting point for you to create a more specific schema.

Best regards
Kevin

2 Likes