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
@@ -664,6 +664,13 @@ export default class SchemaController {
|
||||
classLevelPermissions
|
||||
);
|
||||
if (validationError) {
|
||||
if (validationError instanceof Parse.Error) {
|
||||
return Promise.reject(validationError);
|
||||
} else if (validationError.code && validationError.error) {
|
||||
return Promise.reject(
|
||||
new Parse.Error(validationError.code, validationError.error)
|
||||
);
|
||||
}
|
||||
return Promise.reject(validationError);
|
||||
}
|
||||
|
||||
@@ -884,8 +891,23 @@ export default class SchemaController {
|
||||
error: 'field ' + fieldName + ' cannot be added',
|
||||
};
|
||||
}
|
||||
const error = fieldTypeIsInvalid(fields[fieldName]);
|
||||
const type = fields[fieldName];
|
||||
const error = fieldTypeIsInvalid(type);
|
||||
if (error) return { code: error.code, error: error.message };
|
||||
if (type.defaultValue !== undefined) {
|
||||
let defaultValueType = getType(type.defaultValue);
|
||||
if (typeof defaultValueType === 'string') {
|
||||
defaultValueType = { type: defaultValueType };
|
||||
}
|
||||
if (!dbTypeMatchesObjectType(type, defaultValueType)) {
|
||||
return {
|
||||
code: Parse.Error.INCORRECT_TYPE,
|
||||
error: `schema mismatch for ${className}.${fieldName} default value; expected ${typeToString(
|
||||
type
|
||||
)} but got ${typeToString(defaultValueType)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -947,7 +969,22 @@ export default class SchemaController {
|
||||
|
||||
const expectedType = this.getExpectedType(className, fieldName);
|
||||
if (typeof type === 'string') {
|
||||
type = { type };
|
||||
type = ({ type }: SchemaField);
|
||||
}
|
||||
|
||||
if (type.defaultValue !== undefined) {
|
||||
let defaultValueType = getType(type.defaultValue);
|
||||
if (typeof defaultValueType === 'string') {
|
||||
defaultValueType = { type: defaultValueType };
|
||||
}
|
||||
if (!dbTypeMatchesObjectType(type, defaultValueType)) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INCORRECT_TYPE,
|
||||
`schema mismatch for ${className}.${fieldName} default value; expected ${typeToString(
|
||||
type
|
||||
)} but got ${typeToString(defaultValueType)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (expectedType) {
|
||||
|
||||
Reference in New Issue
Block a user