31 lines
1.3 KiB
JavaScript
31 lines
1.3 KiB
JavaScript
import { APIError } from 'payload';
|
|
/**
|
|
* Field hook for a System Page role relationship in SiteSettings: ensures a page
|
|
* isn't assigned to two roles at once (e.g. the same page as both homepage and
|
|
* privacyPolicy), which would make routing ambiguous.
|
|
*
|
|
* Attach to each role field's beforeValidate. `siblingFields` are the OTHER role
|
|
* field names to check against.
|
|
*
|
|
* hooks: { beforeValidate: [buildValidateUniqueRole({
|
|
* siblingFields: ['privacyPolicy', 'cookiePolicy', 'termsOfService'],
|
|
* })] }
|
|
*/ export function buildValidateUniqueRole(args) {
|
|
return ({ field, siblingData, value })=>{
|
|
if (value == null) {
|
|
return value;
|
|
}
|
|
const thisId = typeof value === 'object' ? value.id : value;
|
|
for (const sibling of args.siblingFields){
|
|
const other = siblingData?.[sibling];
|
|
const otherId = other && typeof other === 'object' ? other.id : other;
|
|
if (otherId != null && String(otherId) === String(thisId)) {
|
|
const name = typeof field === 'object' && 'name' in field ? field.name : 'ta rola';
|
|
throw new APIError(`Ta sama strona jest przypisana do "${name}" i "${sibling}". ` + `Każda rola systemowa musi wskazywać inną stronę.`, 400);
|
|
}
|
|
}
|
|
return value;
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=validateUniqueRole.js.map
|