2022-11-14 17:35:21 +01:00
|
|
|
import type { Diff } from '../../types';
|
2022-11-15 13:52:11 +01:00
|
|
|
|
2022-11-14 17:35:21 +01:00
|
|
|
import { jsonDiffs } from '../utils';
|
|
|
|
|
2022-11-15 14:15:25 +01:00
|
|
|
const strategies = {
|
|
|
|
// No diffs
|
2022-11-28 16:41:51 +01:00
|
|
|
exact(diffs: Diff[]) {
|
2022-11-15 14:15:25 +01:00
|
|
|
return diffs;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Diffs allowed on specific attributes properties
|
2022-11-28 16:41:51 +01:00
|
|
|
strict(diffs: Diff[]) {
|
2022-11-15 14:15:25 +01:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
};
|
2022-11-14 17:35:21 +01:00
|
|
|
|
2022-11-15 14:15:25 +01:00
|
|
|
const compareSchemas = <T, P>(a: T, b: P, strategy: keyof typeof strategies) => {
|
|
|
|
const diffs = jsonDiffs(a, b);
|
|
|
|
return strategies[strategy](diffs);
|
2022-11-14 17:35:21 +01:00
|
|
|
};
|
|
|
|
|
2022-11-15 14:15:25 +01:00
|
|
|
export default compareSchemas;
|