@strapi/core 5.0.0-rc.2 → 5.0.0-rc.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Strapi.js +2 -2
- package/dist/Strapi.js.map +1 -1
- package/dist/configuration/get-dirs.js +2 -2
- package/dist/configuration/get-dirs.js.map +1 -1
- package/dist/configuration/index.js +2 -2
- package/dist/configuration/index.js.map +1 -1
- package/dist/core-api/controller/collection-type.js +3 -3
- package/dist/core-api/controller/collection-type.js.map +1 -1
- package/dist/core-api/controller/index.js +2 -2
- package/dist/core-api/controller/index.js.map +1 -1
- package/dist/core-api/controller/single-type.js +2 -2
- package/dist/core-api/controller/single-type.js.map +1 -1
- package/dist/core-api/controller/transform.js +8 -8
- package/dist/core-api/controller/transform.js.map +1 -1
- package/dist/core-api/service/collection-type.d.ts +2 -2
- package/dist/core-api/service/pagination.js +8 -8
- package/dist/core-api/service/pagination.js.map +1 -1
- package/dist/domain/content-type/index.js +4 -4
- package/dist/domain/content-type/index.js.map +1 -1
- package/dist/ee/index.js +3 -3
- package/dist/ee/index.js.map +1 -1
- package/dist/factories.js +4 -4
- package/dist/factories.js.map +1 -1
- package/dist/factories.mjs.map +1 -1
- package/dist/loaders/apis.js +2 -2
- package/dist/loaders/apis.js.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.d.ts.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.js +39 -11
- package/dist/loaders/plugins/get-enabled-plugins.js.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.mjs +9 -3
- package/dist/loaders/plugins/get-enabled-plugins.mjs.map +1 -1
- package/dist/loaders/plugins/get-user-plugins-config.js +2 -2
- package/dist/loaders/plugins/get-user-plugins-config.js.map +1 -1
- package/dist/loaders/plugins/index.d.ts.map +1 -1
- package/dist/loaders/plugins/index.js +34 -7
- package/dist/loaders/plugins/index.js.map +1 -1
- package/dist/loaders/plugins/index.mjs +9 -1
- package/dist/loaders/plugins/index.mjs.map +1 -1
- package/dist/middlewares/body.js +2 -2
- package/dist/middlewares/body.js.map +1 -1
- package/dist/middlewares/public.js +2 -2
- package/dist/middlewares/public.js.map +1 -1
- package/dist/middlewares/query.js.map +1 -1
- package/dist/middlewares/query.mjs.map +1 -1
- package/dist/middlewares/responses.js +2 -2
- package/dist/middlewares/responses.js.map +1 -1
- package/dist/middlewares/security.d.ts.map +1 -1
- package/dist/middlewares/security.js +4 -4
- package/dist/middlewares/security.js.map +1 -1
- package/dist/middlewares/security.mjs +1 -1
- package/dist/middlewares/security.mjs.map +1 -1
- package/dist/middlewares/session.js +2 -2
- package/dist/middlewares/session.js.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.d.ts +12 -9
- package/dist/migrations/database/5.0.0-discard-drafts.d.ts.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.js +50 -11
- package/dist/migrations/database/5.0.0-discard-drafts.js.map +1 -1
- package/dist/migrations/database/5.0.0-discard-drafts.mjs +51 -12
- package/dist/migrations/database/5.0.0-discard-drafts.mjs.map +1 -1
- package/dist/providers/admin.d.ts.map +1 -1
- package/dist/providers/admin.js.map +1 -1
- package/dist/providers/admin.mjs.map +1 -1
- package/dist/registries/apis.js +2 -2
- package/dist/registries/apis.js.map +1 -1
- package/dist/registries/components.js +2 -2
- package/dist/registries/components.js.map +1 -1
- package/dist/registries/content-types.js +3 -3
- package/dist/registries/content-types.js.map +1 -1
- package/dist/registries/controllers.js +3 -3
- package/dist/registries/controllers.js.map +1 -1
- package/dist/registries/custom-fields.js +4 -4
- package/dist/registries/custom-fields.js.map +1 -1
- package/dist/registries/hooks.js +2 -2
- package/dist/registries/hooks.js.map +1 -1
- package/dist/registries/middlewares.js +3 -3
- package/dist/registries/middlewares.js.map +1 -1
- package/dist/registries/modules.js +3 -3
- package/dist/registries/modules.js.map +1 -1
- package/dist/registries/plugins.js +2 -2
- package/dist/registries/plugins.js.map +1 -1
- package/dist/registries/policies.d.ts +1 -1
- package/dist/registries/policies.d.ts.map +1 -1
- package/dist/registries/policies.js +5 -5
- package/dist/registries/policies.js.map +1 -1
- package/dist/registries/policies.mjs +1 -1
- package/dist/registries/policies.mjs.map +1 -1
- package/dist/registries/services.js +3 -3
- package/dist/registries/services.js.map +1 -1
- package/dist/services/auth/index.js +3 -3
- package/dist/services/auth/index.js.map +1 -1
- package/dist/services/content-api/index.d.ts +10 -12
- package/dist/services/content-api/index.d.ts.map +1 -1
- package/dist/services/content-api/permissions/index.d.ts +10 -12
- package/dist/services/content-api/permissions/index.d.ts.map +1 -1
- package/dist/services/content-api/permissions/providers/action.d.ts +5 -6
- package/dist/services/content-api/permissions/providers/action.d.ts.map +1 -1
- package/dist/services/content-api/permissions/providers/condition.d.ts +5 -6
- package/dist/services/content-api/permissions/providers/condition.d.ts.map +1 -1
- package/dist/services/core-store.js +3 -3
- package/dist/services/core-store.js.map +1 -1
- package/dist/services/cron.d.ts +3 -3
- package/dist/services/cron.d.ts.map +1 -1
- package/dist/services/cron.js +3 -3
- package/dist/services/cron.js.map +1 -1
- package/dist/services/cron.mjs.map +1 -1
- package/dist/services/document-service/attributes/index.js +2 -2
- package/dist/services/document-service/attributes/index.js.map +1 -1
- package/dist/services/document-service/attributes/transforms.js +3 -3
- package/dist/services/document-service/attributes/transforms.js.map +1 -1
- package/dist/services/document-service/components.js +15 -15
- package/dist/services/document-service/components.js.map +1 -1
- package/dist/services/document-service/draft-and-publish.js +16 -16
- package/dist/services/document-service/draft-and-publish.js.map +1 -1
- package/dist/services/document-service/entries.js +5 -5
- package/dist/services/document-service/entries.js.map +1 -1
- package/dist/services/document-service/internationalization.js +9 -9
- package/dist/services/document-service/internationalization.js.map +1 -1
- package/dist/services/document-service/params.js +2 -2
- package/dist/services/document-service/params.js.map +1 -1
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +51 -15
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +43 -7
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/transform/id-transform.js +2 -2
- package/dist/services/document-service/transform/id-transform.js.map +1 -1
- package/dist/services/document-service/transform/query.js +3 -3
- package/dist/services/document-service/transform/query.js.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.js +2 -2
- package/dist/services/document-service/transform/relations/extract/data-ids.js.map +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.js +2 -2
- package/dist/services/document-service/transform/relations/transform/data-ids.js.map +1 -1
- package/dist/services/document-service/transform/relations/utils/dp.js +2 -2
- package/dist/services/document-service/transform/relations/utils/dp.js.map +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.js +6 -6
- package/dist/services/document-service/transform/relations/utils/map-relation.js.map +1 -1
- package/dist/services/document-service/utils/populate.d.ts.map +1 -1
- package/dist/services/document-service/utils/populate.js +3 -1
- package/dist/services/document-service/utils/populate.js.map +1 -1
- package/dist/services/document-service/utils/populate.mjs +3 -1
- package/dist/services/document-service/utils/populate.mjs.map +1 -1
- package/dist/services/document-service/utils/unidirectional-relations.d.ts +33 -0
- package/dist/services/document-service/utils/unidirectional-relations.d.ts.map +1 -0
- package/dist/services/document-service/utils/unidirectional-relations.js +58 -0
- package/dist/services/document-service/utils/unidirectional-relations.js.map +1 -0
- package/dist/services/document-service/utils/unidirectional-relations.mjs +58 -0
- package/dist/services/document-service/utils/unidirectional-relations.mjs.map +1 -0
- package/dist/services/entity-validator/blocks-validator.d.ts +1 -2
- package/dist/services/entity-validator/blocks-validator.d.ts.map +1 -1
- package/dist/services/entity-validator/blocks-validator.js +4 -3
- package/dist/services/entity-validator/blocks-validator.js.map +1 -1
- package/dist/services/entity-validator/blocks-validator.mjs +3 -3
- package/dist/services/entity-validator/blocks-validator.mjs.map +1 -1
- package/dist/services/entity-validator/index.d.ts +2 -1
- package/dist/services/entity-validator/index.d.ts.map +1 -1
- package/dist/services/entity-validator/index.js +28 -33
- package/dist/services/entity-validator/index.js.map +1 -1
- package/dist/services/entity-validator/index.mjs +14 -19
- package/dist/services/entity-validator/index.mjs.map +1 -1
- package/dist/services/entity-validator/validators.d.ts +32 -23
- package/dist/services/entity-validator/validators.d.ts.map +1 -1
- package/dist/services/entity-validator/validators.js +136 -63
- package/dist/services/entity-validator/validators.js.map +1 -1
- package/dist/services/entity-validator/validators.mjs +135 -63
- package/dist/services/entity-validator/validators.mjs.map +1 -1
- package/dist/services/server/compose-endpoint.js +7 -7
- package/dist/services/server/compose-endpoint.js.map +1 -1
- package/dist/services/server/koa.js +3 -3
- package/dist/services/server/koa.js.map +1 -1
- package/dist/services/server/middleware.js +3 -3
- package/dist/services/server/middleware.js.map +1 -1
- package/dist/services/server/routing.js +2 -2
- package/dist/services/server/routing.js.map +1 -1
- package/dist/services/utils/dynamic-zones.js +5 -5
- package/dist/services/utils/dynamic-zones.js.map +1 -1
- package/dist/utils/cron.js +3 -3
- package/dist/utils/cron.js.map +1 -1
- package/dist/utils/filepath-to-prop-path.d.ts +1 -1
- package/dist/utils/filepath-to-prop-path.d.ts.map +1 -1
- package/dist/utils/filepath-to-prop-path.js +27 -6
- package/dist/utils/filepath-to-prop-path.js.map +1 -1
- package/dist/utils/filepath-to-prop-path.mjs +25 -5
- package/dist/utils/filepath-to-prop-path.mjs.map +1 -1
- package/dist/utils/is-initialized.js +3 -3
- package/dist/utils/is-initialized.js.map +1 -1
- package/dist/utils/startup-logger.js +4 -4
- package/dist/utils/startup-logger.js.map +1 -1
- package/dist/utils/startup-logger.mjs +3 -3
- package/dist/utils/startup-logger.mjs.map +1 -1
- package/dist/utils/transform-content-types-to-models.d.ts +2 -2
- package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
- package/dist/utils/transform-content-types-to-models.js +6 -5
- package/dist/utils/transform-content-types-to-models.js.map +1 -1
- package/dist/utils/transform-content-types-to-models.mjs +5 -4
- package/dist/utils/transform-content-types-to-models.mjs.map +1 -1
- package/package.json +15 -15
@@ -1,4 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
2
3
|
const _ = require("lodash");
|
3
4
|
const strapiUtils = require("@strapi/utils");
|
4
5
|
const blocksValidator = require("./blocks-validator.js");
|
@@ -16,20 +17,20 @@ const addMaxLengthValidator = (validator, {
|
|
16
17
|
};
|
17
18
|
const addMinIntegerValidator = (validator, {
|
18
19
|
attr
|
19
|
-
}) => ___default.default.isNumber(attr.min) ? validator.min(___default.default.toInteger(attr.min)) : validator;
|
20
|
+
}, { isDraft }) => ___default.default.isNumber(attr.min) && !isDraft ? validator.min(___default.default.toInteger(attr.min)) : validator;
|
20
21
|
const addMaxIntegerValidator = (validator, {
|
21
22
|
attr
|
22
23
|
}) => ___default.default.isNumber(attr.max) ? validator.max(___default.default.toInteger(attr.max)) : validator;
|
23
24
|
const addMinFloatValidator = (validator, {
|
24
25
|
attr
|
25
|
-
}) => ___default.default.isNumber(attr.min) ? validator.min(attr.min) : validator;
|
26
|
+
}, { isDraft }) => ___default.default.isNumber(attr.min) && !isDraft ? validator.min(attr.min) : validator;
|
26
27
|
const addMaxFloatValidator = (validator, {
|
27
28
|
attr
|
28
29
|
}) => ___default.default.isNumber(attr.max) ? validator.max(attr.max) : validator;
|
29
30
|
const addStringRegexValidator = (validator, {
|
30
31
|
attr
|
31
|
-
}) => {
|
32
|
-
return "regex" in attr && !___default.default.isUndefined(attr.regex) ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required }) : validator;
|
32
|
+
}, { isDraft }) => {
|
33
|
+
return "regex" in attr && !___default.default.isUndefined(attr.regex) && !isDraft ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required }) : validator;
|
33
34
|
};
|
34
35
|
const addUniqueValidator = (validator, {
|
35
36
|
attr,
|
@@ -41,82 +42,147 @@ const addUniqueValidator = (validator, {
|
|
41
42
|
if (attr.type !== "uid" && !attr.unique) {
|
42
43
|
return validator;
|
43
44
|
}
|
45
|
+
const validateUniqueFieldWithinComponent = async (value) => {
|
46
|
+
if (!componentContext) {
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
const hasRepeatableData = componentContext.repeatableData.length > 0;
|
50
|
+
if (hasRepeatableData) {
|
51
|
+
const { name: updatedName, value: updatedValue } = updatedAttribute;
|
52
|
+
const pathToCheck = [...componentContext.pathToComponent.slice(1), updatedName].join(".");
|
53
|
+
const values = componentContext.repeatableData.map((item) => {
|
54
|
+
return pathToCheck.split(".").reduce((acc, key) => acc[key], item);
|
55
|
+
});
|
56
|
+
const isUpdatedAttributeRepeatedInThisEntity = values.filter((value2) => value2 === updatedValue).length > 1;
|
57
|
+
if (isUpdatedAttributeRepeatedInThisEntity) {
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
const {
|
62
|
+
model: parentModel,
|
63
|
+
options: parentOptions,
|
64
|
+
id: excludeId
|
65
|
+
} = componentContext.parentContent;
|
66
|
+
const whereConditions = {};
|
67
|
+
const isParentDraft = parentOptions && parentOptions.isDraft;
|
68
|
+
whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };
|
69
|
+
if (parentOptions?.locale) {
|
70
|
+
whereConditions.locale = parentOptions.locale;
|
71
|
+
}
|
72
|
+
if (excludeId && !Number.isNaN(excludeId)) {
|
73
|
+
whereConditions.id = { $ne: excludeId };
|
74
|
+
}
|
75
|
+
const queryUid = parentModel.uid;
|
76
|
+
const queryWhere = {
|
77
|
+
...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {
|
78
|
+
[updatedAttribute.name]: value
|
79
|
+
}),
|
80
|
+
...whereConditions
|
81
|
+
};
|
82
|
+
return !await strapi.db.query(queryUid).findOne({ where: queryWhere });
|
83
|
+
};
|
84
|
+
const validateUniqueFieldWithinDynamicZoneComponent = async (startOfPath) => {
|
85
|
+
if (!componentContext) {
|
86
|
+
return false;
|
87
|
+
}
|
88
|
+
const targetComponentUID = model.uid;
|
89
|
+
const countOfValueInThisEntity = (componentContext?.fullDynamicZoneContent ?? []).reduce(
|
90
|
+
(acc, component) => {
|
91
|
+
if (component.__component !== targetComponentUID) {
|
92
|
+
return acc;
|
93
|
+
}
|
94
|
+
const updatedValue = component[updatedAttribute.name];
|
95
|
+
return updatedValue === updatedAttribute.value ? acc + 1 : acc;
|
96
|
+
},
|
97
|
+
0
|
98
|
+
);
|
99
|
+
if (countOfValueInThisEntity > 1) {
|
100
|
+
return false;
|
101
|
+
}
|
102
|
+
const query = {
|
103
|
+
select: ["id"],
|
104
|
+
where: {},
|
105
|
+
populate: {
|
106
|
+
[startOfPath]: {
|
107
|
+
on: {
|
108
|
+
[targetComponentUID]: {
|
109
|
+
select: ["id"],
|
110
|
+
where: { [updatedAttribute.name]: updatedAttribute.value }
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}
|
115
|
+
};
|
116
|
+
const { options: options2, id } = componentContext.parentContent;
|
117
|
+
if (options2?.isDraft !== void 0) {
|
118
|
+
query.where.published_at = options2.isDraft ? { $eq: null } : { $ne: null };
|
119
|
+
}
|
120
|
+
if (id) {
|
121
|
+
query.where.id = { $ne: id };
|
122
|
+
}
|
123
|
+
if (options2?.locale) {
|
124
|
+
query.where.locale = options2.locale;
|
125
|
+
}
|
126
|
+
const parentModelQueryResult = await strapi.db.query(componentContext.parentContent.model.uid).findMany(query);
|
127
|
+
const filteredResults = parentModelQueryResult.filter((result) => Array.isArray(result[startOfPath]) && result[startOfPath].length).flatMap((result) => result[startOfPath]).filter((dynamicZoneComponent) => dynamicZoneComponent.__component === targetComponentUID);
|
128
|
+
if (filteredResults.length >= 1) {
|
129
|
+
return false;
|
130
|
+
}
|
131
|
+
return true;
|
132
|
+
};
|
44
133
|
return validator.test("unique", "This attribute must be unique", async (value) => {
|
45
|
-
|
46
|
-
if (___default.default.isNil(value)) {
|
134
|
+
if (___default.default.isNil(value) || value === "") {
|
47
135
|
return true;
|
48
136
|
}
|
49
|
-
if (
|
137
|
+
if (options.isDraft) {
|
50
138
|
return true;
|
51
139
|
}
|
52
|
-
|
53
|
-
let queryWhere = {};
|
54
|
-
const hasPathToComponent = componentContext?.pathToComponent?.length > 0;
|
140
|
+
const hasPathToComponent = componentContext && componentContext.pathToComponent.length > 0;
|
55
141
|
if (hasPathToComponent) {
|
56
|
-
const
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
const values = componentContext.repeatableData.map((item) => {
|
61
|
-
return pathToCheck.split(".").reduce((acc, key) => acc[key], item);
|
62
|
-
});
|
63
|
-
const isUpdatedAttributeRepeatedInThisEntity = values.filter((value2) => value2 === updatedValue).length > 1;
|
64
|
-
if (isUpdatedAttributeRepeatedInThisEntity) {
|
65
|
-
return false;
|
66
|
-
}
|
67
|
-
}
|
68
|
-
const {
|
69
|
-
model: parentModel,
|
70
|
-
options: parentOptions,
|
71
|
-
id: excludeId
|
72
|
-
} = componentContext.parentContent;
|
73
|
-
queryUid = parentModel.uid;
|
74
|
-
const whereConditions = {};
|
75
|
-
const isParentDraft = parentOptions && parentOptions.isDraft;
|
76
|
-
whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };
|
77
|
-
if (parentOptions?.locale) {
|
78
|
-
whereConditions.locale = parentOptions.locale;
|
79
|
-
}
|
80
|
-
if (excludeId && !Number.isNaN(excludeId)) {
|
81
|
-
whereConditions.id = { $ne: excludeId };
|
82
|
-
}
|
83
|
-
queryWhere = {
|
84
|
-
...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {
|
85
|
-
[updatedAttribute.name]: value
|
86
|
-
}),
|
87
|
-
...whereConditions
|
88
|
-
};
|
89
|
-
} else {
|
90
|
-
queryUid = model.uid;
|
91
|
-
const scalarAttributeWhere = {
|
92
|
-
[updatedAttribute.name]: value
|
93
|
-
};
|
94
|
-
scalarAttributeWhere.publishedAt = options.isDraft ? null : { $notNull: true };
|
95
|
-
if (options?.locale) {
|
96
|
-
scalarAttributeWhere.locale = options.locale;
|
97
|
-
}
|
98
|
-
if (entity?.id) {
|
99
|
-
scalarAttributeWhere.id = { $ne: entity.id };
|
142
|
+
const startOfPath = componentContext.pathToComponent[0];
|
143
|
+
const testingDZ = componentContext.parentContent.model.attributes[startOfPath].type === "dynamiczone";
|
144
|
+
if (testingDZ) {
|
145
|
+
return validateUniqueFieldWithinDynamicZoneComponent(startOfPath);
|
100
146
|
}
|
101
|
-
|
147
|
+
return validateUniqueFieldWithinComponent(value);
|
102
148
|
}
|
103
|
-
|
149
|
+
const scalarAttributeWhere = {
|
150
|
+
[updatedAttribute.name]: value,
|
151
|
+
publishedAt: { $notNull: true }
|
152
|
+
};
|
153
|
+
if (options?.locale) {
|
154
|
+
scalarAttributeWhere.locale = options.locale;
|
155
|
+
}
|
156
|
+
if (entity?.id) {
|
157
|
+
scalarAttributeWhere.id = { $ne: entity.id };
|
158
|
+
}
|
159
|
+
return !await strapi.db.query(model.uid).findOne({ where: scalarAttributeWhere, select: ["id"] });
|
104
160
|
});
|
105
161
|
};
|
106
162
|
const stringValidator = (metas, options) => {
|
107
163
|
let schema = strapiUtils.yup.string().transform((val, originalVal) => originalVal);
|
108
164
|
schema = addMinLengthValidator(schema, metas, options);
|
109
165
|
schema = addMaxLengthValidator(schema, metas);
|
110
|
-
schema = addStringRegexValidator(schema, metas);
|
166
|
+
schema = addStringRegexValidator(schema, metas, options);
|
111
167
|
schema = addUniqueValidator(schema, metas, options);
|
112
168
|
return schema;
|
113
169
|
};
|
114
170
|
const emailValidator = (metas, options) => {
|
115
171
|
const schema = stringValidator(metas, options);
|
116
|
-
|
172
|
+
if (options.isDraft) {
|
173
|
+
return schema;
|
174
|
+
}
|
175
|
+
return schema.email().min(
|
176
|
+
1,
|
177
|
+
// eslint-disable-next-line no-template-curly-in-string
|
178
|
+
"${path} cannot be empty"
|
179
|
+
);
|
117
180
|
};
|
118
181
|
const uidValidator = (metas, options) => {
|
119
182
|
const schema = stringValidator(metas, options);
|
183
|
+
if (options.isDraft) {
|
184
|
+
return schema;
|
185
|
+
}
|
120
186
|
return schema.matches(/^[A-Za-z0-9-_.~]*$/);
|
121
187
|
};
|
122
188
|
const enumerationValidator = ({ attr }) => {
|
@@ -124,14 +190,14 @@ const enumerationValidator = ({ attr }) => {
|
|
124
190
|
};
|
125
191
|
const integerValidator = (metas, options) => {
|
126
192
|
let schema = strapiUtils.yup.number().integer();
|
127
|
-
schema = addMinIntegerValidator(schema, metas);
|
193
|
+
schema = addMinIntegerValidator(schema, metas, options);
|
128
194
|
schema = addMaxIntegerValidator(schema, metas);
|
129
195
|
schema = addUniqueValidator(schema, metas, options);
|
130
196
|
return schema;
|
131
197
|
};
|
132
198
|
const floatValidator = (metas, options) => {
|
133
199
|
let schema = strapiUtils.yup.number();
|
134
|
-
schema = addMinFloatValidator(schema, metas);
|
200
|
+
schema = addMinFloatValidator(schema, metas, options);
|
135
201
|
schema = addMaxFloatValidator(schema, metas);
|
136
202
|
schema = addUniqueValidator(schema, metas, options);
|
137
203
|
return schema;
|
@@ -144,7 +210,7 @@ const datesValidator = (metas, options) => {
|
|
144
210
|
const schema = strapiUtils.yup.mixed();
|
145
211
|
return addUniqueValidator(schema, metas, options);
|
146
212
|
};
|
147
|
-
const
|
213
|
+
const Validators = {
|
148
214
|
string: stringValidator,
|
149
215
|
text: stringValidator,
|
150
216
|
richtext: stringValidator,
|
@@ -162,7 +228,14 @@ const validators = {
|
|
162
228
|
time: datesValidator,
|
163
229
|
datetime: datesValidator,
|
164
230
|
timestamp: datesValidator,
|
165
|
-
blocks: blocksValidator
|
231
|
+
blocks: blocksValidator.blocksValidator
|
166
232
|
};
|
167
|
-
|
233
|
+
exports.Validators = Validators;
|
234
|
+
exports.bigintegerValidator = bigintegerValidator;
|
235
|
+
exports.datesValidator = datesValidator;
|
236
|
+
exports.emailValidator = emailValidator;
|
237
|
+
exports.enumerationValidator = enumerationValidator;
|
238
|
+
exports.floatValidator = floatValidator;
|
239
|
+
exports.integerValidator = integerValidator;
|
240
|
+
exports.uidValidator = uidValidator;
|
168
241
|
//# sourceMappingURL=validators.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"validators.js","sources":["../../../src/services/entity-validator/validators.ts"],"sourcesContent":["import _ from 'lodash';\nimport { yup } from '@strapi/utils';\nimport type { Schema, Struct, Modules } from '@strapi/types';\nimport blocksValidator from './blocks-validator';\n\nimport type { ComponentContext } from '.';\n\ninterface ValidatorMetas<TAttribute extends Schema.Attribute.AnyAttribute> {\n attr: TAttribute;\n model: Struct.ContentTypeSchema;\n updatedAttribute: { name: string; value: unknown };\n entity: Modules.EntityValidator.Entity;\n componentContext: ComponentContext;\n}\n\ninterface ValidatorOptions {\n isDraft: boolean;\n locale?: string;\n}\n\n/* Validator utils */\n\n/**\n * Adds minLength validator\n */\nconst addMinLengthValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n },\n { isDraft }: ValidatorOptions\n) => {\n return attr.minLength && _.isInteger(attr.minLength) && !isDraft\n ? validator.min(attr.minLength)\n : validator;\n};\n\n/**\n * Adds maxLength validator\n * @returns {StringSchema}\n */\nconst addMaxLengthValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n }\n) => {\n return attr.maxLength && _.isInteger(attr.maxLength) ? validator.max(attr.maxLength) : validator;\n};\n\n/**\n * Adds min integer validator\n * @returns {NumberSchema}\n */\nconst addMinIntegerValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Integer | Schema.Attribute.BigInteger;\n }\n) => (_.isNumber(attr.min) ? validator.min(_.toInteger(attr.min)) : validator);\n\n/**\n * Adds max integer validator\n */\nconst addMaxIntegerValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Integer | Schema.Attribute.BigInteger;\n }\n) => (_.isNumber(attr.max) ? validator.max(_.toInteger(attr.max)) : validator);\n\n/**\n * Adds min float/decimal validator\n */\nconst addMinFloatValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Decimal | Schema.Attribute.Float;\n }\n) => (_.isNumber(attr.min) ? validator.min(attr.min) : validator);\n\n/**\n * Adds max float/decimal validator\n */\nconst addMaxFloatValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Decimal | Schema.Attribute.Float;\n }\n) => (_.isNumber(attr.max) ? validator.max(attr.max) : validator);\n\n/**\n * Adds regex validator\n */\nconst addStringRegexValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n }\n) => {\n return 'regex' in attr && !_.isUndefined(attr.regex)\n ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required })\n : validator;\n};\n\n/**\n * Adds unique validator\n */\nconst addUniqueValidator = <T extends yup.AnySchema>(\n validator: T,\n {\n attr,\n model,\n updatedAttribute,\n entity,\n componentContext,\n }: ValidatorMetas<Schema.Attribute.AnyAttribute & Schema.Attribute.UniqueOption>,\n options: ValidatorOptions\n): T => {\n if (attr.type !== 'uid' && !attr.unique) {\n return validator;\n }\n\n return validator.test('unique', 'This attribute must be unique', async (value) => {\n const isPublish = options.isDraft === false;\n\n /**\n * If the attribute value is `null` we want to skip the unique validation.\n * Otherwise it'll only accept a single `null` entry in the database.\n */\n if (_.isNil(value)) {\n return true;\n }\n\n /**\n * If we are updating a draft and the value is unchanged we skip the unique verification. This will\n * prevent the validator to be triggered in case the user activated the\n * unique constraint after already creating multiple entries with\n * the same attribute value for that field.\n */\n if (!isPublish && value === entity?.[updatedAttribute.name]) {\n return true;\n }\n\n let queryUid: string;\n let queryWhere: Record<string, any> = {};\n\n const hasPathToComponent = componentContext?.pathToComponent?.length > 0;\n if (hasPathToComponent) {\n const hasRepeatableData = componentContext.repeatableData.length > 0;\n if (hasRepeatableData) {\n // If we are validating a unique field within a repeatable component,\n // we first need to ensure that the repeatable in the current entity is\n // valid against itself.\n\n const { name: updatedName, value: updatedValue } = updatedAttribute;\n // Construct the full path to the unique field within the component.\n const pathToCheck = [...componentContext.pathToComponent.slice(1), updatedName].join('.');\n\n // Extract the values from the repeatable data using the constructed path\n const values = componentContext.repeatableData.map((item) => {\n return pathToCheck.split('.').reduce((acc, key) => acc[key], item as any);\n });\n\n // Check if the value is repeated in the current entity\n const isUpdatedAttributeRepeatedInThisEntity =\n values.filter((value) => value === updatedValue).length > 1;\n\n if (isUpdatedAttributeRepeatedInThisEntity) {\n return false;\n }\n }\n\n /**\n * When `componentContext` is present it means we are dealing with a unique\n * field within a component.\n *\n * The unique validation must consider the specific context of the\n * component, which will always be contained within a parent content type\n * and may also be nested within another component.\n *\n * We construct a query that takes into account the parent's model UID,\n * dimensions (such as draft and publish state/locale) and excludes the current\n * content type entity by its ID if provided.\n */\n const {\n model: parentModel,\n options: parentOptions,\n id: excludeId,\n } = componentContext.parentContent;\n queryUid = parentModel.uid;\n\n const whereConditions: Record<string, any> = {};\n const isParentDraft = parentOptions && parentOptions.isDraft;\n\n whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };\n\n if (parentOptions?.locale) {\n whereConditions.locale = parentOptions.locale;\n }\n\n if (excludeId && !Number.isNaN(excludeId)) {\n whereConditions.id = { $ne: excludeId };\n }\n\n queryWhere = {\n ...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {\n [updatedAttribute.name]: value,\n }),\n\n ...whereConditions,\n };\n } else {\n /**\n * Here we are validating a scalar unique field from the content type's schema.\n * We construct a query to check if the value is unique\n * considering dimensions (e.g. locale, publication state) and excluding the current entity by its ID if available.\n */\n queryUid = model.uid;\n const scalarAttributeWhere: Record<string, any> = {\n [updatedAttribute.name]: value,\n };\n\n scalarAttributeWhere.publishedAt = options.isDraft ? null : { $notNull: true };\n\n if (options?.locale) {\n scalarAttributeWhere.locale = options.locale;\n }\n\n if (entity?.id) {\n scalarAttributeWhere.id = { $ne: entity.id };\n }\n\n queryWhere = scalarAttributeWhere;\n }\n\n // The validation should pass if there is no other record found from the query\n // TODO query not working for dynamic zones (type === relation)\n return !(await strapi.db.query(queryUid).findOne({ where: queryWhere }));\n });\n};\n\n/* Type validators */\n\nconst stringValidator = (\n metas: ValidatorMetas<\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID\n >,\n options: ValidatorOptions\n) => {\n let schema = yup.string().transform((val, originalVal) => originalVal);\n\n schema = addMinLengthValidator(schema, metas, options);\n schema = addMaxLengthValidator(schema, metas);\n schema = addStringRegexValidator(schema, metas);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nconst emailValidator = (\n metas: ValidatorMetas<Schema.Attribute.Email>,\n options: ValidatorOptions\n) => {\n const schema = stringValidator(metas, options);\n return schema.email().min(1, '${path} cannot be empty');\n};\n\nconst uidValidator = (metas: ValidatorMetas<Schema.Attribute.UID>, options: ValidatorOptions) => {\n const schema = stringValidator(metas, options);\n\n return schema.matches(/^[A-Za-z0-9-_.~]*$/);\n};\n\nconst enumerationValidator = ({ attr }: { attr: Schema.Attribute.Enumeration }) => {\n return yup\n .string()\n .oneOf((Array.isArray(attr.enum) ? attr.enum : [attr.enum]).concat(null as any));\n};\n\nconst integerValidator = (\n metas: ValidatorMetas<Schema.Attribute.Integer | Schema.Attribute.BigInteger>,\n options: ValidatorOptions\n) => {\n let schema = yup.number().integer();\n\n schema = addMinIntegerValidator(schema, metas);\n schema = addMaxIntegerValidator(schema, metas);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nconst floatValidator = (\n metas: ValidatorMetas<Schema.Attribute.Decimal | Schema.Attribute.Float>,\n options: ValidatorOptions\n) => {\n let schema = yup.number();\n schema = addMinFloatValidator(schema, metas);\n schema = addMaxFloatValidator(schema, metas);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nconst bigintegerValidator = (\n metas: ValidatorMetas<Schema.Attribute.BigInteger>,\n options: ValidatorOptions\n) => {\n const schema = yup.mixed();\n return addUniqueValidator(schema, metas, options);\n};\n\nconst datesValidator = (\n metas: ValidatorMetas<\n | Schema.Attribute.Date\n | Schema.Attribute.DateTime\n | Schema.Attribute.Time\n | Schema.Attribute.Timestamp\n >,\n options: ValidatorOptions\n) => {\n const schema = yup.mixed();\n return addUniqueValidator(schema, metas, options);\n};\n\nexport default {\n string: stringValidator,\n text: stringValidator,\n richtext: stringValidator,\n password: stringValidator,\n email: emailValidator,\n enumeration: enumerationValidator,\n boolean: () => yup.boolean(),\n uid: uidValidator,\n json: () => yup.mixed(),\n integer: integerValidator,\n biginteger: bigintegerValidator,\n float: floatValidator,\n decimal: floatValidator,\n date: datesValidator,\n time: datesValidator,\n datetime: datesValidator,\n timestamp: datesValidator,\n blocks: blocksValidator,\n};\n"],"names":["_","value","yup"],"mappings":";;;;;;AAyBA,MAAM,wBAAwB,CAC5B,WACA;AAAA,EACE;AACF,GASA,EAAE,cACC;AACH,SAAO,KAAK,aAAaA,WAAE,QAAA,UAAU,KAAK,SAAS,KAAK,CAAC,UACrD,UAAU,IAAI,KAAK,SAAS,IAC5B;AACN;AAMA,MAAM,wBAAwB,CAC5B,WACA;AAAA,EACE;AACF,MASG;AACI,SAAA,KAAK,aAAaA,mBAAE,UAAU,KAAK,SAAS,IAAI,UAAU,IAAI,KAAK,SAAS,IAAI;AACzF;AAMA,MAAM,yBAAyB,CAC7B,WACA;AAAA,EACE;AACF,MAGIA,WAAE,QAAA,SAAS,KAAK,GAAG,IAAI,UAAU,IAAIA,WAAAA,QAAE,UAAU,KAAK,GAAG,CAAC,IAAI;AAKpE,MAAM,yBAAyB,CAC7B,WACA;AAAA,EACE;AACF,MAG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mBAAmB,QAAQ,OAAO,OAAO;AAClD;AAEA,MAAM,iBAAiB,CACrB,OAMA,YACG;AACG,QAAA,SAASA,gBAAI;AACZ,SAAA,mBAAmB,QAAQ,OAAO,OAAO;AAClD;AAEA,MAAe,aAAA;AAAA,EACb,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,UAAU;AAAA,EACV,UAAU;AAAA,EACV,OAAO;AAAA,EACP,aAAa;AAAA,EACb,SAAS,MAAMA,YAAA,IAAI,QAAQ;AAAA,EAC3B,KAAK;AAAA,EACL,MAAM,MAAMA,YAAA,IAAI,MAAM;AAAA,EACtB,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AAAA,EACN,UAAU;AAAA,EACV,WAAW;AAAA,EACX,QAAQ;AACV;;"}
|
1
|
+
{"version":3,"file":"validators.js","sources":["../../../src/services/entity-validator/validators.ts"],"sourcesContent":["/**\n * Validators check if the entry data meets specific criteria before saving or publishing.\n * (e.g., length, range, format).\n *\n * Drafts have limited validations (mainly max constraints),\n * while published content undergoes full validation.\n *\n * The system also takes locales into account when validating data.\n * E.g, unique fields must be unique within the same locale.\n */\nimport _ from 'lodash';\nimport { yup } from '@strapi/utils';\nimport type { Schema, Struct, Modules } from '@strapi/types';\nimport { blocksValidator } from './blocks-validator';\n\nimport type { ComponentContext } from '.';\n\nexport interface ValidatorMetas<\n TAttribute extends Schema.Attribute.AnyAttribute = Schema.Attribute.AnyAttribute,\n TValue extends Schema.Attribute.Value<TAttribute> = Schema.Attribute.Value<TAttribute>,\n> {\n attr: TAttribute;\n model: Struct.Schema;\n updatedAttribute: {\n name: string;\n value: TValue;\n };\n componentContext?: ComponentContext;\n entity?: Modules.EntityValidator.Entity;\n}\n\ninterface ValidatorOptions {\n isDraft: boolean;\n locale?: string;\n}\n\n/* Validator utils */\n\n/**\n * Adds minLength validator\n */\nconst addMinLengthValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n },\n { isDraft }: ValidatorOptions\n) => {\n return attr.minLength && _.isInteger(attr.minLength) && !isDraft\n ? validator.min(attr.minLength)\n : validator;\n};\n\n/**\n * Adds maxLength validator\n * @returns {StringSchema}\n */\nconst addMaxLengthValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n }\n) => {\n return attr.maxLength && _.isInteger(attr.maxLength) ? validator.max(attr.maxLength) : validator;\n};\n\n/**\n * Adds min integer validator\n * @returns {NumberSchema}\n */\nconst addMinIntegerValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Integer | Schema.Attribute.BigInteger;\n },\n { isDraft }: ValidatorOptions\n) => (_.isNumber(attr.min) && !isDraft ? validator.min(_.toInteger(attr.min)) : validator);\n\n/**\n * Adds max integer validator\n */\nconst addMaxIntegerValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Integer | Schema.Attribute.BigInteger;\n }\n) => (_.isNumber(attr.max) ? validator.max(_.toInteger(attr.max)) : validator);\n\n/**\n * Adds min float/decimal validator\n */\nconst addMinFloatValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Decimal | Schema.Attribute.Float;\n },\n { isDraft }: ValidatorOptions\n) => (_.isNumber(attr.min) && !isDraft ? validator.min(attr.min) : validator);\n\n/**\n * Adds max float/decimal validator\n */\nconst addMaxFloatValidator = (\n validator: yup.NumberSchema,\n {\n attr,\n }: {\n attr: Schema.Attribute.Decimal | Schema.Attribute.Float;\n }\n) => (_.isNumber(attr.max) ? validator.max(attr.max) : validator);\n\n/**\n * Adds regex validator\n */\nconst addStringRegexValidator = (\n validator: yup.StringSchema,\n {\n attr,\n }: {\n attr:\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID;\n },\n { isDraft }: ValidatorOptions\n) => {\n return 'regex' in attr && !_.isUndefined(attr.regex) && !isDraft\n ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required })\n : validator;\n};\n\nconst addUniqueValidator = <T extends yup.AnySchema>(\n validator: T,\n {\n attr,\n model,\n updatedAttribute,\n entity,\n componentContext,\n }: ValidatorMetas<Schema.Attribute.AnyAttribute & Schema.Attribute.UniqueOption>,\n options: ValidatorOptions\n): T => {\n if (attr.type !== 'uid' && !attr.unique) {\n return validator;\n }\n\n const validateUniqueFieldWithinComponent = async (value: any): Promise<boolean> => {\n if (!componentContext) {\n return false;\n }\n\n // If we are validating a unique field within a repeatable component,\n // we first need to ensure that the repeatable in the current entity is\n // valid against itself.\n const hasRepeatableData = componentContext.repeatableData.length > 0;\n if (hasRepeatableData) {\n const { name: updatedName, value: updatedValue } = updatedAttribute;\n // Construct the full path to the unique field within the component.\n const pathToCheck = [...componentContext.pathToComponent.slice(1), updatedName].join('.');\n\n // Extract the values from the repeatable data using the constructed path\n const values = componentContext.repeatableData.map((item) => {\n return pathToCheck.split('.').reduce((acc, key) => acc[key], item as any);\n });\n\n // Check if the value is repeated in the current entity\n const isUpdatedAttributeRepeatedInThisEntity =\n values.filter((value) => value === updatedValue).length > 1;\n\n if (isUpdatedAttributeRepeatedInThisEntity) {\n return false;\n }\n }\n\n /**\n * When `componentContext` is present it means we are dealing with a unique\n * field within a component.\n *\n * The unique validation must consider the specific context of the\n * component, which will always be contained within a parent content type\n * and may also be nested within another component.\n *\n * We construct a query that takes into account the parent's model UID,\n * dimensions (such as draft and publish state/locale) and excludes the current\n * content type entity by its ID if provided.\n */\n const {\n model: parentModel,\n options: parentOptions,\n id: excludeId,\n } = componentContext.parentContent;\n\n const whereConditions: Record<string, any> = {};\n const isParentDraft = parentOptions && parentOptions.isDraft;\n\n whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };\n\n if (parentOptions?.locale) {\n whereConditions.locale = parentOptions.locale;\n }\n\n if (excludeId && !Number.isNaN(excludeId)) {\n whereConditions.id = { $ne: excludeId };\n }\n\n const queryUid = parentModel.uid;\n const queryWhere = {\n ...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {\n [updatedAttribute.name]: value,\n }),\n\n ...whereConditions,\n };\n\n // The validation should pass if there is no other record found from the query\n return !(await strapi.db.query(queryUid).findOne({ where: queryWhere }));\n };\n\n const validateUniqueFieldWithinDynamicZoneComponent = async (\n startOfPath: string\n ): Promise<boolean> => {\n if (!componentContext) {\n return false;\n }\n\n const targetComponentUID = model.uid;\n // Ensure that the value is unique within the dynamic zone in this entity.\n const countOfValueInThisEntity = (componentContext?.fullDynamicZoneContent ?? []).reduce(\n (acc, component) => {\n if (component.__component !== targetComponentUID) {\n return acc;\n }\n\n const updatedValue = component[updatedAttribute.name];\n return updatedValue === updatedAttribute.value ? acc + 1 : acc;\n },\n 0\n );\n\n if (countOfValueInThisEntity > 1) {\n // If the value is repeated in the current entity, the validation fails.\n return false;\n }\n\n // Build a query for the parent content type to find all entities in the\n // same locale and publication state\n type QueryType = {\n select: string[];\n where: {\n published_at?: { $eq: null } | { $ne: null };\n id?: { $ne: number };\n locale?: string;\n };\n populate: {\n [key: string]: {\n on: {\n [key: string]: {\n select: string[];\n where: { [key: string]: string | number | boolean };\n };\n };\n };\n };\n };\n\n // Populate the dynamic zone for any components that share the same value\n // as the updated attribute.\n const query: QueryType = {\n select: ['id'],\n where: {},\n populate: {\n [startOfPath]: {\n on: {\n [targetComponentUID]: {\n select: ['id'],\n where: { [updatedAttribute.name]: updatedAttribute.value },\n },\n },\n },\n },\n };\n\n const { options, id } = componentContext.parentContent;\n\n if (options?.isDraft !== undefined) {\n query.where.published_at = options.isDraft ? { $eq: null } : { $ne: null };\n }\n\n if (id) {\n query.where.id = { $ne: id };\n }\n\n if (options?.locale) {\n query.where.locale = options.locale;\n }\n\n const parentModelQueryResult = await strapi.db\n .query(componentContext.parentContent.model.uid)\n .findMany(query);\n\n // Filter the results to only include results that have components in the\n // dynamic zone that match the target component type.\n const filteredResults = parentModelQueryResult\n .filter((result) => Array.isArray(result[startOfPath]) && result[startOfPath].length)\n .flatMap((result) => result[startOfPath])\n .filter((dynamicZoneComponent) => dynamicZoneComponent.__component === targetComponentUID);\n\n if (filteredResults.length >= 1) {\n return false;\n }\n\n return true;\n };\n\n return validator.test('unique', 'This attribute must be unique', async (value) => {\n /**\n * If the attribute value is `null` or an empty string we want to skip the unique validation.\n * Otherwise it'll only accept a single entry with that value in the database.\n */\n if (_.isNil(value) || value === '') {\n return true;\n }\n\n /**\n * We don't validate any unique constraint for draft entries.\n */\n if (options.isDraft) {\n return true;\n }\n\n const hasPathToComponent = componentContext && componentContext.pathToComponent.length > 0;\n if (hasPathToComponent) {\n // Detect if we are validating within a dynamiczone by checking if the first\n // path is a dynamiczone attribute in the parent content type.\n const startOfPath = componentContext.pathToComponent[0];\n const testingDZ =\n componentContext.parentContent.model.attributes[startOfPath].type === 'dynamiczone';\n\n if (testingDZ) {\n return validateUniqueFieldWithinDynamicZoneComponent(startOfPath);\n }\n\n return validateUniqueFieldWithinComponent(value);\n }\n\n /**\n * Here we are validating a scalar unique field from the content type's schema.\n * We construct a query to check if the value is unique\n * considering dimensions (e.g. locale, publication state) and excluding the current entity by its ID if available.\n */\n const scalarAttributeWhere: Record<string, any> = {\n [updatedAttribute.name]: value,\n publishedAt: { $notNull: true },\n };\n\n if (options?.locale) {\n scalarAttributeWhere.locale = options.locale;\n }\n\n if (entity?.id) {\n scalarAttributeWhere.id = { $ne: entity.id };\n }\n\n // The validation should pass if there is no other record found from the query\n return !(await strapi.db\n .query(model.uid)\n .findOne({ where: scalarAttributeWhere, select: ['id'] }));\n });\n};\n\n/* Type validators */\n\nconst stringValidator = (\n metas: ValidatorMetas<\n | Schema.Attribute.String\n | Schema.Attribute.Text\n | Schema.Attribute.RichText\n | Schema.Attribute.Password\n | Schema.Attribute.Email\n | Schema.Attribute.UID\n >,\n options: ValidatorOptions\n) => {\n let schema = yup.string().transform((val, originalVal) => originalVal);\n\n schema = addMinLengthValidator(schema, metas, options);\n schema = addMaxLengthValidator(schema, metas);\n schema = addStringRegexValidator(schema, metas, options);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nexport const emailValidator = (\n metas: ValidatorMetas<Schema.Attribute.Email>,\n options: ValidatorOptions\n) => {\n const schema = stringValidator(metas, options);\n\n if (options.isDraft) {\n return schema;\n }\n\n return schema.email().min(\n 1,\n // eslint-disable-next-line no-template-curly-in-string\n '${path} cannot be empty'\n );\n};\n\nexport const uidValidator = (\n metas: ValidatorMetas<Schema.Attribute.UID>,\n options: ValidatorOptions\n) => {\n const schema = stringValidator(metas, options);\n\n if (options.isDraft) {\n return schema;\n }\n\n return schema.matches(/^[A-Za-z0-9-_.~]*$/);\n};\n\nexport const enumerationValidator = ({ attr }: { attr: Schema.Attribute.Enumeration }) => {\n return yup\n .string()\n .oneOf((Array.isArray(attr.enum) ? attr.enum : [attr.enum]).concat(null as any));\n};\n\nexport const integerValidator = (\n metas: ValidatorMetas<Schema.Attribute.Integer | Schema.Attribute.BigInteger>,\n options: ValidatorOptions\n) => {\n let schema = yup.number().integer();\n\n schema = addMinIntegerValidator(schema, metas, options);\n schema = addMaxIntegerValidator(schema, metas);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nexport const floatValidator = (\n metas: ValidatorMetas<Schema.Attribute.Decimal | Schema.Attribute.Float>,\n options: ValidatorOptions\n) => {\n let schema = yup.number();\n\n schema = addMinFloatValidator(schema, metas, options);\n schema = addMaxFloatValidator(schema, metas);\n schema = addUniqueValidator(schema, metas, options);\n\n return schema;\n};\n\nexport const bigintegerValidator = (\n metas: ValidatorMetas<Schema.Attribute.BigInteger>,\n options: ValidatorOptions\n) => {\n const schema = yup.mixed();\n return addUniqueValidator(schema, metas, options);\n};\n\nexport const datesValidator = (\n metas: ValidatorMetas<\n | Schema.Attribute.Date\n | Schema.Attribute.DateTime\n | Schema.Attribute.Time\n | Schema.Attribute.Timestamp\n >,\n options: ValidatorOptions\n) => {\n const schema = yup.mixed();\n return addUniqueValidator(schema, metas, options);\n};\n\nexport const Validators = {\n string: stringValidator,\n text: stringValidator,\n richtext: stringValidator,\n password: stringValidator,\n email: emailValidator,\n enumeration: enumerationValidator,\n boolean: () => yup.boolean(),\n uid: uidValidator,\n json: () => yup.mixed(),\n integer: integerValidator,\n biginteger: bigintegerValidator,\n float: floatValidator,\n decimal: floatValidator,\n date: datesValidator,\n time: datesValidator,\n datetime: datesValidator,\n timestamp: datesValidator,\n blocks: blocksValidator,\n};\n"],"names":["_","value","options","yup","blocksValidator"],"mappings":";;;;;;;AAyCA,MAAM,wBAAwB,CAC5B,WACA;AAAA,EACE;AACF,GASA,EAAE,cACC;AACH,SAAO,KAAK,aAAaA,WAAE,QAAA,UAAU,KAAK,SAAS,KAAK,CAAC,UACrD,UAAU,IAAI,KAAK,SAAS,IAC5B;AACN;AAMA,MAAM,wBAAwB,CAC5B,WACA;AAAA,EACE;AACF,MASG;AACI,SAAA,KAAK,aAAaA,mBAAE,UAAU,KAAK,SAAS,IAAI,UAAU,IAAI,KAAK,SAAS,IAAI;AACzF;AAMA,MAAM,yBAAyB,CAC7B,WACA;AAAA,EACE;AACF,GAGA,EAAE,QAAQ,MACNA,WAAE,QAAA,SAAS,KAAK,GAAG,KAAK,CAAC,UAAU,UAAU,IAAIA,WAAAA,QAAE,UAAU,KAAK,GAAG,CAAC,IAAI;AAKhF,MAAM,yBAAyB,CAC7B,WACA;AAAA,EACE;AACF,MAGIA,WAAE,QAAA,SAAS,KAAK,GAAG,IAAI,UAAU,IAAIA,WAAAA,QAAE,UAAU,KAAK,GAAG,CAAC,IAAI;AAKpE,MAAM,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,KAAK,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,IAAW,CAAC;AACnF;AAEa,MAAA,mBAAmB,CAC9B,OACA,YACG;AACH,MAAI,SAASA,YAAA,IAAI,OAAO,EAAE,QAAQ;AAEzB,WAAA,uBAAuB,QAAQ,OAAO,OAAO;AAC7C,WAAA,uBAAuB,QAAQ,KAAK;AACpC,WAAA,mBAAmB,QAAQ,OAAO,OAAO;AAE3C,SAAA;AACT;AAEa,MAAA,iBAAiB,CAC5B,OACA,YACG;AACC,MAAA,SAASA,gBAAI;AAER,WAAA,qBAAqB,QAAQ,OAAO,OAAO;AAC3C,WAAA,qBAAqB,QAAQ,KAAK;AAClC,WAAA,mBAAmB,QAAQ,OAAO,OAAO;AAE3C,SAAA;AACT;AAEa,MAAA,sBAAsB,CACjC,OACA,YACG;AACG,QAAA,SAASA,gBAAI;AACZ,SAAA,mBAAmB,QAAQ,OAAO,OAAO;AAClD;AAEa,MAAA,iBAAiB,CAC5B,OAMA,YACG;AACG,QAAA,SAASA,gBAAI;AACZ,SAAA,mBAAmB,QAAQ,OAAO,OAAO;AAClD;AAEO,MAAM,aAAa;AAAA,EACxB,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,UAAU;AAAA,EACV,UAAU;AAAA,EACV,OAAO;AAAA,EACP,aAAa;AAAA,EACb,SAAS,MAAMA,YAAA,IAAI,QAAQ;AAAA,EAC3B,KAAK;AAAA,EACL,MAAM,MAAMA,YAAA,IAAI,MAAM;AAAA,EACtB,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AAAA,EACN,UAAU;AAAA,EACV,WAAW;AAAA,EACX,QAAQC,gBAAA;AACV;;;;;;;;;"}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import _ from "lodash";
|
2
2
|
import { yup } from "@strapi/utils";
|
3
|
-
import blocksValidator from "./blocks-validator.mjs";
|
3
|
+
import { blocksValidator } from "./blocks-validator.mjs";
|
4
4
|
const addMinLengthValidator = (validator, {
|
5
5
|
attr
|
6
6
|
}, { isDraft }) => {
|
@@ -13,20 +13,20 @@ const addMaxLengthValidator = (validator, {
|
|
13
13
|
};
|
14
14
|
const addMinIntegerValidator = (validator, {
|
15
15
|
attr
|
16
|
-
}) => _.isNumber(attr.min) ? validator.min(_.toInteger(attr.min)) : validator;
|
16
|
+
}, { isDraft }) => _.isNumber(attr.min) && !isDraft ? validator.min(_.toInteger(attr.min)) : validator;
|
17
17
|
const addMaxIntegerValidator = (validator, {
|
18
18
|
attr
|
19
19
|
}) => _.isNumber(attr.max) ? validator.max(_.toInteger(attr.max)) : validator;
|
20
20
|
const addMinFloatValidator = (validator, {
|
21
21
|
attr
|
22
|
-
}) => _.isNumber(attr.min) ? validator.min(attr.min) : validator;
|
22
|
+
}, { isDraft }) => _.isNumber(attr.min) && !isDraft ? validator.min(attr.min) : validator;
|
23
23
|
const addMaxFloatValidator = (validator, {
|
24
24
|
attr
|
25
25
|
}) => _.isNumber(attr.max) ? validator.max(attr.max) : validator;
|
26
26
|
const addStringRegexValidator = (validator, {
|
27
27
|
attr
|
28
|
-
}) => {
|
29
|
-
return "regex" in attr && !_.isUndefined(attr.regex) ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required }) : validator;
|
28
|
+
}, { isDraft }) => {
|
29
|
+
return "regex" in attr && !_.isUndefined(attr.regex) && !isDraft ? validator.matches(new RegExp(attr.regex), { excludeEmptyString: !attr.required }) : validator;
|
30
30
|
};
|
31
31
|
const addUniqueValidator = (validator, {
|
32
32
|
attr,
|
@@ -38,82 +38,147 @@ const addUniqueValidator = (validator, {
|
|
38
38
|
if (attr.type !== "uid" && !attr.unique) {
|
39
39
|
return validator;
|
40
40
|
}
|
41
|
+
const validateUniqueFieldWithinComponent = async (value) => {
|
42
|
+
if (!componentContext) {
|
43
|
+
return false;
|
44
|
+
}
|
45
|
+
const hasRepeatableData = componentContext.repeatableData.length > 0;
|
46
|
+
if (hasRepeatableData) {
|
47
|
+
const { name: updatedName, value: updatedValue } = updatedAttribute;
|
48
|
+
const pathToCheck = [...componentContext.pathToComponent.slice(1), updatedName].join(".");
|
49
|
+
const values = componentContext.repeatableData.map((item) => {
|
50
|
+
return pathToCheck.split(".").reduce((acc, key) => acc[key], item);
|
51
|
+
});
|
52
|
+
const isUpdatedAttributeRepeatedInThisEntity = values.filter((value2) => value2 === updatedValue).length > 1;
|
53
|
+
if (isUpdatedAttributeRepeatedInThisEntity) {
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
const {
|
58
|
+
model: parentModel,
|
59
|
+
options: parentOptions,
|
60
|
+
id: excludeId
|
61
|
+
} = componentContext.parentContent;
|
62
|
+
const whereConditions = {};
|
63
|
+
const isParentDraft = parentOptions && parentOptions.isDraft;
|
64
|
+
whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };
|
65
|
+
if (parentOptions?.locale) {
|
66
|
+
whereConditions.locale = parentOptions.locale;
|
67
|
+
}
|
68
|
+
if (excludeId && !Number.isNaN(excludeId)) {
|
69
|
+
whereConditions.id = { $ne: excludeId };
|
70
|
+
}
|
71
|
+
const queryUid = parentModel.uid;
|
72
|
+
const queryWhere = {
|
73
|
+
...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {
|
74
|
+
[updatedAttribute.name]: value
|
75
|
+
}),
|
76
|
+
...whereConditions
|
77
|
+
};
|
78
|
+
return !await strapi.db.query(queryUid).findOne({ where: queryWhere });
|
79
|
+
};
|
80
|
+
const validateUniqueFieldWithinDynamicZoneComponent = async (startOfPath) => {
|
81
|
+
if (!componentContext) {
|
82
|
+
return false;
|
83
|
+
}
|
84
|
+
const targetComponentUID = model.uid;
|
85
|
+
const countOfValueInThisEntity = (componentContext?.fullDynamicZoneContent ?? []).reduce(
|
86
|
+
(acc, component) => {
|
87
|
+
if (component.__component !== targetComponentUID) {
|
88
|
+
return acc;
|
89
|
+
}
|
90
|
+
const updatedValue = component[updatedAttribute.name];
|
91
|
+
return updatedValue === updatedAttribute.value ? acc + 1 : acc;
|
92
|
+
},
|
93
|
+
0
|
94
|
+
);
|
95
|
+
if (countOfValueInThisEntity > 1) {
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
const query = {
|
99
|
+
select: ["id"],
|
100
|
+
where: {},
|
101
|
+
populate: {
|
102
|
+
[startOfPath]: {
|
103
|
+
on: {
|
104
|
+
[targetComponentUID]: {
|
105
|
+
select: ["id"],
|
106
|
+
where: { [updatedAttribute.name]: updatedAttribute.value }
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
};
|
112
|
+
const { options: options2, id } = componentContext.parentContent;
|
113
|
+
if (options2?.isDraft !== void 0) {
|
114
|
+
query.where.published_at = options2.isDraft ? { $eq: null } : { $ne: null };
|
115
|
+
}
|
116
|
+
if (id) {
|
117
|
+
query.where.id = { $ne: id };
|
118
|
+
}
|
119
|
+
if (options2?.locale) {
|
120
|
+
query.where.locale = options2.locale;
|
121
|
+
}
|
122
|
+
const parentModelQueryResult = await strapi.db.query(componentContext.parentContent.model.uid).findMany(query);
|
123
|
+
const filteredResults = parentModelQueryResult.filter((result) => Array.isArray(result[startOfPath]) && result[startOfPath].length).flatMap((result) => result[startOfPath]).filter((dynamicZoneComponent) => dynamicZoneComponent.__component === targetComponentUID);
|
124
|
+
if (filteredResults.length >= 1) {
|
125
|
+
return false;
|
126
|
+
}
|
127
|
+
return true;
|
128
|
+
};
|
41
129
|
return validator.test("unique", "This attribute must be unique", async (value) => {
|
42
|
-
|
43
|
-
if (_.isNil(value)) {
|
130
|
+
if (_.isNil(value) || value === "") {
|
44
131
|
return true;
|
45
132
|
}
|
46
|
-
if (
|
133
|
+
if (options.isDraft) {
|
47
134
|
return true;
|
48
135
|
}
|
49
|
-
|
50
|
-
let queryWhere = {};
|
51
|
-
const hasPathToComponent = componentContext?.pathToComponent?.length > 0;
|
136
|
+
const hasPathToComponent = componentContext && componentContext.pathToComponent.length > 0;
|
52
137
|
if (hasPathToComponent) {
|
53
|
-
const
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
const values = componentContext.repeatableData.map((item) => {
|
58
|
-
return pathToCheck.split(".").reduce((acc, key) => acc[key], item);
|
59
|
-
});
|
60
|
-
const isUpdatedAttributeRepeatedInThisEntity = values.filter((value2) => value2 === updatedValue).length > 1;
|
61
|
-
if (isUpdatedAttributeRepeatedInThisEntity) {
|
62
|
-
return false;
|
63
|
-
}
|
64
|
-
}
|
65
|
-
const {
|
66
|
-
model: parentModel,
|
67
|
-
options: parentOptions,
|
68
|
-
id: excludeId
|
69
|
-
} = componentContext.parentContent;
|
70
|
-
queryUid = parentModel.uid;
|
71
|
-
const whereConditions = {};
|
72
|
-
const isParentDraft = parentOptions && parentOptions.isDraft;
|
73
|
-
whereConditions.publishedAt = isParentDraft ? null : { $notNull: true };
|
74
|
-
if (parentOptions?.locale) {
|
75
|
-
whereConditions.locale = parentOptions.locale;
|
76
|
-
}
|
77
|
-
if (excludeId && !Number.isNaN(excludeId)) {
|
78
|
-
whereConditions.id = { $ne: excludeId };
|
79
|
-
}
|
80
|
-
queryWhere = {
|
81
|
-
...componentContext.pathToComponent.reduceRight((acc, key) => ({ [key]: acc }), {
|
82
|
-
[updatedAttribute.name]: value
|
83
|
-
}),
|
84
|
-
...whereConditions
|
85
|
-
};
|
86
|
-
} else {
|
87
|
-
queryUid = model.uid;
|
88
|
-
const scalarAttributeWhere = {
|
89
|
-
[updatedAttribute.name]: value
|
90
|
-
};
|
91
|
-
scalarAttributeWhere.publishedAt = options.isDraft ? null : { $notNull: true };
|
92
|
-
if (options?.locale) {
|
93
|
-
scalarAttributeWhere.locale = options.locale;
|
94
|
-
}
|
95
|
-
if (entity?.id) {
|
96
|
-
scalarAttributeWhere.id = { $ne: entity.id };
|
138
|
+
const startOfPath = componentContext.pathToComponent[0];
|
139
|
+
const testingDZ = componentContext.parentContent.model.attributes[startOfPath].type === "dynamiczone";
|
140
|
+
if (testingDZ) {
|
141
|
+
return validateUniqueFieldWithinDynamicZoneComponent(startOfPath);
|
97
142
|
}
|
98
|
-
|
143
|
+
return validateUniqueFieldWithinComponent(value);
|
99
144
|
}
|
100
|
-
|
145
|
+
const scalarAttributeWhere = {
|
146
|
+
[updatedAttribute.name]: value,
|
147
|
+
publishedAt: { $notNull: true }
|
148
|
+
};
|
149
|
+
if (options?.locale) {
|
150
|
+
scalarAttributeWhere.locale = options.locale;
|
151
|
+
}
|
152
|
+
if (entity?.id) {
|
153
|
+
scalarAttributeWhere.id = { $ne: entity.id };
|
154
|
+
}
|
155
|
+
return !await strapi.db.query(model.uid).findOne({ where: scalarAttributeWhere, select: ["id"] });
|
101
156
|
});
|
102
157
|
};
|
103
158
|
const stringValidator = (metas, options) => {
|
104
159
|
let schema = yup.string().transform((val, originalVal) => originalVal);
|
105
160
|
schema = addMinLengthValidator(schema, metas, options);
|
106
161
|
schema = addMaxLengthValidator(schema, metas);
|
107
|
-
schema = addStringRegexValidator(schema, metas);
|
162
|
+
schema = addStringRegexValidator(schema, metas, options);
|
108
163
|
schema = addUniqueValidator(schema, metas, options);
|
109
164
|
return schema;
|
110
165
|
};
|
111
166
|
const emailValidator = (metas, options) => {
|
112
167
|
const schema = stringValidator(metas, options);
|
113
|
-
|
168
|
+
if (options.isDraft) {
|
169
|
+
return schema;
|
170
|
+
}
|
171
|
+
return schema.email().min(
|
172
|
+
1,
|
173
|
+
// eslint-disable-next-line no-template-curly-in-string
|
174
|
+
"${path} cannot be empty"
|
175
|
+
);
|
114
176
|
};
|
115
177
|
const uidValidator = (metas, options) => {
|
116
178
|
const schema = stringValidator(metas, options);
|
179
|
+
if (options.isDraft) {
|
180
|
+
return schema;
|
181
|
+
}
|
117
182
|
return schema.matches(/^[A-Za-z0-9-_.~]*$/);
|
118
183
|
};
|
119
184
|
const enumerationValidator = ({ attr }) => {
|
@@ -121,14 +186,14 @@ const enumerationValidator = ({ attr }) => {
|
|
121
186
|
};
|
122
187
|
const integerValidator = (metas, options) => {
|
123
188
|
let schema = yup.number().integer();
|
124
|
-
schema = addMinIntegerValidator(schema, metas);
|
189
|
+
schema = addMinIntegerValidator(schema, metas, options);
|
125
190
|
schema = addMaxIntegerValidator(schema, metas);
|
126
191
|
schema = addUniqueValidator(schema, metas, options);
|
127
192
|
return schema;
|
128
193
|
};
|
129
194
|
const floatValidator = (metas, options) => {
|
130
195
|
let schema = yup.number();
|
131
|
-
schema = addMinFloatValidator(schema, metas);
|
196
|
+
schema = addMinFloatValidator(schema, metas, options);
|
132
197
|
schema = addMaxFloatValidator(schema, metas);
|
133
198
|
schema = addUniqueValidator(schema, metas, options);
|
134
199
|
return schema;
|
@@ -141,7 +206,7 @@ const datesValidator = (metas, options) => {
|
|
141
206
|
const schema = yup.mixed();
|
142
207
|
return addUniqueValidator(schema, metas, options);
|
143
208
|
};
|
144
|
-
const
|
209
|
+
const Validators = {
|
145
210
|
string: stringValidator,
|
146
211
|
text: stringValidator,
|
147
212
|
richtext: stringValidator,
|
@@ -162,6 +227,13 @@ const validators = {
|
|
162
227
|
blocks: blocksValidator
|
163
228
|
};
|
164
229
|
export {
|
165
|
-
|
230
|
+
Validators,
|
231
|
+
bigintegerValidator,
|
232
|
+
datesValidator,
|
233
|
+
emailValidator,
|
234
|
+
enumerationValidator,
|
235
|
+
floatValidator,
|
236
|
+
integerValidator,
|
237
|
+
uidValidator
|
166
238
|
};
|
167
239
|
//# sourceMappingURL=validators.mjs.map
|