1
import { TYPES } from '@directus/constants';
3
import { ALIAS_TYPES } from '../constants.js';
4
import { getDatabaseClient } from '../database/index.js';
5
import { InvalidPayloadError } from '@directus/errors';
6
import type { Snapshot } from '../types/index.js';
7
import { DatabaseClients } from '../types/index.js';
8
import { version } from 'directus/version';
10
const snapshotJoiSchema = Joi.object({
11
version: Joi.number().valid(1).required(),
12
directus: Joi.string().required(),
14
.valid(...DatabaseClients)
16
collections: Joi.array().items(
18
collection: Joi.string(),
25
fields: Joi.array().items(
27
collection: Joi.string(),
31
default_value: Joi.any(),
32
max_length: [Joi.number(), Joi.string(), Joi.valid(null)],
33
is_nullable: Joi.bool(),
38
.valid(...TYPES, ...ALIAS_TYPES)
42
relations: Joi.array().items(
44
collection: Joi.string(),
47
related_collection: Joi.any(),
56
export function validateSnapshot(snapshot: Snapshot, force = false) {
57
const { error } = snapshotJoiSchema.validate(snapshot);
58
if (error) throw new InvalidPayloadError({ reason: error.message });
63
if (snapshot.directus !== version) {
64
throw new InvalidPayloadError({
65
reason: `Provided snapshot's directus version ${snapshot.directus} does not match the current instance's version ${version}. You can bypass this check by passing the "force" query parameter`,
69
if (!snapshot.vendor) {
70
throw new InvalidPayloadError({
72
'Provided snapshot does not contain the "vendor" property. You can bypass this check by passing the "force" query parameter',
76
const currentVendor = getDatabaseClient();
78
if (snapshot.vendor !== currentVendor) {
79
throw new InvalidPayloadError({
80
reason: `Provided snapshot's vendor ${snapshot.vendor} does not match the current instance's vendor ${currentVendor}. You can bypass this check by passing the "force" query parameter`,