Merge branch 'master' into docs/google-app-engine

This commit is contained in:
Jim LAURIE 2020-06-05 14:09:29 +02:00 committed by GitHub
commit a94dffa4bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 5 deletions

View File

@ -28,7 +28,7 @@
<p align="center">
<a href="https://www.npmjs.org/package/strapi">
<img src="https://img.shields.io/npm/v/strapi/beta.svg" alt="NPM Version" />
<img src="https://img.shields.io/npm/v/strapi/latest.svg" alt="NPM Version" />
</a>
<a href="https://www.npmjs.org/package/strapi">
<img src="https://img.shields.io/npm/dm/strapi.svg" alt="Monthly download on NPM" />

View File

@ -215,7 +215,10 @@ To improve the Developer Experience when developing or using the administration
### Exceptions
- `uid` — This field type allows a `targetField` key. The value is the name of an attribute thas has `string` of `text` type.
**uid**
- `targetField`(string) — The value is the name of an attribute thas has `string` of `text` type.
- `options` (string) — The value is a set of options passed to [the underlying `uid` generator](https://github.com/sindresorhus/slugify). A caveat is that the resulting `uid` must abide to the following RegEx `/^[A-Za-z0-9-_.~]*$`.
### Example

View File

@ -132,6 +132,43 @@ describe('Test uid service', () => {
expect(uidWithEmptyTarget).toBe('my-test-model');
});
test('Uses options for generation', async () => {
global.strapi = {
contentTypes: {
'my-model': {
modelName: 'myTestModel',
attributes: {
title: {
type: 'string',
},
slug: {
type: 'uid',
targetField: 'title',
options: { lowercase: false },
},
},
},
},
db: {
query() {
return {
find: async () => [],
};
},
},
};
const uid = await uidService.generateUIDField({
contentTypeUID: 'my-model',
field: 'slug',
data: {
title: 'Test title',
},
});
expect(uid).toBe('Test-title');
});
test('Ignores minLength attribute (should be handle by the user)', async () => {
global.strapi = {
contentTypes: {

View File

@ -8,21 +8,21 @@ module.exports = {
const contentType = strapi.contentTypes[contentTypeUID];
const { attributes } = contentType;
const { targetField, default: defaultValue } = attributes[field];
const { targetField, default: defaultValue, options } = attributes[field];
const targetValue = _.get(data, targetField);
if (!_.isEmpty(targetValue)) {
return this.findUniqueUID({
contentTypeUID,
field,
value: slugify(targetValue),
value: slugify(targetValue, options),
});
}
return this.findUniqueUID({
contentTypeUID,
field,
value: slugify(defaultValue || contentType.modelName),
value: slugify(defaultValue || contentType.modelName, options),
});
},

View File

@ -86,6 +86,19 @@ const getTypeShape = (attribute, { modelType, attributes } = {}) => {
.test(isValidUID),
minLength: validators.minLength,
maxLength: validators.maxLength.max(256).test(maxLengthIsGreaterThanOrEqualToMinLength),
options: yup.object().shape({
separator: yup.string(),
lowercase: yup.boolean(),
decamelize: yup.boolean(),
customReplacements: yup.array().of(
yup
.array()
.of(yup.string())
.min(2)
.max(2)
),
preserveLeadingUnderscore: yup.boolean(),
}),
};
}