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:
committed by
Antonio Davi Macedo Coelho de Castro
parent
fddd9c26b2
commit
b9839c1e92
@@ -431,6 +431,7 @@ describe('schemas', () => {
|
|||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
defaultZero: { type: 'Number', defaultValue: 0 },
|
defaultZero: { type: 'Number', defaultValue: 0 },
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass' }
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).then(async response => {
|
}).then(async response => {
|
||||||
@@ -457,6 +458,7 @@ describe('schemas', () => {
|
|||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
defaultZero: { type: 'Number', defaultValue: 0 },
|
defaultZero: { type: 'Number', defaultValue: 0 },
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass' }
|
||||||
},
|
},
|
||||||
classLevelPermissions: defaultClassLevelPermissions,
|
classLevelPermissions: defaultClassLevelPermissions,
|
||||||
});
|
});
|
||||||
@@ -479,10 +481,108 @@ describe('schemas', () => {
|
|||||||
expect(obj.get('defaultFalse')).toEqual(false);
|
expect(obj.get('defaultFalse')).toEqual(false);
|
||||||
expect(obj.get('defaultZero')).toEqual(0);
|
expect(obj.get('defaultZero')).toEqual(0);
|
||||||
expect(obj.get('ptr')).toBeUndefined();
|
expect(obj.get('ptr')).toBeUndefined();
|
||||||
|
expect(obj.get('relation')).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('try to set a relation field as a required field', async (done) => {
|
||||||
|
try {
|
||||||
|
await request({
|
||||||
|
url: 'http://localhost:8378/1/schemas',
|
||||||
|
method: 'POST',
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
json: true,
|
||||||
|
body: {
|
||||||
|
className: 'NewClassWithRelationRequired',
|
||||||
|
fields: {
|
||||||
|
foo: { type: 'String' },
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass', required: true }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
fail('should fail');
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.data.code).toEqual(111);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('try to set a relation field with a default value', async (done) => {
|
||||||
|
try {
|
||||||
|
await request({
|
||||||
|
url: 'http://localhost:8378/1/schemas',
|
||||||
|
method: 'POST',
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
json: true,
|
||||||
|
body: {
|
||||||
|
className: 'NewClassRelationWithOptions',
|
||||||
|
fields: {
|
||||||
|
foo: { type: 'String' },
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass', defaultValue: { __type: 'Relation', className: '_User' } }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
fail('should fail');
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.data.code).toEqual(111);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('try to update schemas with a relation field with options', async (done) => {
|
||||||
|
await request({
|
||||||
|
url: 'http://localhost:8378/1/schemas',
|
||||||
|
method: 'POST',
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
json: true,
|
||||||
|
body: {
|
||||||
|
className: 'NewClassRelationWithOptions',
|
||||||
|
fields: {
|
||||||
|
foo: { type: 'String' }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await request({
|
||||||
|
url: 'http://localhost:8378/1/schemas/NewClassRelationWithOptions',
|
||||||
|
method: 'POST',
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
json: true,
|
||||||
|
body: {
|
||||||
|
className: 'NewClassRelationWithOptions',
|
||||||
|
fields: {
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass', required: true }
|
||||||
|
},
|
||||||
|
_method: "PUT"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
fail('should fail');
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.data.code).toEqual(111);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await request({
|
||||||
|
url: 'http://localhost:8378/1/schemas/NewClassRelationWithOptions',
|
||||||
|
method: 'POST',
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
json: true,
|
||||||
|
body: {
|
||||||
|
className: 'NewClassRelationWithOptions',
|
||||||
|
fields: {
|
||||||
|
relation: { type: 'Relation', targetClass: 'SomeClass', defaultValue: { __type: 'Relation', className: '_User' } }
|
||||||
|
},
|
||||||
|
_method: "PUT"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
fail('should fail');
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.data.code).toEqual(111);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
it('validated the data type of default values when creating a new class', async () => {
|
it('validated the data type of default values when creating a new class', async () => {
|
||||||
try {
|
try {
|
||||||
await request({
|
await request({
|
||||||
|
|||||||
@@ -898,22 +898,34 @@ export default class SchemaController {
|
|||||||
error: 'field ' + fieldName + ' cannot be added',
|
error: 'field ' + fieldName + ' cannot be added',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const type = fields[fieldName];
|
const fieldType = fields[fieldName];
|
||||||
const error = fieldTypeIsInvalid(type);
|
const error = fieldTypeIsInvalid(fieldType);
|
||||||
if (error) return { code: error.code, error: error.message };
|
if (error) return { code: error.code, error: error.message };
|
||||||
if (type.defaultValue !== undefined) {
|
if (fieldType.defaultValue !== undefined) {
|
||||||
let defaultValueType = getType(type.defaultValue);
|
let defaultValueType = getType(fieldType.defaultValue);
|
||||||
if (typeof defaultValueType === 'string') {
|
if (typeof defaultValueType === 'string') {
|
||||||
defaultValueType = { type: defaultValueType };
|
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 {
|
return {
|
||||||
code: Parse.Error.INCORRECT_TYPE,
|
code: Parse.Error.INCORRECT_TYPE,
|
||||||
error: `schema mismatch for ${className}.${fieldName} default value; expected ${typeToString(
|
error: `schema mismatch for ${className}.${fieldName} default value; expected ${typeToString(
|
||||||
type
|
fieldType
|
||||||
)} but got ${typeToString(defaultValueType)}`,
|
)} 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)}`
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user