feat: Avoid setting a relation as required or with a defaultValue (#5922)

* feat: Avoid setting a relation as required or with a defaultValue

* chore: Test to update a class with a relation field with options

* chore: Improve tests
This commit is contained in:
Lucas Alencar
2019-08-21 01:27:49 -03:00
committed by Antonio Davi Macedo Coelho de Castro
parent fddd9c26b2
commit b9839c1e92
2 changed files with 118 additions and 6 deletions

View File

@@ -898,22 +898,34 @@ export default class SchemaController {
error: 'field ' + fieldName + ' cannot be added',
};
}
const type = fields[fieldName];
const error = fieldTypeIsInvalid(type);
const fieldType = fields[fieldName];
const error = fieldTypeIsInvalid(fieldType);
if (error) return { code: error.code, error: error.message };
if (type.defaultValue !== undefined) {
let defaultValueType = getType(type.defaultValue);
if (fieldType.defaultValue !== undefined) {
let defaultValueType = getType(fieldType.defaultValue);
if (typeof defaultValueType === 'string') {
defaultValueType = { type: defaultValueType };
} else if (typeof defaultValueType === 'object' && fieldType.type === 'Relation') {
return {
code: Parse.Error.INCORRECT_TYPE,
error: `The 'default value' option is not applicable for ${typeToString(fieldType)}`
};
}
if (!dbTypeMatchesObjectType(type, defaultValueType)) {
if (!dbTypeMatchesObjectType(fieldType, defaultValueType)) {
return {
code: Parse.Error.INCORRECT_TYPE,
error: `schema mismatch for ${className}.${fieldName} default value; expected ${typeToString(
type
fieldType
)} but got ${typeToString(defaultValueType)}`,
};
}
} else if (fieldType.required) {
if (typeof fieldType === 'object' && fieldType.type === 'Relation') {
return {
code: Parse.Error.INCORRECT_TYPE,
error: `The 'required' option is not applicable for ${typeToString(fieldType)}`
};
}
}
}
}