-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbefore-create-update.js
57 lines (46 loc) · 1.68 KB
/
before-create-update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const get = require('lodash/get');
const { ValidationError } = require('@strapi/utils').errors;
const { getService } = require('../utils');
module.exports = async ({ strapi }) => {
const layouts = await getService('config').layouts();
const uids = await getService('config').uids();
// Lifecycle hook to validate permalink values before they are created or updated.
const beforeCreateUpdate = async (event) => {
const { model, params } = event;
const { data, where } = params;
const { uid } = model;
const id = get(where, 'id', null);
const attr = layouts[uid];
const value = data[attr.name];
let locale = data.locale || undefined;
if (id && !locale && model.attributes.locale) {
const entity = await strapi.db.query(uid).findOne({ where: { id } });
locale = entity.locale;
}
await getService('validation').validateFormat(uid, value);
await getService('validation').validateConnection(uid, data, id);
// Check availability in each related collection.
const promisedAvailables = await Promise.all(
uids.map((uid) => {
const { name } = layouts[uid];
return getService('validation')
.validateAvailability(uid, name, value, id, locale)
.then((available) => ({
uid,
available,
}));
})
);
const isAvailable = promisedAvailables.every(({ available }) => available);
if (!isAvailable) {
throw new ValidationError(`Permalink value must be unique.`);
}
};
// Subscribe to lifecycle hook.
strapi.db.lifecycles.subscribe({
models: uids,
beforeCreate: beforeCreateUpdate,
beforeUpdate: beforeCreateUpdate,
});
};