Skip to content

Commit

Permalink
Fix integration child mapping validation bug (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiddis authored Nov 22, 2023
1 parent 1f8c64d commit 46259f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ describe('Validation', () => {

describe('doPropertyValidation', () => {
it('should return true if all properties pass validation', () => {
const rootType = 'root';
const dataSourceProps = {
prop1: { type: 'string' },
prop2: { type: 'number' },
Expand All @@ -121,13 +120,12 @@ describe('Validation', () => {
},
};

const result = doPropertyValidation(rootType, dataSourceProps as any, requiredMappings);
const result = doPropertyValidation(dataSourceProps as any, requiredMappings);

expect(result).toBe(true);
});

it('should return false if a property fails validation', () => {
const rootType = 'root';
const dataSourceProps = {
prop1: { type: 'string' },
prop2: { type: 'number' },
Expand All @@ -145,13 +143,12 @@ describe('Validation', () => {
},
};

const result = doPropertyValidation(rootType, dataSourceProps as any, requiredMappings);
const result = doPropertyValidation(dataSourceProps as any, requiredMappings);

expect(result).toBe(false);
});

it('should return false if a required nested property is missing', () => {
const rootType = 'root';
const dataSourceProps = {
prop1: { type: 'string' },
};
Expand All @@ -168,9 +165,40 @@ describe('Validation', () => {
},
};

const result = doPropertyValidation(rootType, dataSourceProps as any, requiredMappings);
const result = doPropertyValidation(dataSourceProps as any, requiredMappings);

expect(result).toBe(false);
});

it('should correctly handle if the properties in a mapping mismatch with the mapping name', () => {
const dataSourceProps = {
prop1: { type: 'string' },
prop2: { type: 'number' },
};
const requiredMappings = {
root: {
template: {
mappings: {
properties: {
prop1: { type: 'string' },
},
},
},
},
child: {
template: {
mappings: {
properties: {
prop2: { type: 'number' },
},
},
},
},
};

const result = doPropertyValidation(dataSourceProps as any, requiredMappings);

expect(result).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,17 @@ export const doNestedPropertyValidation = (
};

export const doPropertyValidation = (
rootType: string,
dataSourceProps: { [key: string]: { properties?: any } },
requiredMappings: { [key: string]: { template: { mappings: { properties?: any } } } }
): boolean => {
// Check root object type (without dependencies)
for (const [key, value] of Object.entries(
requiredMappings[rootType].template.mappings.properties
)) {
if (!dataSourceProps[key] || !doNestedPropertyValidation(dataSourceProps[key], value as any)) {
return false;
}
}
// Check nested dependencies
for (const [key, value] of Object.entries(requiredMappings)) {
if (key === rootType) {
continue;
}
if (
!dataSourceProps[key] ||
!doNestedPropertyValidation(dataSourceProps[key], value.template.mappings.properties)
) {
return false;
for (const mapping of Object.values(requiredMappings)) {
for (const [key, value] of Object.entries(mapping.template.mappings.properties)) {
if (
!dataSourceProps[key] ||
!doNestedPropertyValidation(dataSourceProps[key], value as any)
) {
return false;
}
}
}
return true;
Expand Down Expand Up @@ -188,7 +177,7 @@ export function AddIntegrationFlyout(props: IntegrationFlyoutProps) {
return false;
}
const validationResult = Object.values(dataSourceMappings).every((value) =>
doPropertyValidation(integrationType, value.properties, integrationMappings)
doPropertyValidation(value.properties, integrationMappings)
);
if (!validationResult) {
validationErrors.push('The provided index does not match the schema');
Expand Down

0 comments on commit 46259f2

Please sign in to comment.