@strapi/core 5.0.0-beta.1 → 5.0.0-beta.3
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.d.ts +34 -45
- package/dist/Strapi.d.ts.map +1 -1
- package/dist/Strapi.js +143 -222
- package/dist/Strapi.js.map +1 -1
- package/dist/Strapi.mjs +133 -213
- package/dist/Strapi.mjs.map +1 -1
- package/dist/configuration/config-loader.d.ts.map +1 -1
- package/dist/configuration/config-loader.js +9 -5
- package/dist/configuration/config-loader.js.map +1 -1
- package/dist/configuration/config-loader.mjs +9 -5
- package/dist/configuration/config-loader.mjs.map +1 -1
- package/dist/configuration/get-dirs.d.ts +10 -0
- package/dist/configuration/get-dirs.d.ts.map +1 -0
- package/dist/{utils → configuration}/get-dirs.js +3 -2
- package/dist/configuration/get-dirs.js.map +1 -0
- package/dist/{utils → configuration}/get-dirs.mjs +3 -2
- package/dist/configuration/get-dirs.mjs.map +1 -0
- package/dist/configuration/index.d.ts +4 -7
- package/dist/configuration/index.d.ts.map +1 -1
- package/dist/configuration/index.js +6 -4
- package/dist/configuration/index.js.map +1 -1
- package/dist/configuration/index.mjs +5 -4
- package/dist/configuration/index.mjs.map +1 -1
- package/dist/core-api/service/collection-type.d.ts +4 -4
- package/dist/core-api/service/collection-type.d.ts.map +1 -1
- package/dist/core-api/service/collection-type.js +14 -5
- package/dist/core-api/service/collection-type.js.map +1 -1
- package/dist/core-api/service/collection-type.mjs +14 -5
- package/dist/core-api/service/collection-type.mjs.map +1 -1
- package/dist/core-api/service/single-type.d.ts.map +1 -1
- package/dist/core-api/service/single-type.js +8 -2
- package/dist/core-api/service/single-type.js.map +1 -1
- package/dist/core-api/service/single-type.mjs +8 -2
- package/dist/core-api/service/single-type.mjs.map +1 -1
- package/dist/ee/index.d.ts +2 -0
- package/dist/ee/index.d.ts.map +1 -1
- package/dist/ee/index.js.map +1 -1
- package/dist/ee/index.mjs.map +1 -1
- package/dist/factories.js.map +1 -1
- package/dist/factories.mjs.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -3
- package/dist/index.mjs.map +1 -1
- package/dist/loaders/admin.js +0 -1
- package/dist/loaders/admin.js.map +1 -1
- package/dist/loaders/admin.mjs +0 -1
- package/dist/loaders/admin.mjs.map +1 -1
- package/dist/loaders/index.d.ts.map +1 -1
- package/dist/loaders/index.js +0 -2
- package/dist/loaders/index.js.map +1 -1
- package/dist/loaders/index.mjs +0 -2
- package/dist/loaders/index.mjs.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.d.ts.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.js +2 -1
- package/dist/loaders/plugins/get-enabled-plugins.js.map +1 -1
- package/dist/loaders/plugins/get-enabled-plugins.mjs +2 -1
- package/dist/loaders/plugins/get-enabled-plugins.mjs.map +1 -1
- package/dist/middlewares/body.d.ts +2 -2
- package/dist/middlewares/body.d.ts.map +1 -1
- package/dist/middlewares/body.js +4 -4
- package/dist/middlewares/body.js.map +1 -1
- package/dist/middlewares/body.mjs +4 -4
- package/dist/middlewares/body.mjs.map +1 -1
- package/dist/middlewares/public.js +1 -1
- package/dist/middlewares/public.js.map +1 -1
- package/dist/middlewares/public.mjs +1 -1
- package/dist/middlewares/public.mjs.map +1 -1
- package/dist/providers/admin.d.ts +3 -0
- package/dist/providers/admin.d.ts.map +1 -0
- package/dist/providers/admin.js +20 -0
- package/dist/providers/admin.js.map +1 -0
- package/dist/providers/admin.mjs +21 -0
- package/dist/providers/admin.mjs.map +1 -0
- package/dist/providers/coreStore.d.ts +3 -0
- package/dist/providers/coreStore.d.ts.map +1 -0
- package/dist/providers/coreStore.js +11 -0
- package/dist/providers/coreStore.js.map +1 -0
- package/dist/providers/coreStore.mjs +12 -0
- package/dist/providers/coreStore.mjs.map +1 -0
- package/dist/providers/cron.d.ts +3 -0
- package/dist/providers/cron.d.ts.map +1 -0
- package/dist/providers/cron.js +20 -0
- package/dist/providers/cron.js.map +1 -0
- package/dist/providers/cron.mjs +21 -0
- package/dist/providers/cron.mjs.map +1 -0
- package/dist/providers/index.d.ts +3 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/index.mjs +11 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/providers/provider.d.ts +9 -0
- package/dist/providers/provider.d.ts.map +1 -0
- package/dist/providers/provider.js +5 -0
- package/dist/providers/provider.js.map +1 -0
- package/dist/providers/provider.mjs +5 -0
- package/dist/providers/provider.mjs.map +1 -0
- package/dist/providers/registries.d.ts +3 -0
- package/dist/providers/registries.d.ts.map +1 -0
- package/dist/providers/registries.js +33 -0
- package/dist/providers/registries.js.map +1 -0
- package/dist/providers/registries.mjs +34 -0
- package/dist/providers/registries.mjs.map +1 -0
- package/dist/providers/telemetry.d.ts +3 -0
- package/dist/providers/telemetry.d.ts.map +1 -0
- package/dist/providers/telemetry.js +19 -0
- package/dist/providers/telemetry.js.map +1 -0
- package/dist/providers/telemetry.mjs +20 -0
- package/dist/providers/telemetry.mjs.map +1 -0
- package/dist/providers/webhooks.d.ts +3 -0
- package/dist/providers/webhooks.d.ts.map +1 -0
- package/dist/providers/webhooks.js +30 -0
- package/dist/providers/webhooks.js.map +1 -0
- package/dist/providers/webhooks.mjs +31 -0
- package/dist/providers/webhooks.mjs.map +1 -0
- package/dist/registries/index.d.ts +0 -1
- package/dist/registries/index.d.ts.map +1 -1
- package/dist/services/config.d.ts +3 -0
- package/dist/services/config.d.ts.map +1 -0
- package/dist/{registries → services}/config.js +11 -7
- package/dist/services/config.js.map +1 -0
- package/dist/{registries → services}/config.mjs +10 -7
- package/dist/services/config.mjs.map +1 -0
- package/dist/services/content-api/index.js +1 -1
- package/dist/services/content-api/index.js.map +1 -1
- package/dist/services/content-api/index.mjs +1 -1
- package/dist/services/content-api/index.mjs.map +1 -1
- package/dist/services/content-api/permissions/index.js +1 -1
- package/dist/services/content-api/permissions/index.js.map +1 -1
- package/dist/services/content-api/permissions/index.mjs +1 -1
- package/dist/services/content-api/permissions/index.mjs.map +1 -1
- package/dist/services/document-service/attributes/index.d.ts.map +1 -0
- package/dist/services/document-service/attributes/index.js.map +1 -0
- package/dist/services/document-service/attributes/index.mjs.map +1 -0
- package/dist/services/document-service/attributes/transforms.d.ts.map +1 -0
- package/dist/services/document-service/attributes/transforms.js.map +1 -0
- package/dist/services/document-service/attributes/transforms.mjs.map +1 -0
- package/dist/services/document-service/common.d.ts +1 -1
- package/dist/services/document-service/common.d.ts.map +1 -1
- package/dist/services/document-service/common.js.map +1 -1
- package/dist/services/document-service/common.mjs.map +1 -1
- package/dist/services/document-service/components.d.ts +97 -1
- package/dist/services/document-service/components.d.ts.map +1 -1
- package/dist/services/{entity-service → document-service}/components.js +22 -31
- package/dist/services/document-service/components.js.map +1 -0
- package/dist/services/{entity-service → document-service}/components.mjs +22 -31
- package/dist/services/document-service/components.mjs.map +1 -0
- package/dist/services/document-service/entries.d.ts +8 -0
- package/dist/services/document-service/entries.d.ts.map +1 -0
- package/dist/services/document-service/entries.js +62 -0
- package/dist/services/document-service/entries.js.map +1 -0
- package/dist/services/document-service/entries.mjs +62 -0
- package/dist/services/document-service/entries.mjs.map +1 -0
- package/dist/services/document-service/index.d.ts +0 -2
- package/dist/services/document-service/index.d.ts.map +1 -1
- package/dist/services/document-service/index.js +14 -1
- package/dist/services/document-service/index.js.map +1 -1
- package/dist/services/document-service/index.mjs +14 -1
- package/dist/services/document-service/index.mjs.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.d.ts +4 -1
- package/dist/services/document-service/middlewares/middleware-manager.d.ts.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.js +8 -5
- package/dist/services/document-service/middlewares/middleware-manager.js.map +1 -1
- package/dist/services/document-service/middlewares/middleware-manager.mjs +8 -5
- package/dist/services/document-service/middlewares/middleware-manager.mjs.map +1 -1
- package/dist/services/document-service/params.d.ts +7 -1
- package/dist/services/document-service/params.d.ts.map +1 -1
- package/dist/services/document-service/params.js.map +1 -0
- package/dist/services/document-service/params.mjs.map +1 -0
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +74 -95
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +58 -79
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/document-service/transform/data.d.ts.map +1 -1
- package/dist/services/document-service/transform/data.js +4 -2
- package/dist/services/document-service/transform/data.js.map +1 -1
- package/dist/services/document-service/transform/data.mjs +4 -2
- package/dist/services/document-service/transform/data.mjs.map +1 -1
- package/dist/services/document-service/transform/query.d.ts +5 -0
- package/dist/services/document-service/transform/query.d.ts.map +1 -0
- package/dist/services/document-service/transform/query.js +9 -0
- package/dist/services/document-service/transform/query.js.map +1 -0
- package/dist/services/document-service/transform/query.mjs +9 -0
- package/dist/services/document-service/transform/query.mjs.map +1 -0
- package/dist/services/document-service/transform/relations/extract/data-ids.d.ts +8 -7
- package/dist/services/document-service/transform/relations/extract/data-ids.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.js +42 -78
- package/dist/services/document-service/transform/relations/extract/data-ids.js.map +1 -1
- package/dist/services/document-service/transform/relations/extract/data-ids.mjs +42 -78
- package/dist/services/document-service/transform/relations/extract/data-ids.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.d.ts +7 -3
- package/dist/services/document-service/transform/relations/transform/data-ids.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.js +57 -125
- package/dist/services/document-service/transform/relations/transform/data-ids.js.map +1 -1
- package/dist/services/document-service/transform/relations/transform/data-ids.mjs +58 -126
- package/dist/services/document-service/transform/relations/transform/data-ids.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/transform/default-locale.d.ts +9 -0
- package/dist/services/document-service/transform/relations/transform/default-locale.d.ts.map +1 -0
- package/dist/services/document-service/transform/relations/transform/default-locale.js +32 -0
- package/dist/services/document-service/transform/relations/transform/default-locale.js.map +1 -0
- package/dist/services/document-service/transform/relations/transform/default-locale.mjs +32 -0
- package/dist/services/document-service/transform/relations/transform/default-locale.mjs.map +1 -0
- package/dist/services/document-service/transform/relations/utils/i18n.d.ts +2 -2
- package/dist/services/document-service/transform/relations/utils/i18n.d.ts.map +1 -1
- package/dist/services/document-service/transform/relations/utils/i18n.js +3 -9
- package/dist/services/document-service/transform/relations/utils/i18n.js.map +1 -1
- package/dist/services/document-service/transform/relations/utils/i18n.mjs +3 -9
- package/dist/services/document-service/transform/relations/utils/i18n.mjs.map +1 -1
- package/dist/services/document-service/transform/relations/utils/map-relation.d.ts +6 -0
- package/dist/services/document-service/transform/relations/utils/map-relation.d.ts.map +1 -0
- package/dist/services/document-service/transform/relations/utils/map-relation.js +87 -0
- package/dist/services/document-service/transform/relations/utils/map-relation.js.map +1 -0
- package/dist/services/document-service/transform/relations/utils/map-relation.mjs +87 -0
- package/dist/services/document-service/transform/relations/utils/map-relation.mjs.map +1 -0
- package/dist/services/document-service/transform/relations/utils/types.d.ts +6 -0
- package/dist/services/document-service/transform/relations/utils/types.d.ts.map +1 -1
- package/dist/services/entity-service/index.d.ts.map +1 -1
- package/dist/services/entity-service/index.js +11 -4
- package/dist/services/entity-service/index.js.map +1 -1
- package/dist/services/entity-service/index.mjs +11 -4
- package/dist/services/entity-service/index.mjs.map +1 -1
- package/dist/services/entity-validator/index.d.ts.map +1 -1
- package/dist/services/entity-validator/index.js +93 -87
- package/dist/services/entity-validator/index.js.map +1 -1
- package/dist/services/entity-validator/index.mjs +93 -87
- package/dist/services/entity-validator/index.mjs.map +1 -1
- package/dist/services/metrics/rate-limiter.d.ts.map +1 -1
- package/dist/services/metrics/rate-limiter.js.map +1 -1
- package/dist/services/metrics/rate-limiter.mjs.map +1 -1
- package/dist/services/reloader.d.ts +7 -0
- package/dist/services/reloader.d.ts.map +1 -0
- package/dist/services/reloader.js +36 -0
- package/dist/services/reloader.js.map +1 -0
- package/dist/services/reloader.mjs +36 -0
- package/dist/services/reloader.mjs.map +1 -0
- package/dist/services/server/register-routes.js +3 -3
- package/dist/services/server/register-routes.js.map +1 -1
- package/dist/services/server/register-routes.mjs +3 -3
- package/dist/services/server/register-routes.mjs.map +1 -1
- package/dist/services/server/routing.d.ts +1 -1
- package/dist/services/webhook-store.d.ts.map +1 -1
- package/dist/services/webhook-store.js +5 -6
- package/dist/services/webhook-store.js.map +1 -1
- package/dist/services/webhook-store.mjs +5 -6
- package/dist/services/webhook-store.mjs.map +1 -1
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/resolve-working-dirs.d.ts +19 -0
- package/dist/utils/resolve-working-dirs.d.ts.map +1 -0
- package/dist/utils/resolve-working-dirs.js +13 -0
- package/dist/utils/resolve-working-dirs.js.map +1 -0
- package/dist/utils/resolve-working-dirs.mjs +11 -0
- package/dist/utils/resolve-working-dirs.mjs.map +1 -0
- package/dist/utils/transform-content-types-to-models.d.ts +21 -16
- package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
- package/dist/utils/transform-content-types-to-models.js +95 -57
- package/dist/utils/transform-content-types-to-models.js.map +1 -1
- package/dist/utils/transform-content-types-to-models.mjs +95 -57
- package/dist/utils/transform-content-types-to-models.mjs.map +1 -1
- package/dist/utils/update-notifier/index.d.ts +1 -6
- package/dist/utils/update-notifier/index.d.ts.map +1 -1
- package/dist/utils/update-notifier/index.js +11 -14
- package/dist/utils/update-notifier/index.js.map +1 -1
- package/dist/utils/update-notifier/index.mjs +11 -14
- package/dist/utils/update-notifier/index.mjs.map +1 -1
- package/package.json +21 -21
- package/dist/registries/config.d.ts +0 -4
- package/dist/registries/config.d.ts.map +0 -1
- package/dist/registries/config.js.map +0 -1
- package/dist/registries/config.mjs.map +0 -1
- package/dist/services/document-service/transform/relations/transform/output-ids.d.ts +0 -2
- package/dist/services/document-service/transform/relations/transform/output-ids.d.ts.map +0 -1
- package/dist/services/document-service/transform/relations/utils/data.js +0 -12
- package/dist/services/document-service/transform/relations/utils/data.js.map +0 -1
- package/dist/services/document-service/transform/relations/utils/data.mjs +0 -12
- package/dist/services/document-service/transform/relations/utils/data.mjs.map +0 -1
- package/dist/services/entity-service/attributes/index.d.ts.map +0 -1
- package/dist/services/entity-service/attributes/index.js.map +0 -1
- package/dist/services/entity-service/attributes/index.mjs.map +0 -1
- package/dist/services/entity-service/attributes/transforms.d.ts.map +0 -1
- package/dist/services/entity-service/attributes/transforms.js.map +0 -1
- package/dist/services/entity-service/attributes/transforms.mjs.map +0 -1
- package/dist/services/entity-service/components.d.ts +0 -95
- package/dist/services/entity-service/components.d.ts.map +0 -1
- package/dist/services/entity-service/components.js.map +0 -1
- package/dist/services/entity-service/components.mjs.map +0 -1
- package/dist/services/entity-service/params.d.ts +0 -8
- package/dist/services/entity-service/params.d.ts.map +0 -1
- package/dist/services/entity-service/params.js.map +0 -1
- package/dist/services/entity-service/params.mjs.map +0 -1
- package/dist/utils/get-dirs.d.ts +0 -9
- package/dist/utils/get-dirs.d.ts.map +0 -1
- package/dist/utils/get-dirs.js.map +0 -1
- package/dist/utils/get-dirs.mjs.map +0 -1
- /package/dist/services/{entity-service → document-service}/attributes/index.d.ts +0 -0
- /package/dist/services/{entity-service → document-service}/attributes/index.js +0 -0
- /package/dist/services/{entity-service → document-service}/attributes/index.mjs +0 -0
- /package/dist/services/{entity-service → document-service}/attributes/transforms.d.ts +0 -0
- /package/dist/services/{entity-service → document-service}/attributes/transforms.js +0 -0
- /package/dist/services/{entity-service → document-service}/attributes/transforms.mjs +0 -0
- /package/dist/services/{entity-service → document-service}/params.js +0 -0
- /package/dist/services/{entity-service → document-service}/params.mjs +0 -0
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport validators from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype Entity = {\n id: ID;\n [key: string]: unknown;\n} | null;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface AttributeValidatorMetas {\n attr: Schema.Attribute.AnyAttribute;\n updatedAttribute: { name: string; value: unknown };\n model: Struct.Schema;\n entity?: Entity;\n}\n\ninterface ModelValidatorMetas {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n }\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n }\n\n // FIXME: v4 was broken\n let validator = createModelValidator(createOrUpdate)(\n { model, data: updatedAttribute.value },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }))\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n };\n\nconst createRelationValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Relation>,\n { isDraft }: ValidatorContext\n ) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, validators)) {\n validator = (validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (metas: AttributeValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component') {\n validator = createComponentValidator(createOrUpdate)(\n { attr: metas.attr, updatedAttribute: metas.updatedAttribute },\n options\n );\n } else if (metas.attr.type === 'dynamiczone') {\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator(createOrUpdate)(\n {\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce((validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n }, {} as Record<string, strapiUtils.yup.BaseSchema>);\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n { model, data, entity },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test('relations-test', 'check that all relations exist', async function (data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n })\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce((result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n }, {} as Record<string, ID[]>);\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["strapiUtils","validator","prop","has","validators","isObject","data","isEmpty","isNil","castArray","mergeWith","isArray","value","uniqBy"],"mappings":";;;;;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0BA,qBAAAA,QAAY;AACnF,MAAM,EAAE,gBAAgB,IAAIA,qBAAY,QAAA;AAkCxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,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AAAA,YAAA,CACP;AAAA,YACD,CAAC,UAAU,aAAa;AAClB,kBAAAD,IAAAA,QAAQ,QAAQ,GAAG;AACd,uBAAA,SAAS,OAAO,QAAQ;AAAA,cACjC;AAAA,YACF;AAAA,UAAA;AAAA,WAED,MAAM;AAAA,MACX;AAAA,IAGF;AAEO,WAAA;AAAA,EACT,GAAG,CAA0B,CAAA;AAC/B;AAMA,MAAM,sBAAsB,OAAO,iBAAuC,OAAO;AAC/E,QAAM,WAAW,CAAA;AAEjB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,cAAc,GAAG;AACzD,UAAM,WAAW,YAAY;AACrB,YAAA,eAAeE,IAAO,OAAA,OAAO,IAAI;AACvC,YAAM,QAAQ,MAAM,OAAO,GAAG,MAAM,GAAiB,EAAE,MAAM;AAAA,QAC3D,OAAO;AAAA,UACL,IAAI;AAAA,YACF,KAAK,aAAa,IAAI,CAAC,MAAM,EAAE,EAAE;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,CACD;AAEG,UAAA,UAAU,aAAa,QAAQ;AACjC,cAAM,IAAI;AAAA,UACR,GACE,aAAa,SAAS,KACxB,wBAAwB,GAAG;AAAA,QAAA;AAAA,MAE/B;AAAA,IAAA;AAEO,aAAA,KAAK,UAAU;AAAA,EAC1B;AAEO,SAAA,QAAQ,IAAI,QAAQ;AAC7B;AAEA,MAAM,kBAA2D;AAAA,EAC/D,wBAAwB,qBAAqB,UAAU;AAAA,EACvD,sBAAsB,qBAAqB,QAAQ;AACrD;;"}
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport validators from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype Entity = {\n id: ID;\n [key: string]: unknown;\n} | null;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface AttributeValidatorMetas {\n attr: Schema.Attribute.AnyAttribute;\n updatedAttribute: { name: string; value: unknown };\n model: Struct.Schema;\n entity?: Entity;\n}\n\ninterface ModelValidatorMetas {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n },\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n }\n\n // FIXME: v4 was broken\n let validator = createModelValidator(createOrUpdate)(\n { model, data: updatedAttribute.value },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }))\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n };\n\nconst createRelationValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Relation>,\n { isDraft }: ValidatorContext\n ) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, validators)) {\n validator = (validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (metas: AttributeValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component') {\n validator = createComponentValidator(createOrUpdate)(\n { attr: metas.attr, updatedAttribute: metas.updatedAttribute },\n options\n );\n } else if (metas.attr.type === 'dynamiczone') {\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator(createOrUpdate)(\n {\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce(\n (validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n },\n {} as Record<string, strapiUtils.yup.BaseSchema>\n );\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n { model, data, entity },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test('relations-test', 'check that all relations exist', async function (data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n })\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce(\n (result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n },\n {} as Record<string, ID[]>\n );\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["strapiUtils","validator","prop","has","validators","isObject","data","isEmpty","isNil","castArray","mergeWith","isArray","value","uniqBy"],"mappings":";;;;;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0BA,qBAAAA,QAAY;AACnF,MAAM,EAAE,gBAAgB,IAAIA,qBAAY,QAAA;AAkCxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,GAAG,MAChB,cAAc,QAAQ,KAAK,YAC1B,MAAM,QAAQ,iBAAiB,KAAK,KAAK,iBAAiB,MAAM,SAAS,IAC5E;AACgB,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACI,MA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;AAAA,gBACjC;AAAA,cACF;AAAA,YAAA;AAAA,aAED,MAAM;AAAA,QACX;AAAA,MAGF;AAEO,aAAA;AAAA,IACT;AAAA,IACA,CAAC;AAAA,EAAA;AAEL;AAMA,MAAM,sBAAsB,OAAO,iBAAuC,OAAO;AAC/E,QAAM,WAAW,CAAA;AAEjB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,cAAc,GAAG;AACzD,UAAM,WAAW,YAAY;AACrB,YAAA,eAAeE,IAAO,OAAA,OAAO,IAAI;AACvC,YAAM,QAAQ,MAAM,OAAO,GAAG,MAAM,GAAiB,EAAE,MAAM;AAAA,QAC3D,OAAO;AAAA,UACL,IAAI;AAAA,YACF,KAAK,aAAa,IAAI,CAAC,MAAM,EAAE,EAAE;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,CACD;AAEG,UAAA,UAAU,aAAa,QAAQ;AACjC,cAAM,IAAI;AAAA,UACR,GACE,aAAa,SAAS,KACxB,wBAAwB,GAAG;AAAA,QAAA;AAAA,MAE/B;AAAA,IAAA;AAEO,aAAA,KAAK,UAAU;AAAA,EAC1B;AAEO,SAAA,QAAQ,IAAI,QAAQ;AAC7B;AAEA,MAAM,kBAA2D;AAAA,EAC/D,wBAAwB,qBAAqB,UAAU;AAAA,EACvD,sBAAsB,qBAAqB,QAAQ;AACrD;;"}
|
@@ -155,17 +155,20 @@ const createAttributeValidator = (createOrUpdate) => (metas, options) => {
|
|
155
155
|
};
|
156
156
|
const createModelValidator = (createOrUpdate) => ({ model, data, entity }, options) => {
|
157
157
|
const writableAttributes = model ? getWritableAttributes(model) : [];
|
158
|
-
const schema = writableAttributes.reduce(
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
158
|
+
const schema = writableAttributes.reduce(
|
159
|
+
(validators2, attributeName) => {
|
160
|
+
const metas = {
|
161
|
+
attr: model.attributes[attributeName],
|
162
|
+
updatedAttribute: { name: attributeName, value: prop(attributeName, data) },
|
163
|
+
model,
|
164
|
+
entity
|
165
|
+
};
|
166
|
+
const validator = createAttributeValidator(createOrUpdate)(metas, options);
|
167
|
+
validators2[attributeName] = validator;
|
168
|
+
return validators2;
|
169
|
+
},
|
170
|
+
{}
|
171
|
+
);
|
169
172
|
return yup.object().shape(schema);
|
170
173
|
};
|
171
174
|
const createValidateEntity = (createOrUpdate) => {
|
@@ -210,89 +213,92 @@ const buildRelationsStore = ({
|
|
210
213
|
return {};
|
211
214
|
}
|
212
215
|
const currentModel = strapi.getModel(uid);
|
213
|
-
return Object.keys(currentModel.attributes).reduce(
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
switch (attribute.type) {
|
220
|
-
case "relation":
|
221
|
-
case "media": {
|
222
|
-
if (attribute.type === "relation" && (attribute.relation === "morphToMany" || attribute.relation === "morphToOne")) {
|
223
|
-
break;
|
224
|
-
}
|
225
|
-
const target = (
|
226
|
-
// eslint-disable-next-line no-nested-ternary
|
227
|
-
attribute.type === "media" ? "plugin::upload.file" : attribute.target
|
228
|
-
);
|
229
|
-
let source;
|
230
|
-
if (Array.isArray(value)) {
|
231
|
-
source = value;
|
232
|
-
} else if (isObject(value)) {
|
233
|
-
if ("connect" in value && !isNil(value.connect)) {
|
234
|
-
source = value.connect;
|
235
|
-
} else if ("set" in value && !isNil(value.set)) {
|
236
|
-
source = value.set;
|
237
|
-
} else {
|
238
|
-
source = [];
|
239
|
-
}
|
240
|
-
} else {
|
241
|
-
source = castArray(value);
|
242
|
-
}
|
243
|
-
const idArray = source.map((v) => ({
|
244
|
-
id: typeof v === "object" ? v.id : v
|
245
|
-
}));
|
246
|
-
result[target] = result[target] || [];
|
247
|
-
result[target].push(...idArray);
|
248
|
-
break;
|
216
|
+
return Object.keys(currentModel.attributes).reduce(
|
217
|
+
(result, attributeName) => {
|
218
|
+
const attribute = currentModel.attributes[attributeName];
|
219
|
+
const value = data[attributeName];
|
220
|
+
if (isNil(value)) {
|
221
|
+
return result;
|
249
222
|
}
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
);
|
223
|
+
switch (attribute.type) {
|
224
|
+
case "relation":
|
225
|
+
case "media": {
|
226
|
+
if (attribute.type === "relation" && (attribute.relation === "morphToMany" || attribute.relation === "morphToOne")) {
|
227
|
+
break;
|
256
228
|
}
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
uid: attribute.component,
|
261
|
-
data: componentValue
|
262
|
-
}),
|
263
|
-
(objValue, srcValue) => {
|
264
|
-
if (isArray(objValue)) {
|
265
|
-
return objValue.concat(srcValue);
|
266
|
-
}
|
267
|
-
}
|
229
|
+
const target = (
|
230
|
+
// eslint-disable-next-line no-nested-ternary
|
231
|
+
attribute.type === "media" ? "plugin::upload.file" : attribute.target
|
268
232
|
);
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
233
|
+
let source;
|
234
|
+
if (Array.isArray(value)) {
|
235
|
+
source = value;
|
236
|
+
} else if (isObject(value)) {
|
237
|
+
if ("connect" in value && !isNil(value.connect)) {
|
238
|
+
source = value.connect;
|
239
|
+
} else if ("set" in value && !isNil(value.set)) {
|
240
|
+
source = value.set;
|
241
|
+
} else {
|
242
|
+
source = [];
|
243
|
+
}
|
244
|
+
} else {
|
245
|
+
source = castArray(value);
|
278
246
|
}
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
247
|
+
const idArray = source.map((v) => ({
|
248
|
+
id: typeof v === "object" ? v.id : v
|
249
|
+
}));
|
250
|
+
result[target] = result[target] || [];
|
251
|
+
result[target].push(...idArray);
|
252
|
+
break;
|
253
|
+
}
|
254
|
+
case "component": {
|
255
|
+
return castArray(value).reduce((relationsStore, componentValue) => {
|
256
|
+
if (!attribute.component) {
|
257
|
+
throw new ValidationError(
|
258
|
+
`Cannot build relations store from component, component identifier is undefined`
|
259
|
+
);
|
260
|
+
}
|
261
|
+
return mergeWith(
|
262
|
+
relationsStore,
|
263
|
+
buildRelationsStore({
|
264
|
+
uid: attribute.component,
|
265
|
+
data: componentValue
|
266
|
+
}),
|
267
|
+
(objValue, srcValue) => {
|
268
|
+
if (isArray(objValue)) {
|
269
|
+
return objValue.concat(srcValue);
|
270
|
+
}
|
288
271
|
}
|
272
|
+
);
|
273
|
+
}, result);
|
274
|
+
}
|
275
|
+
case "dynamiczone": {
|
276
|
+
return castArray(value).reduce((relationsStore, dzValue) => {
|
277
|
+
const value2 = dzValue;
|
278
|
+
if (!value2.__component) {
|
279
|
+
throw new ValidationError(
|
280
|
+
`Cannot build relations store from dynamiczone, component identifier is undefined`
|
281
|
+
);
|
289
282
|
}
|
290
|
-
|
291
|
-
|
283
|
+
return mergeWith(
|
284
|
+
relationsStore,
|
285
|
+
buildRelationsStore({
|
286
|
+
uid: value2.__component,
|
287
|
+
data: value2
|
288
|
+
}),
|
289
|
+
(objValue, srcValue) => {
|
290
|
+
if (isArray(objValue)) {
|
291
|
+
return objValue.concat(srcValue);
|
292
|
+
}
|
293
|
+
}
|
294
|
+
);
|
295
|
+
}, result);
|
296
|
+
}
|
292
297
|
}
|
293
|
-
|
294
|
-
|
295
|
-
|
298
|
+
return result;
|
299
|
+
},
|
300
|
+
{}
|
301
|
+
);
|
296
302
|
};
|
297
303
|
const checkRelationsExist = async (relationsStore = {}) => {
|
298
304
|
const promises = [];
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport validators from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype Entity = {\n id: ID;\n [key: string]: unknown;\n} | null;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface AttributeValidatorMetas {\n attr: Schema.Attribute.AnyAttribute;\n updatedAttribute: { name: string; value: unknown };\n model: Struct.Schema;\n entity?: Entity;\n}\n\ninterface ModelValidatorMetas {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n }\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n }\n\n // FIXME: v4 was broken\n let validator = createModelValidator(createOrUpdate)(\n { model, data: updatedAttribute.value },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }))\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n };\n\nconst createRelationValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Relation>,\n { isDraft }: ValidatorContext\n ) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, validators)) {\n validator = (validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (metas: AttributeValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component') {\n validator = createComponentValidator(createOrUpdate)(\n { attr: metas.attr, updatedAttribute: metas.updatedAttribute },\n options\n );\n } else if (metas.attr.type === 'dynamiczone') {\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator(createOrUpdate)(\n {\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce((validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n }, {} as Record<string, strapiUtils.yup.BaseSchema>);\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n { model, data, entity },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test('relations-test', 'check that all relations exist', async function (data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n })\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce((result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n }, {} as Record<string, ID[]>);\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["validator","validators","data","value"],"mappings":";;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0B,YAAY;AACnF,MAAM,EAAE,gBAAgB,IAAI,YAAY;AAkCxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,GAAG,MAChB,cAAc,QAAQ,KAAK,YAC1B,MAAM,QAAQ,iBAAiB,KAAK,KAAK,iBAAiB,MAAM,SAAS,IAC5E;AACgB,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACI,MAAA,UAAU,KAAK,GAAG,GAAG;AACP,oBAAA,cAAc,IAAI,KAAK,GAAG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AAK,KAAK,OAAO,QAAQ,cAAc,GAAG;AACzD,UAAM,WAAW,YAAY;AACrB,YAAA,eAAe,OAAO,OAAO,IAAI;AACvC,YAAM,QAAQ,MAAM,OAAO,GAAG,MAAM,GAAiB,EAAE,MAAM;AAAA,QAC3D,OAAO;AAAA,UACL,IAAI;AAAA,YACF,KAAK,aAAa,IAAI,CAAC,MAAM,EAAE,EAAE;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,CACD;AAEG,UAAA,UAAU,aAAa,QAAQ;AACjC,cAAM,IAAI;AAAA,UACR,GACE,aAAa,SAAS,KACxB,wBAAwB,GAAG;AAAA,QAAA;AAAA,MAE/B;AAAA,IAAA;AAEO,aAAA,KAAK,UAAU;AAAA,EAC1B;AAEO,SAAA,QAAQ,IAAI,QAAQ;AAC7B;AAEA,MAAM,kBAA2D;AAAA,EAC/D,wBAAwB,qBAAqB,UAAU;AAAA,EACvD,sBAAsB,qBAAqB,QAAQ;AACrD;"}
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../src/services/entity-validator/index.ts"],"sourcesContent":["/**\n * Entity validator\n * Module that will validate input data for entity creation or edition\n */\n\nimport { uniqBy, castArray, isNil, isArray, mergeWith } from 'lodash';\nimport { has, prop, isObject, isEmpty } from 'lodash/fp';\nimport strapiUtils from '@strapi/utils';\nimport { Modules, UID, Struct, Schema } from '@strapi/types';\nimport validators from './validators';\n\ntype CreateOrUpdate = 'creation' | 'update';\n\nconst { yup, validateYupSchema } = strapiUtils;\nconst { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;\nconst { ValidationError } = strapiUtils.errors;\n\ntype Entity = {\n id: ID;\n [key: string]: unknown;\n} | null;\n\ntype ID = { id: string | number };\n\ntype RelationSource = string | number | ID;\n\ninterface ValidatorMeta<TAttribute = Schema.Attribute.AnyAttribute> {\n attr: TAttribute;\n updatedAttribute: { name: string; value: any };\n}\n\ninterface ValidatorContext {\n isDraft?: boolean;\n locale?: string | null;\n}\n\ninterface AttributeValidatorMetas {\n attr: Schema.Attribute.AnyAttribute;\n updatedAttribute: { name: string; value: unknown };\n model: Struct.Schema;\n entity?: Entity;\n}\n\ninterface ModelValidatorMetas {\n model: Struct.Schema;\n data: Record<string, unknown>;\n entity?: Entity;\n}\n\nconst isInteger = (value: unknown): value is number => Number.isInteger(value);\n\nconst addMinMax = <\n T extends {\n min(value: number): T;\n max(value: number): T;\n },\n>(\n validator: T,\n {\n attr,\n updatedAttribute,\n }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.MinMaxOption<string | number>>\n): T => {\n let nextValidator: T = validator;\n\n if (\n isInteger(attr.min) &&\n (('required' in attr && attr.required) ||\n (Array.isArray(updatedAttribute.value) && updatedAttribute.value.length > 0))\n ) {\n nextValidator = nextValidator.min(attr.min);\n }\n if (isInteger(attr.max)) {\n nextValidator = nextValidator.max(attr.max);\n }\n return nextValidator;\n};\n\nconst addRequiredValidation = (createOrUpdate: CreateOrUpdate) => {\n return <T extends strapiUtils.yup.AnySchema>(\n validator: T,\n {\n attr: { required },\n }: ValidatorMeta<Partial<Schema.Attribute.AnyAttribute & Schema.Attribute.RequiredOption>>\n ): T => {\n let nextValidator = validator;\n\n if (required) {\n if (createOrUpdate === 'creation') {\n nextValidator = nextValidator.notNil();\n } else if (createOrUpdate === 'update') {\n nextValidator = nextValidator.notNull();\n }\n } else {\n nextValidator = nextValidator.nullable();\n }\n return nextValidator;\n };\n};\n\nconst addDefault = (createOrUpdate: CreateOrUpdate) => {\n return (\n validator: strapiUtils.yup.BaseSchema,\n { attr }: ValidatorMeta<Schema.Attribute.AnyAttribute & Schema.Attribute.DefaultOption<unknown>>\n ) => {\n let nextValidator = validator;\n\n if (createOrUpdate === 'creation') {\n if (\n ((attr.type === 'component' && attr.repeatable) || attr.type === 'dynamiczone') &&\n !attr.required\n ) {\n nextValidator = nextValidator.default([]);\n } else {\n nextValidator = nextValidator.default(attr.default);\n }\n } else {\n nextValidator = nextValidator.default(undefined);\n }\n\n return nextValidator;\n };\n};\n\nconst preventCast = (validator: strapiUtils.yup.AnySchema) =>\n validator.transform((val, originalVal) => originalVal);\n\nconst createComponentValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Component<UID.Component, boolean>>,\n { isDraft }: ValidatorContext\n ) => {\n const model = strapi.getModel(attr.component);\n if (!model) {\n throw new Error('Validation failed: Model not found');\n }\n\n if (attr?.repeatable) {\n // FIXME: yup v1\n\n let validator = yup\n .array()\n .of(\n yup.lazy((item) =>\n createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }).notNull()\n ) as any\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n }\n\n // FIXME: v4 was broken\n let validator = createModelValidator(createOrUpdate)(\n { model, data: updatedAttribute.value },\n { isDraft }\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createDzValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ attr, updatedAttribute }: ValidatorMeta, { isDraft }: ValidatorContext) => {\n let validator;\n\n validator = yup.array().of(\n yup.lazy((item) => {\n const model = strapi.getModel(prop('__component', item));\n const schema = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(strapi.components)),\n })\n .notNull();\n\n return model\n ? schema.concat(createModelValidator(createOrUpdate)({ model, data: item }, { isDraft }))\n : schema;\n }) as any // FIXME: yup v1\n );\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: true },\n updatedAttribute,\n });\n\n validator = addMinMax(validator, { attr, updatedAttribute });\n\n return validator;\n };\n\nconst createRelationValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (\n { attr, updatedAttribute }: ValidatorMeta<Schema.Attribute.Relation>,\n { isDraft }: ValidatorContext\n ) => {\n let validator;\n\n if (Array.isArray(updatedAttribute.value)) {\n validator = yup.array().of(yup.mixed());\n } else {\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !isDraft && attr.required },\n updatedAttribute,\n });\n\n return validator;\n };\n\nconst createScalarAttributeValidator =\n (createOrUpdate: CreateOrUpdate) => (metas: ValidatorMeta, options: ValidatorContext) => {\n let validator;\n\n if (has(metas.attr.type, validators)) {\n validator = (validators as any)[metas.attr.type](metas, options);\n } else {\n // No validators specified - fall back to mixed\n validator = yup.mixed();\n }\n\n validator = addRequiredValidation(createOrUpdate)(validator, {\n attr: { required: !options.isDraft && metas.attr.required },\n updatedAttribute: metas.updatedAttribute,\n });\n\n return validator;\n };\n\nconst createAttributeValidator =\n (createOrUpdate: CreateOrUpdate) =>\n (metas: AttributeValidatorMetas, options: ValidatorContext) => {\n let validator = yup.mixed();\n\n if (isMediaAttribute(metas.attr)) {\n validator = yup.mixed();\n } else if (isScalarAttribute(metas.attr)) {\n validator = createScalarAttributeValidator(createOrUpdate)(metas, options);\n } else {\n if (metas.attr.type === 'component') {\n validator = createComponentValidator(createOrUpdate)(\n { attr: metas.attr, updatedAttribute: metas.updatedAttribute },\n options\n );\n } else if (metas.attr.type === 'dynamiczone') {\n validator = createDzValidator(createOrUpdate)(metas, options);\n } else if (metas.attr.type === 'relation') {\n validator = createRelationValidator(createOrUpdate)(\n {\n attr: metas.attr,\n updatedAttribute: metas.updatedAttribute,\n },\n options\n );\n }\n\n validator = preventCast(validator);\n }\n\n validator = addDefault(createOrUpdate)(validator, metas);\n\n return validator;\n };\n\nconst createModelValidator =\n (createOrUpdate: CreateOrUpdate) =>\n ({ model, data, entity }: ModelValidatorMetas, options: ValidatorContext) => {\n const writableAttributes = model ? getWritableAttributes(model as any) : [];\n\n const schema = writableAttributes.reduce(\n (validators, attributeName) => {\n const metas = {\n attr: model.attributes[attributeName],\n updatedAttribute: { name: attributeName, value: prop(attributeName, data) },\n model,\n entity,\n };\n\n const validator = createAttributeValidator(createOrUpdate)(metas, options);\n\n validators[attributeName] = validator;\n\n return validators;\n },\n {} as Record<string, strapiUtils.yup.BaseSchema>\n );\n\n return yup.object().shape(schema);\n };\n\nconst createValidateEntity = (createOrUpdate: CreateOrUpdate) => {\n return async <\n TUID extends UID.ContentType,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n >(\n model: Schema.ContentType<TUID>,\n data: TData | Partial<TData> | undefined,\n options?: ValidatorContext,\n entity?: Entity\n ): Promise<TData> => {\n if (!isObject(data)) {\n const { displayName } = model.info;\n\n throw new ValidationError(\n `Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`\n );\n }\n\n const validator = createModelValidator(createOrUpdate)(\n { model, data, entity },\n {\n isDraft: options?.isDraft ?? false,\n locale: options?.locale ?? null,\n }\n )\n .test('relations-test', 'check that all relations exist', async function (data) {\n try {\n await checkRelationsExist(buildRelationsStore({ uid: model.uid, data }));\n } catch (e) {\n return this.createError({\n path: this.path,\n message: (e instanceof ValidationError && e.message) || 'Invalid relations',\n });\n }\n return true;\n })\n .required();\n\n return validateYupSchema(validator, {\n strict: false,\n abortEarly: false,\n })(data);\n };\n};\n\n/**\n * Builds an object containing all the media and relations being associated with an entity\n */\nconst buildRelationsStore = <TUID extends UID.Schema>({\n uid,\n data,\n}: {\n uid: TUID;\n data: Record<string, unknown> | null;\n}): Record<string, ID[]> => {\n if (!uid) {\n throw new ValidationError(`Cannot build relations store: \"uid\" is undefined`);\n }\n\n if (isEmpty(data)) {\n return {};\n }\n\n const currentModel = strapi.getModel(uid);\n\n return Object.keys(currentModel.attributes).reduce(\n (result, attributeName: string) => {\n const attribute = currentModel.attributes[attributeName];\n const value = data[attributeName];\n\n if (isNil(value)) {\n return result;\n }\n\n switch (attribute.type) {\n case 'relation':\n case 'media': {\n if (\n attribute.type === 'relation' &&\n (attribute.relation === 'morphToMany' || attribute.relation === 'morphToOne')\n ) {\n // TODO: handle polymorphic relations\n break;\n }\n\n const target =\n // eslint-disable-next-line no-nested-ternary\n attribute.type === 'media' ? 'plugin::upload.file' : attribute.target;\n // As there are multiple formats supported for associating relations\n // with an entity, the value here can be an: array, object or number.\n let source: RelationSource[];\n if (Array.isArray(value)) {\n source = value;\n } else if (isObject(value)) {\n if ('connect' in value && !isNil(value.connect)) {\n source = value.connect as RelationSource[];\n } else if ('set' in value && !isNil(value.set)) {\n source = value.set as RelationSource[];\n } else {\n source = [];\n }\n } else {\n source = castArray(value as RelationSource);\n }\n const idArray = source.map((v) => ({\n id: typeof v === 'object' ? v.id : v,\n }));\n\n // Update the relationStore to keep track of all associations being made\n // with relations and media.\n result[target] = result[target] || [];\n result[target].push(...idArray);\n break;\n }\n case 'component': {\n return castArray(value).reduce((relationsStore, componentValue) => {\n if (!attribute.component) {\n throw new ValidationError(\n `Cannot build relations store from component, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: attribute.component,\n data: componentValue as Record<string, unknown>,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n case 'dynamiczone': {\n return castArray(value).reduce((relationsStore, dzValue) => {\n const value = dzValue as Record<string, unknown>;\n if (!value.__component) {\n throw new ValidationError(\n `Cannot build relations store from dynamiczone, component identifier is undefined`\n );\n }\n\n return mergeWith(\n relationsStore,\n buildRelationsStore({\n uid: value.__component as UID.Component,\n data: value,\n }),\n (objValue, srcValue) => {\n if (isArray(objValue)) {\n return objValue.concat(srcValue);\n }\n }\n );\n }, result) as Record<string, ID[]>;\n }\n default:\n break;\n }\n\n return result;\n },\n {} as Record<string, ID[]>\n );\n};\n\n/**\n * Iterate through the relations store and validates that every relation or media\n * mentioned exists\n */\nconst checkRelationsExist = async (relationsStore: Record<string, ID[]> = {}) => {\n const promises = [];\n\n for (const [key, value] of Object.entries(relationsStore)) {\n const evaluate = async () => {\n const uniqueValues = uniqBy(value, `id`);\n const count = await strapi.db.query(key as UID.Schema).count({\n where: {\n id: {\n $in: uniqueValues.map((v) => v.id),\n },\n },\n });\n\n if (count !== uniqueValues.length) {\n throw new ValidationError(\n `${\n uniqueValues.length - count\n } relation(s) of type ${key} associated with this entity do not exist`\n );\n }\n };\n promises.push(evaluate());\n }\n\n return Promise.all(promises);\n};\n\nconst entityValidator: Modules.EntityValidator.EntityValidator = {\n validateEntityCreation: createValidateEntity('creation'),\n validateEntityUpdate: createValidateEntity('update'),\n};\n\nexport default entityValidator;\n"],"names":["validator","validators","data","value"],"mappings":";;;;AAaA,MAAM,EAAE,KAAK,kBAAsB,IAAA;AACnC,MAAM,EAAE,kBAAkB,mBAAmB,sBAAA,IAA0B,YAAY;AACnF,MAAM,EAAE,gBAAgB,IAAI,YAAY;AAkCxC,MAAM,YAAY,CAAC,UAAoC,OAAO,UAAU,KAAK;AAE7E,MAAM,YAAY,CAMhB,WACA;AAAA,EACE;AAAA,EACA;AACF,MACM;AACN,MAAI,gBAAmB;AAEvB,MACE,UAAU,KAAK,GAAG,MAChB,cAAc,QAAQ,KAAK,YAC1B,MAAM,QAAQ,iBAAiB,KAAK,KAAK,iBAAiB,MAAM,SAAS,IAC5E;AACgB,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACI,MAAA,UAAU,KAAK,GAAG,GAAG;AACP,oBAAA,cAAc,IAAI,KAAK,GAAG;AAAA,EAC5C;AACO,SAAA;AACT;AAEA,MAAM,wBAAwB,CAAC,mBAAmC;AAChE,SAAO,CACL,WACA;AAAA,IACE,MAAM,EAAE,SAAS;AAAA,EAAA,MAEb;AACN,QAAI,gBAAgB;AAEpB,QAAI,UAAU;AACZ,UAAI,mBAAmB,YAAY;AACjC,wBAAgB,cAAc;MAAO,WAC5B,mBAAmB,UAAU;AACtC,wBAAgB,cAAc;MAChC;AAAA,IAAA,OACK;AACL,sBAAgB,cAAc;IAChC;AACO,WAAA;AAAA,EAAA;AAEX;AAEA,MAAM,a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AAa,QAAQ;AACjC,cAAM,IAAI;AAAA,UACR,GACE,aAAa,SAAS,KACxB,wBAAwB,GAAG;AAAA,QAAA;AAAA,MAE/B;AAAA,IAAA;AAEO,aAAA,KAAK,UAAU;AAAA,EAC1B;AAEO,SAAA,QAAQ,IAAI,QAAQ;AAC7B;AAEA,MAAM,kBAA2D;AAAA,EAC/D,wBAAwB,qBAAqB,UAAU;AAAA,EACvD,sBAAsB,qBAAqB,QAAQ;AACrD;"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rate-limiter.d.ts","sourceRoot":"","sources":["../../../src/services/metrics/rate-limiter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC,UAAU,OAAO;IACf,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;
|
1
|
+
{"version":3,"file":"rate-limiter.d.ts","sourceRoot":"","sources":["../../../src/services/metrics/rate-limiter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC,UAAU,OAAO;IACf,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;iCAKuB,MAAM,sBAA0B,OAAO,aAIxC,MAAM;AAJ7B,wBAqBE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rate-limiter.js","sources":["../../../src/services/metrics/rate-limiter.ts"],"sourcesContent":["import type { Sender } from './sender';\n\ninterface Options {\n limitedEvents?: string[];\n}\n\ntype TailParams<T extends (...args: any[]) => any>
|
1
|
+
{"version":3,"file":"rate-limiter.js","sources":["../../../src/services/metrics/rate-limiter.ts"],"sourcesContent":["import type { Sender } from './sender';\n\ninterface Options {\n limitedEvents?: string[];\n}\n\ntype TailParams<T extends (...args: any[]) => any> =\n Parameters<T> extends [unknown, ...infer U] ? U : never;\n\nexport default (sender: Sender, { limitedEvents = [] }: Options = {}) => {\n let currentDay = new Date().getDate();\n const eventCache = new Map();\n\n return async (event: string, ...args: TailParams<Sender>) => {\n if (!limitedEvents.includes(event)) {\n return sender(event, ...args);\n }\n\n if (new Date().getDate() !== currentDay) {\n eventCache.clear();\n currentDay = new Date().getDate();\n }\n\n if (eventCache.has(event)) {\n return false;\n }\n\n eventCache.set(event, true);\n return sender(event, ...args);\n };\n};\n"],"names":[],"mappings":";AASA,MAAe,oBAAA,CAAC,QAAgB,EAAE,gBAAgB,CAAG,EAAA,IAAa,CAAA,MAAO;AACvE,MAAI,cAAa,oBAAI,KAAK,GAAE,QAAQ;AAC9B,QAAA,iCAAiB;AAEhB,SAAA,OAAO,UAAkB,SAA6B;AAC3D,QAAI,CAAC,cAAc,SAAS,KAAK,GAAG;AAC3B,aAAA,OAAO,OAAO,GAAG,IAAI;AAAA,IAC9B;AAEA,SAAQ,oBAAA,KAAA,GAAO,QAAA,MAAc,YAAY;AACvC,iBAAW,MAAM;AACJ,oBAAA,oBAAI,KAAK,GAAE,QAAQ;AAAA,IAClC;AAEI,QAAA,WAAW,IAAI,KAAK,GAAG;AAClB,aAAA;AAAA,IACT;AAEW,eAAA,IAAI,OAAO,IAAI;AACnB,WAAA,OAAO,OAAO,GAAG,IAAI;AAAA,EAAA;AAEhC;;"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rate-limiter.mjs","sources":["../../../src/services/metrics/rate-limiter.ts"],"sourcesContent":["import type { Sender } from './sender';\n\ninterface Options {\n limitedEvents?: string[];\n}\n\ntype TailParams<T extends (...args: any[]) => any>
|
1
|
+
{"version":3,"file":"rate-limiter.mjs","sources":["../../../src/services/metrics/rate-limiter.ts"],"sourcesContent":["import type { Sender } from './sender';\n\ninterface Options {\n limitedEvents?: string[];\n}\n\ntype TailParams<T extends (...args: any[]) => any> =\n Parameters<T> extends [unknown, ...infer U] ? U : never;\n\nexport default (sender: Sender, { limitedEvents = [] }: Options = {}) => {\n let currentDay = new Date().getDate();\n const eventCache = new Map();\n\n return async (event: string, ...args: TailParams<Sender>) => {\n if (!limitedEvents.includes(event)) {\n return sender(event, ...args);\n }\n\n if (new Date().getDate() !== currentDay) {\n eventCache.clear();\n currentDay = new Date().getDate();\n }\n\n if (eventCache.has(event)) {\n return false;\n }\n\n eventCache.set(event, true);\n return sender(event, ...args);\n };\n};\n"],"names":[],"mappings":"AASA,MAAe,oBAAA,CAAC,QAAgB,EAAE,gBAAgB,CAAG,EAAA,IAAa,CAAA,MAAO;AACvE,MAAI,cAAa,oBAAI,KAAK,GAAE,QAAQ;AAC9B,QAAA,iCAAiB;AAEhB,SAAA,OAAO,UAAkB,SAA6B;AAC3D,QAAI,CAAC,cAAc,SAAS,KAAK,GAAG;AAC3B,aAAA,OAAO,OAAO,GAAG,IAAI;AAAA,IAC9B;AAEA,SAAQ,oBAAA,KAAA,GAAO,QAAA,MAAc,YAAY;AACvC,iBAAW,MAAM;AACJ,oBAAA,oBAAI,KAAK,GAAE,QAAQ;AAAA,IAClC;AAEI,QAAA,WAAW,IAAI,KAAK,GAAG;AAClB,aAAA;AAAA,IACT;AAEW,eAAA,IAAI,OAAO,IAAI;AACnB,WAAA,OAAO,OAAO,GAAG,IAAI;AAAA,EAAA;AAEhC;"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"reloader.d.ts","sourceRoot":"","sources":["../../src/services/reloader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAE1C,eAAO,MAAM,cAAc,WAAY,KAAK,MAAM;;;;CAsCjD,CAAC"}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
|
+
const createReloader = (strapi) => {
|
4
|
+
const state = {
|
5
|
+
shouldReload: 0,
|
6
|
+
isWatching: true
|
7
|
+
};
|
8
|
+
function reload() {
|
9
|
+
if (state.shouldReload > 0) {
|
10
|
+
state.shouldReload -= 1;
|
11
|
+
reload.isReloading = false;
|
12
|
+
return;
|
13
|
+
}
|
14
|
+
if (strapi.config.get("autoReload")) {
|
15
|
+
process.send?.("reload");
|
16
|
+
}
|
17
|
+
}
|
18
|
+
Object.defineProperty(reload, "isWatching", {
|
19
|
+
configurable: true,
|
20
|
+
enumerable: true,
|
21
|
+
set(value) {
|
22
|
+
if (state.isWatching === false && value === true) {
|
23
|
+
state.shouldReload += 1;
|
24
|
+
}
|
25
|
+
state.isWatching = value;
|
26
|
+
},
|
27
|
+
get() {
|
28
|
+
return state.isWatching;
|
29
|
+
}
|
30
|
+
});
|
31
|
+
reload.isReloading = false;
|
32
|
+
reload.isWatching = true;
|
33
|
+
return reload;
|
34
|
+
};
|
35
|
+
exports.createReloader = createReloader;
|
36
|
+
//# sourceMappingURL=reloader.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"reloader.js","sources":["../../src/services/reloader.ts"],"sourcesContent":["import type { Core } from '@strapi/types';\n\nexport const createReloader = (strapi: Core.Strapi) => {\n const state = {\n shouldReload: 0,\n isWatching: true,\n };\n\n function reload() {\n if (state.shouldReload > 0) {\n // Reset the reloading state\n state.shouldReload -= 1;\n reload.isReloading = false;\n return;\n }\n\n if (strapi.config.get('autoReload')) {\n process.send?.('reload');\n }\n }\n\n Object.defineProperty(reload, 'isWatching', {\n configurable: true,\n enumerable: true,\n set(value) {\n // Special state when the reloader is disabled temporarly (see GraphQL plugin example).\n if (state.isWatching === false && value === true) {\n state.shouldReload += 1;\n }\n state.isWatching = value;\n },\n get() {\n return state.isWatching;\n },\n });\n\n reload.isReloading = false;\n reload.isWatching = true;\n\n return reload;\n};\n"],"names":[],"mappings":";;AAEa,MAAA,iBAAiB,CAAC,WAAwB;AACrD,QAAM,QAAQ;AAAA,IACZ,cAAc;AAAA,IACd,YAAY;AAAA,EAAA;AAGd,WAAS,SAAS;AACZ,QAAA,MAAM,eAAe,GAAG;AAE1B,YAAM,gBAAgB;AACtB,aAAO,cAAc;AACrB;AAAA,IACF;AAEA,QAAI,OAAO,OAAO,IAAI,YAAY,GAAG;AACnC,cAAQ,OAAO,QAAQ;AAAA,IACzB;AAAA,EACF;AAEO,SAAA,eAAe,QAAQ,cAAc;AAAA,IAC1C,cAAc;AAAA,IACd,YAAY;AAAA,IACZ,IAAI,OAAO;AAET,UAAI,MAAM,eAAe,SAAS,UAAU,MAAM;AAChD,cAAM,gBAAgB;AAAA,MACxB;AACA,YAAM,aAAa;AAAA,IACrB;AAAA,IACA,MAAM;AACJ,aAAO,MAAM;AAAA,IACf;AAAA,EAAA,CACD;AAED,SAAO,cAAc;AACrB,SAAO,aAAa;AAEb,SAAA;AACT;;"}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
const createReloader = (strapi) => {
|
2
|
+
const state = {
|
3
|
+
shouldReload: 0,
|
4
|
+
isWatching: true
|
5
|
+
};
|
6
|
+
function reload() {
|
7
|
+
if (state.shouldReload > 0) {
|
8
|
+
state.shouldReload -= 1;
|
9
|
+
reload.isReloading = false;
|
10
|
+
return;
|
11
|
+
}
|
12
|
+
if (strapi.config.get("autoReload")) {
|
13
|
+
process.send?.("reload");
|
14
|
+
}
|
15
|
+
}
|
16
|
+
Object.defineProperty(reload, "isWatching", {
|
17
|
+
configurable: true,
|
18
|
+
enumerable: true,
|
19
|
+
set(value) {
|
20
|
+
if (state.isWatching === false && value === true) {
|
21
|
+
state.shouldReload += 1;
|
22
|
+
}
|
23
|
+
state.isWatching = value;
|
24
|
+
},
|
25
|
+
get() {
|
26
|
+
return state.isWatching;
|
27
|
+
}
|
28
|
+
});
|
29
|
+
reload.isReloading = false;
|
30
|
+
reload.isWatching = true;
|
31
|
+
return reload;
|
32
|
+
};
|
33
|
+
export {
|
34
|
+
createReloader
|
35
|
+
};
|
36
|
+
//# sourceMappingURL=reloader.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"reloader.mjs","sources":["../../src/services/reloader.ts"],"sourcesContent":["import type { Core } from '@strapi/types';\n\nexport const createReloader = (strapi: Core.Strapi) => {\n const state = {\n shouldReload: 0,\n isWatching: true,\n };\n\n function reload() {\n if (state.shouldReload > 0) {\n // Reset the reloading state\n state.shouldReload -= 1;\n reload.isReloading = false;\n return;\n }\n\n if (strapi.config.get('autoReload')) {\n process.send?.('reload');\n }\n }\n\n Object.defineProperty(reload, 'isWatching', {\n configurable: true,\n enumerable: true,\n set(value) {\n // Special state when the reloader is disabled temporarly (see GraphQL plugin example).\n if (state.isWatching === false && value === true) {\n state.shouldReload += 1;\n }\n state.isWatching = value;\n },\n get() {\n return state.isWatching;\n },\n });\n\n reload.isReloading = false;\n reload.isWatching = true;\n\n return reload;\n};\n"],"names":[],"mappings":"AAEa,MAAA,iBAAiB,CAAC,WAAwB;AACrD,QAAM,QAAQ;AAAA,IACZ,cAAc;AAAA,IACd,YAAY;AAAA,EAAA;AAGd,WAAS,SAAS;AACZ,QAAA,MAAM,eAAe,GAAG;AAE1B,YAAM,gBAAgB;AACtB,aAAO,cAAc;AACrB;AAAA,IACF;AAEA,QAAI,OAAO,OAAO,IAAI,YAAY,GAAG;AACnC,cAAQ,OAAO,QAAQ;AAAA,IACzB;AAAA,EACF;AAEO,SAAA,eAAe,QAAQ,cAAc;AAAA,IAC1C,cAAc;AAAA,IACd,YAAY;AAAA,IACZ,IAAI,OAAO;AAET,UAAI,MAAM,eAAe,SAAS,UAAU,MAAM;AAChD,cAAM,gBAAgB;AAAA,MACxB;AACA,YAAM,aAAa;AAAA,IACrB;AAAA,IACA,MAAM;AACJ,aAAO,MAAM;AAAA,IACf;AAAA,EAAA,CACD;AAED,SAAO,cAAc;AACrB,SAAO,aAAa;AAEb,SAAA;AACT;"}
|
@@ -21,7 +21,7 @@ const registerAllRoutes = (strapi) => {
|
|
21
21
|
};
|
22
22
|
const registerAdminRoutes = (strapi) => {
|
23
23
|
const generateRouteScope = createRouteScopeGenerator(`admin::`);
|
24
|
-
___default.default.forEach(strapi.admin
|
24
|
+
___default.default.forEach(strapi.admin.routes, (router) => {
|
25
25
|
router.type = router.type || "admin";
|
26
26
|
router.prefix = router.prefix || `/admin`;
|
27
27
|
router.routes.forEach((route) => {
|
@@ -59,8 +59,8 @@ const registerPluginRoutes = (strapi) => {
|
|
59
59
|
}
|
60
60
|
};
|
61
61
|
const registerAPIRoutes = (strapi) => {
|
62
|
-
for (const apiName of Object.keys(strapi.
|
63
|
-
const api = strapi.api
|
62
|
+
for (const apiName of Object.keys(strapi.apis)) {
|
63
|
+
const api = strapi.api(apiName);
|
64
64
|
const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);
|
65
65
|
___default.default.forEach(api.routes, (router) => {
|
66
66
|
router.type = "content-api";
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"register-routes.js","sources":["../../../src/services/server/register-routes.ts"],"sourcesContent":["import _ from 'lodash';\nimport type { Core } from '@strapi/types';\n\nconst createRouteScopeGenerator = (namespace: string) => (route: Core.RouteInput) => {\n const prefix = namespace.endsWith('::') ? namespace : `${namespace}.`;\n\n if (typeof route.handler === 'string') {\n _.defaultsDeep(route, {\n config: {\n auth: {\n scope: [`${route.handler.startsWith(prefix) ? '' : prefix}${route.handler}`],\n },\n },\n });\n }\n};\n\n/**\n * Register all routes\n */\nexport default (strapi: Core.Strapi) => {\n registerAdminRoutes(strapi);\n registerAPIRoutes(strapi);\n registerPluginRoutes(strapi);\n};\n\n/**\n * Register admin routes\n * @param {import('../../').Strapi} strapi\n */\nconst registerAdminRoutes = (strapi: Core.Strapi) => {\n const generateRouteScope = createRouteScopeGenerator(`admin::`);\n\n _.forEach(strapi.admin
|
1
|
+
{"version":3,"file":"register-routes.js","sources":["../../../src/services/server/register-routes.ts"],"sourcesContent":["import _ from 'lodash';\nimport type { Core } from '@strapi/types';\n\nconst createRouteScopeGenerator = (namespace: string) => (route: Core.RouteInput) => {\n const prefix = namespace.endsWith('::') ? namespace : `${namespace}.`;\n\n if (typeof route.handler === 'string') {\n _.defaultsDeep(route, {\n config: {\n auth: {\n scope: [`${route.handler.startsWith(prefix) ? '' : prefix}${route.handler}`],\n },\n },\n });\n }\n};\n\n/**\n * Register all routes\n */\nexport default (strapi: Core.Strapi) => {\n registerAdminRoutes(strapi);\n registerAPIRoutes(strapi);\n registerPluginRoutes(strapi);\n};\n\n/**\n * Register admin routes\n * @param {import('../../').Strapi} strapi\n */\nconst registerAdminRoutes = (strapi: Core.Strapi) => {\n const generateRouteScope = createRouteScopeGenerator(`admin::`);\n\n _.forEach(strapi.admin.routes, (router) => {\n router.type = router.type || 'admin';\n router.prefix = router.prefix || `/admin`;\n router.routes.forEach((route) => {\n generateRouteScope(route);\n route.info = { pluginName: 'admin' };\n });\n strapi.server.routes(router);\n });\n};\n\n/**\n * Register plugin routes\n * @param {import('../../').Strapi} strapi\n */\nconst registerPluginRoutes = (strapi: Core.Strapi) => {\n for (const pluginName of Object.keys(strapi.plugins)) {\n const plugin = strapi.plugins[pluginName];\n\n const generateRouteScope = createRouteScopeGenerator(`plugin::${pluginName}`);\n\n if (Array.isArray(plugin.routes)) {\n plugin.routes.forEach((route) => {\n generateRouteScope(route);\n route.info = { pluginName };\n });\n\n strapi.server.routes({\n type: 'admin',\n prefix: `/${pluginName}`,\n routes: plugin.routes,\n });\n } else {\n _.forEach(plugin.routes, (router) => {\n router.type = router.type || 'admin';\n router.prefix = router.prefix || `/${pluginName}`;\n router.routes.forEach((route) => {\n generateRouteScope(route);\n route.info = { pluginName };\n });\n\n strapi.server.routes(router);\n });\n }\n }\n};\n\n/**\n * Register api routes\n */\nconst registerAPIRoutes = (strapi: Core.Strapi) => {\n for (const apiName of Object.keys(strapi.apis)) {\n const api = strapi.api(apiName);\n\n const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);\n\n _.forEach(api.routes, (router) => {\n // TODO: remove once auth setup\n // pass meta down to compose endpoint\n router.type = 'content-api';\n router.routes?.forEach((route) => {\n generateRouteScope(route);\n route.info = { apiName };\n });\n\n return strapi.server.routes(router);\n });\n }\n};\n"],"names":["_"],"mappings":";;;;AAGA,MAAM,4BAA4B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AA,IAAA,OACI;AACLA,iBAAAA,QAAE,QAAQ,OAAO,QAAQ,CAAC,WAAW;AAC5B,eAAA,OAAO,OAAO,QAAQ;AAC7B,eAAO,SAAS,OAAO,UAAU,IAAI,UAAU;AACxC,eAAA,OAAO,QAAQ,CAAC,UAAU;AAC/B,6BAAmB,KAAK;AAClB,gBAAA,OAAO,EAAE;QAAW,CAC3B;AAEM,eAAA,OAAO,OAAO,MAAM;AAAA,MAAA,CAC5B;AAAA,IACH;AAAA,EACF;AACF;AAKA,MAAM,oBAAoB,CAAC,WAAwB;AACjD,aAAW,WAAW,OAAO,KAAK,OAAO,IAAI,GAAG;AACxC,UAAA,MAAM,OAAO,IAAI,OAAO;AAE9B,UAAM,qBAAqB,0BAA0B,QAAQ,OAAO,EAAE;AAEtEA,eAAAA,QAAE,QAAQ,IAAI,QAAQ,CAAC,WAAW;AAGhC,aAAO,OAAO;AACP,aAAA,QAAQ,QAAQ,CAAC,UAAU;AAChC,2BAAmB,KAAK;AAClB,cAAA,OAAO,EAAE;MAAQ,CACxB;AAEM,aAAA,OAAO,OAAO,OAAO,MAAM;AAAA,IAAA,CACnC;AAAA,EACH;AACF;;"}
|
@@ -18,7 +18,7 @@ const registerAllRoutes = (strapi) => {
|
|
18
18
|
};
|
19
19
|
const registerAdminRoutes = (strapi) => {
|
20
20
|
const generateRouteScope = createRouteScopeGenerator(`admin::`);
|
21
|
-
_.forEach(strapi.admin
|
21
|
+
_.forEach(strapi.admin.routes, (router) => {
|
22
22
|
router.type = router.type || "admin";
|
23
23
|
router.prefix = router.prefix || `/admin`;
|
24
24
|
router.routes.forEach((route) => {
|
@@ -56,8 +56,8 @@ const registerPluginRoutes = (strapi) => {
|
|
56
56
|
}
|
57
57
|
};
|
58
58
|
const registerAPIRoutes = (strapi) => {
|
59
|
-
for (const apiName of Object.keys(strapi.
|
60
|
-
const api = strapi.api
|
59
|
+
for (const apiName of Object.keys(strapi.apis)) {
|
60
|
+
const api = strapi.api(apiName);
|
61
61
|
const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);
|
62
62
|
_.forEach(api.routes, (router) => {
|
63
63
|
router.type = "content-api";
|