Skip to content

Commit 93fd7ab

Browse files
committed
Fix a bug in validateDefinedFields where non-relation fields with the same name as relation fields would be accepted
1 parent c968a55 commit 93fd7ab

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

core/src/model/configuration/field.ts

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export module Field {
147147
export const SIMPLE_INPUT_TYPES = [SIMPLE_INPUT, SIMPLE_MULTIINPUT];
148148
export const SUBFIELD_INPUT_TYPES = [INPUT, SIMPLE_INPUT, TEXT, BOOLEAN, DROPDOWN, RADIO, CHECKBOXES,
149149
FLOAT, UNSIGNEDFLOAT, INT, UNSIGNEDINT, DATE, URL];
150+
export const RELATION_INPUT_TYPES = [RELATION, INSTANCE_OF, DERIVED_RELATION];
150151

151152
const INTERCHANGEABLE_INPUT_TYPES: Array<Array<InputType>> = [
152153
[INPUT, SIMPLE_INPUT, TEXT, DROPDOWN, RADIO],

desktop/src/app/model/validations.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ export module Validations {
411411
{ name: 'scanCode' } as Field
412412
];
413413

414-
const definedFields: Array<any> = projectFields.concat(defaultFields);
415-
416-
const invalidFields: Array<any> = [];
414+
const definedFields: Array<Field> = projectFields.concat(defaultFields);
415+
const invalidFields: string[] = [];
417416

418417
for (let resourceField in resource) {
419418
if (resource.hasOwnProperty(resourceField)) {
420419
let fieldFound: boolean = false;
421420
for (let definedField of definedFields) {
422-
if (definedField.name === resourceField) {
421+
if (definedField.name === resourceField
422+
&& !Field.InputType.RELATION_INPUT_TYPES.includes(definedField.inputType)) {
423423
fieldFound = true;
424424
break;
425425
}

desktop/test/unit/components/model/validations.spec.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ describe('Validations', () => {
6060
{ name: 'period4', label: 'period4', inputType: InputType.DROPDOWNRANGE },
6161
{ name: 'beginningDate', label: 'beginningDate', inputType: 'date' },
6262
{ name: 'endDate', label: 'endDate', inputType: 'date' },
63-
{ name: 'shortInput', label: 'shortInput', inputType: 'input', maxCharacters: 10 }
63+
{ name: 'shortInput', label: 'shortInput', inputType: 'input', maxCharacters: 10 },
64+
{ name: 'isBelow', label: 'isBelow', inputType: 'relation' }
6465
]
6566

6667
}]}, []],
@@ -119,7 +120,7 @@ describe('Validations', () => {
119120

120121
it('validate defined fields', () => {
121122

122-
const datastore = jasmine.createSpyObj('datastore',['find']);
123+
const datastore = jasmine.createSpyObj('datastore', ['find']);
123124
datastore.find.and.returnValues(Promise.resolve({ totalCount: 0, documents: [] }));
124125

125126
const doc = {
@@ -157,6 +158,26 @@ describe('Validations', () => {
157158
});
158159

159160

161+
it('validate defined fields - do not allow non-relation fields with the same name as relation fields', () => {
162+
163+
const datastore = jasmine.createSpyObj('datastore', ['find']);
164+
datastore.find.and.returnValues(Promise.resolve({ totalCount: 0, documents: [] }));
165+
166+
const doc = {
167+
resource: {
168+
id: '1',
169+
category: 'T',
170+
mandatory: 'm',
171+
isBelow: 'test',
172+
relations: { isBelow: ['0'] },
173+
}
174+
};
175+
176+
const undefinedFields = Validations.validateDefinedFields(doc.resource as any, projectConfiguration);
177+
expect(undefinedFields).toContain('isBelow');
178+
});
179+
180+
160181
it('should report nothing when omitting optional property', () => {
161182

162183
const datastore = jasmine.createSpyObj('datastore',['find']);

0 commit comments

Comments
 (0)