Skip to content

Commit

Permalink
[backend] do not fail object patch validation when object_path is def…
Browse files Browse the repository at this point in the history
…ined
  • Loading branch information
labo-flg committed Feb 26, 2024
1 parent 15fcc65 commit 2243e49
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const validateAndFormatSchemaAttribute = (
if (!attributeDefinition || isEmptyField(editInput.value)) {
return;
}
if (!attributeDefinition.multiple && editInput.value.length > 1) {
const isPatchObject = attributeDefinition.type === 'object' && !!editInput.object_path;
if (!isPatchObject && !attributeDefinition.multiple && editInput.value.length > 1) {
// with a patch object, this must be checked against the internal schema mappings
// such case is covered later in validateDataBeforeIndexing
throw ValidationError(attributeName, { message: `Attribute ${attributeName} cannot be multiple`, data: editInput });
}
// Data validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,122 @@ describe('Group resolver standard behavior', () => {
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: { key: 'group_confidence_level', object_path: '/group_confidence_level/overrides/0/max_confidence', value: [63] }
input: {
key: 'group_confidence_level',
object_path: '/group_confidence_level/overrides',
value: [
{ entity_type: 'Report', max_confidence: 70 },
{ entity_type: 'Malware', max_confidence: 25 }
],
}
},
});
expect(queryResult.data.groupEdit.fieldPatch.group_confidence_level).toEqual({
max_confidence: 87,
overrides: [
{ entity_type: 'Report', max_confidence: 70 },
{ entity_type: 'Malware', max_confidence: 25 },
],
});
queryResult = await queryAsAdmin({
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: { key: 'group_confidence_level', object_path: '/group_confidence_level/overrides/1/max_confidence', value: [63] }
},
});
expect(queryResult.data.groupEdit.fieldPatch.group_confidence_level).toEqual({
max_confidence: 87, // unchanged!
overrides: [{ entity_type: 'Report', max_confidence: 63 }],
overrides: [
{ entity_type: 'Report', max_confidence: 70 },
{ entity_type: 'Malware', max_confidence: 63 },
],
});
});
it('should context patch group', async () => {
const CONTEXT_PATCH_QUERY = gql`
mutation GroupEdit($id: ID!, $input: EditContext) {
groupEdit(id: $id) {
contextPatch(input: $input) {
id
}
}
}
`;
const queryResult = await queryAsAdmin({
query: CONTEXT_PATCH_QUERY,
variables: { id: groupInternalId, input: { focusOn: 'description' } },
});
expect(queryResult.data.groupEdit.contextPatch.id).toEqual(groupInternalId);
});
it('should fail to update group confidence level with invalid patch data', async () => {
const UPDATE_QUERY = gql`
mutation GroupEdit($id: ID!, $input: [EditInput]!) {
groupEdit(id: $id) {
fieldPatch(input: $input) {
id
}
}
}
`;
let queryResult = await queryAsAdmin({
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: {
key: 'group_confidence_level',
object_path: '/group_confidence_level/overrides',
value: [
{ entity_type: 'Report', max_confidence: 70 },
{ entity_type: 'Malware', max_confidence: null }
],
}
},
});
expect(queryResult.errors).toBeDefined();
expect(queryResult.errors[0].message).toBe('Validation against schema failed on attribute [max_confidence]: this mandatory field cannot be nil');

queryResult = await queryAsAdmin({
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: {
key: 'group_confidence_level',
object_path: '/group_confidence_level/overrides/1',
value: { entity_type: 'Malware' }
}
},
});
expect(queryResult.errors).toBeDefined();
expect(queryResult.errors[0].message).toBe('Validation against schema failed on attribute [overrides]: mandatory field [max_confidence] is not present');

queryResult = await queryAsAdmin({
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: {
key: 'group_confidence_level',
value: {
max_confidence: 87,
}
}
},
});
expect(queryResult.errors).toBeDefined();
expect(queryResult.errors[0].message).toBe('Validation against schema failed on attribute [group_confidence_level]: mandatory field [overrides] is not present');

queryResult = await queryAsAdmin({
query: UPDATE_QUERY,
variables: {
id: groupInternalId,
input: {
key: 'group_confidence_level',
value: 45
}
},
});
expect(queryResult.errors).toBeDefined();
expect(queryResult.errors[0].message).toBe('Validation against schema failed on attribute [group_confidence_level]: value must be an object');
});
it('should context patch group', async () => {
const CONTEXT_PATCH_QUERY = gql`
Expand Down

0 comments on commit 2243e49

Please sign in to comment.