directus

Форк
0
/
validate-snapshot.ts 
83 строки · 2.4 Кб
1
import { TYPES } from '@directus/constants';
2
import Joi from 'joi';
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';
9

10
const snapshotJoiSchema = Joi.object({
11
	version: Joi.number().valid(1).required(),
12
	directus: Joi.string().required(),
13
	vendor: Joi.string()
14
		.valid(...DatabaseClients)
15
		.optional(),
16
	collections: Joi.array().items(
17
		Joi.object({
18
			collection: Joi.string(),
19
			meta: Joi.any(),
20
			schema: Joi.object({
21
				name: Joi.string(),
22
			}),
23
		}),
24
	),
25
	fields: Joi.array().items(
26
		Joi.object({
27
			collection: Joi.string(),
28
			field: Joi.string(),
29
			meta: Joi.any(),
30
			schema: Joi.object({
31
				default_value: Joi.any(),
32
				max_length: [Joi.number(), Joi.string(), Joi.valid(null)],
33
				is_nullable: Joi.bool(),
34
			})
35
				.unknown()
36
				.allow(null),
37
			type: Joi.string()
38
				.valid(...TYPES, ...ALIAS_TYPES)
39
				.allow(null),
40
		}),
41
	),
42
	relations: Joi.array().items(
43
		Joi.object({
44
			collection: Joi.string(),
45
			field: Joi.string(),
46
			meta: Joi.any(),
47
			related_collection: Joi.any(),
48
			schema: Joi.any(),
49
		}),
50
	),
51
});
52

53
/**
54
 * Validates the snapshot against the current instance.
55
 **/
56
export function validateSnapshot(snapshot: Snapshot, force = false) {
57
	const { error } = snapshotJoiSchema.validate(snapshot);
58
	if (error) throw new InvalidPayloadError({ reason: error.message });
59

60
	// Bypass checks when "force" option is enabled
61
	if (force) return;
62

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`,
66
		});
67
	}
68

69
	if (!snapshot.vendor) {
70
		throw new InvalidPayloadError({
71
			reason:
72
				'Provided snapshot does not contain the "vendor" property. You can bypass this check by passing the "force" query parameter',
73
		});
74
	}
75

76
	const currentVendor = getDatabaseClient();
77

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`,
81
		});
82
	}
83
}
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.