Refactor schema comparisson to make it simpler

This commit is contained in:
Christian Capeans 2022-11-15 14:15:25 +01:00
parent 00c964b1d7
commit a9d20f759b
2 changed files with 33 additions and 38 deletions

View File

@ -8,7 +8,7 @@ import type {
import { isEmpty, uniq } from 'lodash/fp';
import calculateSchemaDiffs from '../strategies';
import compareSchemas from '../strategies';
class TransferEngine implements ITransferEngine {
sourceProvider: ISourceProvider;
@ -61,19 +61,17 @@ class TransferEngine implements ITransferEngine {
}
#assertSchemasMatching(sourceSchemas: any, destinationSchemas: any) {
const chosenStrategy = this.options.schemasMatching || 'strict';
const strategy = this.options.schemasMatching || 'strict';
const keys = uniq(Object.keys(sourceSchemas).concat(Object.keys(destinationSchemas)));
const diffs: { [key: string]: Diff[] } = {};
keys.forEach((key) => {
const sourceSchema = sourceSchemas[key];
const destinationSchema = destinationSchemas[key];
const schemaDiffs = calculateSchemaDiffs(sourceSchema, destinationSchema);
const schemaDiffs = compareSchemas(sourceSchema, destinationSchema, strategy);
const isValid = schemaDiffs[chosenStrategy]();
if (!isValid) {
diffs[key] = schemaDiffs.diffs;
if (schemaDiffs.length) {
diffs[key] = schemaDiffs;
}
});
@ -126,7 +124,7 @@ class TransferEngine implements ITransferEngine {
return true;
} catch (error) {
if (error instanceof Error) {
console.error('Integrity checks failed:', error);
console.error('Integrity checks failed:', error.message);
}
return false;

View File

@ -2,38 +2,35 @@ import type { Diff } from '../../types';
import { jsonDiffs } from '../utils';
const calculateSchemaDiffs = <T, P>(a: T, b: P) => {
const diffs = jsonDiffs(a, b);
const strategies = {
// No diffs
exact: (diffs: Diff[]) => {
return diffs;
},
return {
get diffs() {
return diffs;
},
// Diffs allowed on specific attributes properties
strict: (diffs: Diff[]) => {
const isIgnorableDiff = ({ path }: Diff) => {
return (
path.length === 3 &&
// Root property must be attributes
path[0] === 'attributes' &&
// Need a valid string attribute name
typeof path[1] === 'string' &&
// The diff must be on ignorable attribute properties
['private', 'required', 'configurable'].includes(path[2])
);
};
// No diffs
exact() {
return diffs.length === 0;
},
const shouldKeepDiff = (diff: Diff) => !isIgnorableDiff(diff);
// Diffs allowed on specific attributes properties
strict() {
const isIgnorableDiff = ({ path }: Diff) => {
return (
path.length === 3 &&
// Root property must be attributes
path[0] === 'attributes' &&
// Need a valid string attribute name
typeof path[1] === 'string' &&
// The diff must be on ignorable attribute properties
['private', 'required', 'configurable'].includes(path[2])
);
};
const shouldKeepDiff = (diff: Diff) => !isIgnorableDiff(diff);
return diffs.filter(shouldKeepDiff).length === 0;
},
};
return diffs.filter(shouldKeepDiff);
},
};
export default calculateSchemaDiffs;
const compareSchemas = <T, P>(a: T, b: P, strategy: keyof typeof strategies) => {
const diffs = jsonDiffs(a, b);
return strategies[strategy](diffs);
};
export default compareSchemas;