UID is always localized in i18n

This commit is contained in:
Alexandre Bodin 2021-04-06 11:23:43 +02:00
parent 54b51d9d51
commit f2cddb4c15
4 changed files with 44 additions and 1 deletions

View File

@ -114,6 +114,21 @@ describe('content-types service', () => {
})
).toEqual(['stars', 'price']);
});
test('Consider uid to always be localized', () => {
expect(
getNonLocalizedAttributes({
attributes: {
price: {
type: 'integer',
},
slug: {
type: 'uid',
},
},
})
).toEqual(['price']);
});
});
describe('getValidLocale', () => {

View File

@ -6,6 +6,7 @@ const {
isRelationalAttribute,
getVisibleAttributes,
isMediaAttribute,
isTypedAttribute,
} = require('strapi-utils').contentTypes;
const { getService } = require('../utils');
@ -82,7 +83,9 @@ const isLocalizedAttribute = (model, attributeName) => {
const attribute = model.attributes[attributeName];
return (
isLocalized(attribute) || (isRelationalAttribute(attribute) && !isMediaAttribute(attribute))
isLocalized(attribute) ||
(isRelationalAttribute(attribute) && !isMediaAttribute(attribute)) ||
isTypedAttribute(attribute, 'uid')
);
};

View File

@ -2,6 +2,7 @@
const {
isPrivateAttribute,
isTypedAttribute,
getPrivateAttributes,
getVisibleAttributes,
getNonWritableAttributes,
@ -257,4 +258,18 @@ describe('Content types utils', () => {
}
);
});
describe('isTypedAttribute', () => {
test('Returns false if attribute does not have a type', () => {
expect(isTypedAttribute({})).toBe(false);
});
test('Returns true if attribute type matches passed type', () => {
expect(isTypedAttribute({ type: 'test' }, 'test')).toBe(true);
});
test('Returns false if type do not match', () => {
expect(isTypedAttribute({ type: 'test' }, 'other-type')).toBe(false);
});
});
});

View File

@ -191,6 +191,15 @@ const getGlobalId = (model, modelName, prefix) => {
const isRelationalAttribute = attribute =>
_.has(attribute, 'model') || _.has(attribute, 'collection');
/**
* Checks if an attribute is of type `type`
* @param {*} attribute
* @param {*} type
*/
const isTypedAttribute = (attribute, type) => {
return _.has(attribute, 'type') && attribute.type === type;
};
/**
* Returns a route prefix for a contentType
* @param {object} contentType
@ -206,6 +215,7 @@ module.exports = {
isScalarAttribute,
isMediaAttribute,
isRelationalAttribute,
isTypedAttribute,
getPrivateAttributes,
getTimestampsAttributes,
isPrivateAttribute,