2
import { InvalidPayloadError } from '@directus/errors';
3
import type { SnapshotDiffWithHash, SnapshotWithHash } from '../types/snapshot.js';
4
import { DiffKind } from '../types/snapshot.js';
6
const deepDiffSchema = Joi.object({
8
.valid(...Object.values(DiffKind))
10
path: Joi.array().items(Joi.alternatives().try(Joi.string(), Joi.number())),
11
lhs: Joi.any().when('kind', { is: [DiffKind.NEW, DiffKind.ARRAY], then: Joi.optional(), otherwise: Joi.required() }),
12
rhs: Joi.any().when('kind', {
13
is: [DiffKind.DELETE, DiffKind.ARRAY],
15
otherwise: Joi.required(),
17
index: Joi.number().when('kind', { is: DiffKind.ARRAY, then: Joi.required() }),
18
item: Joi.link('#deepdiff').when('kind', { is: DiffKind.ARRAY, then: Joi.required() }),
21
const applyJoiSchema = Joi.object({
22
hash: Joi.string().required(),
24
collections: Joi.array()
27
collection: Joi.string().required(),
28
diff: Joi.array().items(deepDiffSchema).required(),
35
collection: Joi.string().required(),
36
field: Joi.string().required(),
37
diff: Joi.array().items(deepDiffSchema).required(),
41
relations: Joi.array()
44
collection: Joi.string().required(),
45
field: Joi.string().required(),
46
related_collection: Joi.string().allow(null),
47
diff: Joi.array().items(deepDiffSchema).required(),
59
export function validateApplyDiff(applyDiff: SnapshotDiffWithHash, currentSnapshotWithHash: SnapshotWithHash) {
60
const { error } = applyJoiSchema.validate(applyDiff);
61
if (error) throw new InvalidPayloadError({ reason: error.message });
65
applyDiff.diff.collections.length === 0 &&
66
applyDiff.diff.fields.length === 0 &&
67
applyDiff.diff.relations.length === 0
73
if (applyDiff.hash === currentSnapshotWithHash.hash) return true;
75
for (const diffCollection of applyDiff.diff.collections) {
76
const collection = diffCollection.collection;
78
if (diffCollection.diff[0]?.kind === DiffKind.NEW) {
79
const existingCollection = currentSnapshotWithHash.collections.find(
80
(c) => c.collection === diffCollection.collection,
83
if (existingCollection) {
84
throw new InvalidPayloadError({
85
reason: `Provided diff is trying to create collection "${collection}" but it already exists. Please generate a new diff and try again`,
88
} else if (diffCollection.diff[0]?.kind === DiffKind.DELETE) {
89
const existingCollection = currentSnapshotWithHash.collections.find(
90
(c) => c.collection === diffCollection.collection,
93
if (!existingCollection) {
94
throw new InvalidPayloadError({
95
reason: `Provided diff is trying to delete collection "${collection}" but it does not exist. Please generate a new diff and try again`,
101
for (const diffField of applyDiff.diff.fields) {
102
const field = `${diffField.collection}.${diffField.field}`;
104
if (diffField.diff[0]?.kind === DiffKind.NEW) {
105
const existingField = currentSnapshotWithHash.fields.find(
106
(f) => f.collection === diffField.collection && f.field === diffField.field,
110
throw new InvalidPayloadError({
111
reason: `Provided diff is trying to create field "${field}" but it already exists. Please generate a new diff and try again`,
114
} else if (diffField.diff[0]?.kind === DiffKind.DELETE) {
115
const existingField = currentSnapshotWithHash.fields.find(
116
(f) => f.collection === diffField.collection && f.field === diffField.field,
119
if (!existingField) {
120
throw new InvalidPayloadError({
121
reason: `Provided diff is trying to delete field "${field}" but it does not exist. Please generate a new diff and try again`,
127
for (const diffRelation of applyDiff.diff.relations) {
128
let relation = `${diffRelation.collection}.${diffRelation.field}`;
129
if (diffRelation.related_collection) relation += `-> ${diffRelation.related_collection}`;
131
if (diffRelation.diff[0]?.kind === DiffKind.NEW) {
132
const existingRelation = currentSnapshotWithHash.relations.find(
133
(r) => r.collection === diffRelation.collection && r.field === diffRelation.field,
136
if (existingRelation) {
137
throw new InvalidPayloadError({
138
reason: `Provided diff is trying to create relation "${relation}" but it already exists. Please generate a new diff and try again`,
141
} else if (diffRelation.diff[0]?.kind === DiffKind.DELETE) {
142
const existingRelation = currentSnapshotWithHash.relations.find(
143
(r) => r.collection === diffRelation.collection && r.field === diffRelation.field,
146
if (!existingRelation) {
147
throw new InvalidPayloadError({
148
reason: `Provided diff is trying to delete relation "${relation}" but it does not exist. Please generate a new diff and try again`,
154
throw new InvalidPayloadError({
155
reason: `Provided hash does not match the current instance's schema hash, indicating the schema has changed after this diff was generated. Please generate a new diff and try again`,