adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Cannot save entity, because schema with nested property [Number] returns validation error

Open almandoro opened this issue 1 year ago • 1 comments

Describe the bug Given mongoose schema with one property of type [Number] during save returns error ...is required|

Installed libraries and their versions

  • "@adminjs/express": "^5.0.0",
  • "@adminjs/mongoose": "^3.0.0",
  • "@adminjs/nestjs": "^5.0.0",
  • "express-formidable": "^1.2.0",
  • "express-session": "^1.17.3",
  • "mongoose": "^6.4.6",

To Reproduce Steps to reproduce the behavior:

  1. Create mongoose schema as below
  2. Go to adminJS and create entity
  3. Fill required fields
  4. See error

Expected behavior Entity is saved with location as object:

{
type: "Point",
coordinates: [10.20, 30.50]
}

Screenshots image

AdminJSOptions with schema AdminJS Options (nestjs)

adminJsOptions: {
  rootPath: '/admin',
  loginPath: '/admin/login',
  resources: [
    {
      resource: user,
    },
    {
      resource: userLocation,
    },
  ],

entity to save UserSchema

export class UserLocation extends BaseSchema {
  @RequiredProp({ type: Schema.Types.ObjectId, ref: User.name })
  user: User;

  @OptionalProp(null, { type: LocationPointSchema, index: '2dsphere' })
  location: LocationPoint;
}

LocationPointSchema

export const LocationPointSchema = new Schema({
  type: {
    type: String,
    enum: ['Point'],
    required: true,
  },
  coordinates: {
    type: [Number],
    required: true,
    validate: [
      (val) => {
        return val.length === 2;
      },
      '{PATH} must contains 2 elements',
    ],
  },
});

Desktop (please complete the following information if relevant):

  • OS: MacOS
  • Browser Chrome
  • Version 12.1

almandoro avatar Jul 30 '22 22:07 almandoro

Can you show what console.log(val) logs in validate? The payload should be converted to correct types before saving.

Alternatively, in case types cannot be determined correctly based on mongoose schema try to specify them explicitly:

    {
      resource: userLocation,
      options: {
        properties: {
          location: { type: 'mixed' },
          'location.point': { type: 'string', availableValues: ['Point'] }, // this line might be unneccessary
          'location.coordinates': { type: 'number', isArray: true },
        },
      },
    },

dziraf avatar Aug 01 '22 05:08 dziraf

Closing the issue due to lack of response. If it's still the problem please reopen it.

krzysztofstudniarek avatar Aug 16 '22 09:08 krzysztofstudniarek