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
@@ -323,16 +323,66 @@ RestWrite.prototype.runBeforeLoginTrigger = async function(userData) {
|
||||
|
||||
RestWrite.prototype.setRequiredFieldsIfNeeded = function() {
|
||||
if (this.data) {
|
||||
// Add default fields
|
||||
this.data.updatedAt = this.updatedAt;
|
||||
if (!this.query) {
|
||||
this.data.createdAt = this.updatedAt;
|
||||
return this.validSchemaController.getAllClasses().then(allClasses => {
|
||||
const schema = allClasses.find(
|
||||
oneClass => oneClass.className === this.className
|
||||
);
|
||||
const setRequiredFieldIfNeeded = (fieldName, setDefault) => {
|
||||
if (
|
||||
this.data[fieldName] === undefined ||
|
||||
this.data[fieldName] === null ||
|
||||
this.data[fieldName] === '' ||
|
||||
(typeof this.data[fieldName] === 'object' &&
|
||||
this.data[fieldName].__op === 'Delete')
|
||||
) {
|
||||
if (
|
||||
setDefault &&
|
||||
schema.fields[fieldName] &&
|
||||
schema.fields[fieldName].defaultValue &&
|
||||
(this.data[fieldName] === undefined ||
|
||||
(typeof this.data[fieldName] === 'object' &&
|
||||
this.data[fieldName].__op === 'Delete'))
|
||||
) {
|
||||
this.data[fieldName] = schema.fields[fieldName].defaultValue;
|
||||
this.storage.fieldsChangedByTrigger =
|
||||
this.storage.fieldsChangedByTrigger || [];
|
||||
if (this.storage.fieldsChangedByTrigger.indexOf(fieldName) < 0) {
|
||||
this.storage.fieldsChangedByTrigger.push(fieldName);
|
||||
}
|
||||
} else if (
|
||||
schema.fields[fieldName] &&
|
||||
schema.fields[fieldName].required === true
|
||||
) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.VALIDATION_ERROR,
|
||||
`${fieldName} is required`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Only assign new objectId if we are creating new object
|
||||
if (!this.data.objectId) {
|
||||
this.data.objectId = cryptoUtils.newObjectId(this.config.objectIdSize);
|
||||
// Add default fields
|
||||
this.data.updatedAt = this.updatedAt;
|
||||
if (!this.query) {
|
||||
this.data.createdAt = this.updatedAt;
|
||||
|
||||
// Only assign new objectId if we are creating new object
|
||||
if (!this.data.objectId) {
|
||||
this.data.objectId = cryptoUtils.newObjectId(
|
||||
this.config.objectIdSize
|
||||
);
|
||||
}
|
||||
if (schema) {
|
||||
Object.keys(schema.fields).forEach(fieldName => {
|
||||
setRequiredFieldIfNeeded(fieldName, true);
|
||||
});
|
||||
}
|
||||
} else if (schema) {
|
||||
Object.keys(this.data).forEach(fieldName => {
|
||||
setRequiredFieldIfNeeded(fieldName, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user