adminjs
adminjs copied to clipboard
Cannot save entity, because schema with nested property [Number] returns validation error
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:
- Create mongoose schema as below
- Go to adminJS and create entity
- Fill required fields
- See error
Expected behavior
Entity is saved with location
as object:
{
type: "Point",
coordinates: [10.20, 30.50]
}
Screenshots
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
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 },
},
},
},
Closing the issue due to lack of response. If it's still the problem please reopen it.