I've created an API where one of the fields is an arbitrary JSON object. I'm using Sequelize and Postgres. This field needs to be flexible, as it allows users to store their own metadata, but I do want to limit the size to prevent users from uploading gigantic objects. This is my current solution on the object definition:
metadata: {
type: DataTypes.JSONB,
allowNull: true,
validate: {
len: {
args: [18, 1000],
msg: 'Metadata exceeds the max length of 1000 characters.',
},
},
},
This basically works, but I have no idea what len is actually measuring since it's not a string. It doesn't appear to measure the number of characters in the JSON object. I'm sure there's a better way to do this, but all my googling just turns up information about the max size of a JSONB object in Postgres. I want to set a limit waaay below the max size. I would like to be able to limit it to ~1,000 characters or 1KB for now.