mirror of
https://github.com/strapi/strapi.git
synced 2025-07-24 01:18:17 +00:00
Merge branch 'master' into docs/google-app-engine
This commit is contained in:
commit
a94dffa4bb
@ -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" />
|
||||
|
@ -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
|
||||
|
||||
|
@ -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: {
|
||||
|
@ -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),
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -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(),
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user