Required fields and default values (#5835)
* Add field options to mongo schema metadata * Add/fix test with fields options * Add required validation failing test * Add more tests * Only set default value if field is undefined * Fix redis test * Fix tests * Test for creating a new class with field options * Validate default value type * fix lint (weird) * Fix lint another way * Add tests for beforeSave trigger and solve small issue regarding the use of unset in the beforeSave trigger
This commit is contained in:
committed by
GitHub
parent
d3810c2eba
commit
fd637ff4f8
@@ -84,9 +84,19 @@ const mongoSchemaFromFieldsAndClassNameAndCLP = (
|
||||
};
|
||||
|
||||
for (const fieldName in fields) {
|
||||
const { type, targetClass, ...fieldOptions } = fields[fieldName];
|
||||
mongoObject[
|
||||
fieldName
|
||||
] = MongoSchemaCollection.parseFieldTypeToMongoFieldType(fields[fieldName]);
|
||||
] = MongoSchemaCollection.parseFieldTypeToMongoFieldType({
|
||||
type,
|
||||
targetClass,
|
||||
});
|
||||
if (fieldOptions && Object.keys(fieldOptions).length > 0) {
|
||||
mongoObject._metadata = mongoObject._metadata || {};
|
||||
mongoObject._metadata.fields_options =
|
||||
mongoObject._metadata.fields_options || {};
|
||||
mongoObject._metadata.fields_options[fieldName] = fieldOptions;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof classLevelPermissions !== 'undefined') {
|
||||
@@ -425,6 +435,7 @@ export class MongoStorageAdapter implements StorageAdapter {
|
||||
const schemaUpdate = { $unset: {} };
|
||||
fieldNames.forEach(name => {
|
||||
schemaUpdate['$unset'][name] = null;
|
||||
schemaUpdate['$unset'][`_metadata.fields_options.${name}`] = null;
|
||||
});
|
||||
|
||||
return this._adaptiveCollection(className)
|
||||
|
||||
Reference in New Issue
Block a user