@strapi/core 0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064 → 0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744

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.

Potentially problematic release.


This version of @strapi/core might be problematic. Click here for more details.

Files changed (112) hide show
  1. package/dist/Strapi.d.ts.map +1 -1
  2. package/dist/Strapi.js +2 -0
  3. package/dist/Strapi.js.map +1 -1
  4. package/dist/Strapi.mjs +2 -0
  5. package/dist/Strapi.mjs.map +1 -1
  6. package/dist/core-api/routes/index.d.ts +22 -4
  7. package/dist/core-api/routes/index.d.ts.map +1 -1
  8. package/dist/core-api/routes/index.js +8 -118
  9. package/dist/core-api/routes/index.js.map +1 -1
  10. package/dist/core-api/routes/index.mjs +8 -118
  11. package/dist/core-api/routes/index.mjs.map +1 -1
  12. package/dist/domain/module/index.d.ts.map +1 -1
  13. package/dist/domain/module/index.js +0 -3
  14. package/dist/domain/module/index.js.map +1 -1
  15. package/dist/domain/module/index.mjs +0 -3
  16. package/dist/domain/module/index.mjs.map +1 -1
  17. package/dist/ee/index.d.ts +6 -0
  18. package/dist/ee/index.d.ts.map +1 -1
  19. package/dist/ee/index.js +29 -3
  20. package/dist/ee/index.js.map +1 -1
  21. package/dist/ee/index.mjs +30 -4
  22. package/dist/ee/index.mjs.map +1 -1
  23. package/dist/ee/license.d.ts +3 -1
  24. package/dist/ee/license.d.ts.map +1 -1
  25. package/dist/ee/license.js +6 -1
  26. package/dist/ee/license.js.map +1 -1
  27. package/dist/ee/license.mjs +6 -2
  28. package/dist/ee/license.mjs.map +1 -1
  29. package/dist/factories.d.ts +1 -3
  30. package/dist/factories.d.ts.map +1 -1
  31. package/dist/factories.js +2 -10
  32. package/dist/factories.js.map +1 -1
  33. package/dist/factories.mjs +3 -10
  34. package/dist/factories.mjs.map +1 -1
  35. package/dist/index.js +1 -1
  36. package/dist/index.mjs +1 -1
  37. package/dist/package.json.js +15 -14
  38. package/dist/package.json.js.map +1 -1
  39. package/dist/package.json.mjs +15 -14
  40. package/dist/package.json.mjs.map +1 -1
  41. package/dist/services/core-store.d.ts +2 -2
  42. package/dist/services/core-store.d.ts.map +1 -1
  43. package/dist/services/core-store.js.map +1 -1
  44. package/dist/services/core-store.mjs.map +1 -1
  45. package/dist/services/document-service/entries.d.ts.map +1 -1
  46. package/dist/services/document-service/entries.js +42 -0
  47. package/dist/services/document-service/entries.js.map +1 -1
  48. package/dist/services/document-service/entries.mjs +43 -1
  49. package/dist/services/document-service/entries.mjs.map +1 -1
  50. package/dist/services/document-service/internationalization.d.ts +6 -1
  51. package/dist/services/document-service/internationalization.d.ts.map +1 -1
  52. package/dist/services/document-service/internationalization.js +32 -0
  53. package/dist/services/document-service/internationalization.js.map +1 -1
  54. package/dist/services/document-service/internationalization.mjs +32 -1
  55. package/dist/services/document-service/internationalization.mjs.map +1 -1
  56. package/dist/services/document-service/repository.d.ts.map +1 -1
  57. package/dist/services/document-service/repository.js +5 -4
  58. package/dist/services/document-service/repository.js.map +1 -1
  59. package/dist/services/document-service/repository.mjs +6 -5
  60. package/dist/services/document-service/repository.mjs.map +1 -1
  61. package/dist/services/entity-validator/index.d.ts.map +1 -1
  62. package/dist/services/entity-validator/index.js +9 -0
  63. package/dist/services/entity-validator/index.js.map +1 -1
  64. package/dist/services/entity-validator/index.mjs +9 -0
  65. package/dist/services/entity-validator/index.mjs.map +1 -1
  66. package/dist/services/entity-validator/validators.d.ts +1 -0
  67. package/dist/services/entity-validator/validators.d.ts.map +1 -1
  68. package/dist/services/entity-validator/validators.js +3 -0
  69. package/dist/services/entity-validator/validators.js.map +1 -1
  70. package/dist/services/entity-validator/validators.mjs +3 -0
  71. package/dist/services/entity-validator/validators.mjs.map +1 -1
  72. package/dist/services/server/register-routes.js +2 -22
  73. package/dist/services/server/register-routes.js.map +1 -1
  74. package/dist/services/server/register-routes.mjs +2 -22
  75. package/dist/services/server/register-routes.mjs.map +1 -1
  76. package/dist/services/server/routing.d.ts +0 -10
  77. package/dist/services/server/routing.d.ts.map +1 -1
  78. package/dist/services/server/routing.js +1 -7
  79. package/dist/services/server/routing.js.map +1 -1
  80. package/dist/services/server/routing.mjs +1 -7
  81. package/dist/services/server/routing.mjs.map +1 -1
  82. package/dist/services/utils/conditional-fields.d.ts +3 -0
  83. package/dist/services/utils/conditional-fields.d.ts.map +1 -0
  84. package/dist/services/utils/conditional-fields.js +22 -0
  85. package/dist/services/utils/conditional-fields.js.map +1 -0
  86. package/dist/services/utils/conditional-fields.mjs +20 -0
  87. package/dist/services/utils/conditional-fields.mjs.map +1 -0
  88. package/dist/utils/fetch.d.ts +5 -1
  89. package/dist/utils/fetch.d.ts.map +1 -1
  90. package/dist/utils/fetch.js +8 -4
  91. package/dist/utils/fetch.js.map +1 -1
  92. package/dist/utils/fetch.mjs +8 -4
  93. package/dist/utils/fetch.mjs.map +1 -1
  94. package/dist/utils/transform-content-types-to-models.d.ts +197 -0
  95. package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
  96. package/package.json +15 -14
  97. package/dist/core-api/routes/validation/common.d.ts +0 -135
  98. package/dist/core-api/routes/validation/common.d.ts.map +0 -1
  99. package/dist/core-api/routes/validation/common.js +0 -201
  100. package/dist/core-api/routes/validation/common.js.map +0 -1
  101. package/dist/core-api/routes/validation/common.mjs +0 -198
  102. package/dist/core-api/routes/validation/common.mjs.map +0 -1
  103. package/dist/core-api/routes/validation/component.d.ts +0 -42
  104. package/dist/core-api/routes/validation/component.d.ts.map +0 -1
  105. package/dist/core-api/routes/validation/content-type.d.ts +0 -139
  106. package/dist/core-api/routes/validation/content-type.d.ts.map +0 -1
  107. package/dist/core-api/routes/validation/content-type.js +0 -176
  108. package/dist/core-api/routes/validation/content-type.js.map +0 -1
  109. package/dist/core-api/routes/validation/content-type.mjs +0 -174
  110. package/dist/core-api/routes/validation/content-type.mjs.map +0 -1
  111. package/dist/core-api/routes/validation/index.d.ts +0 -4
  112. package/dist/core-api/routes/validation/index.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/core",
3
- "version": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
3
+ "version": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
4
4
  "description": "Core of Strapi",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -56,14 +56,14 @@
56
56
  "@koa/cors": "5.0.0",
57
57
  "@koa/router": "12.0.2",
58
58
  "@paralleldrive/cuid2": "2.2.2",
59
- "@strapi/admin": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
60
- "@strapi/database": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
61
- "@strapi/generators": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
62
- "@strapi/logger": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
63
- "@strapi/permissions": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
64
- "@strapi/types": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
65
- "@strapi/typescript-utils": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
66
- "@strapi/utils": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
59
+ "@strapi/admin": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
60
+ "@strapi/database": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
61
+ "@strapi/generators": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
62
+ "@strapi/logger": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
63
+ "@strapi/permissions": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
64
+ "@strapi/types": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
65
+ "@strapi/typescript-utils": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
66
+ "@strapi/utils": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
67
67
  "bcryptjs": "2.4.3",
68
68
  "boxen": "5.1.2",
69
69
  "chalk": "4.1.2",
@@ -82,7 +82,8 @@
82
82
  "http-errors": "2.0.0",
83
83
  "inquirer": "8.2.5",
84
84
  "is-docker": "2.2.1",
85
- "koa": "2.15.4",
85
+ "json-logic-js": "2.0.5",
86
+ "koa": "2.16.1",
86
87
  "koa-body": "6.0.1",
87
88
  "koa-compose": "4.1.0",
88
89
  "koa-compress": "5.1.1",
@@ -104,8 +105,7 @@
104
105
  "statuses": "2.0.1",
105
106
  "typescript": "5.4.4",
106
107
  "undici": "6.21.2",
107
- "yup": "0.32.9",
108
- "zod": "3.22.4"
108
+ "yup": "0.32.9"
109
109
  },
110
110
  "devDependencies": {
111
111
  "@strapi/ts-zen": "^0.2.0",
@@ -117,6 +117,7 @@
117
117
  "@types/global-agent": "2.1.3",
118
118
  "@types/http-errors": "2.0.4",
119
119
  "@types/jest": "29.5.2",
120
+ "@types/json-logic-js": "2.0.8",
120
121
  "@types/koa": "2.13.4",
121
122
  "@types/koa-compress": "4.0.3",
122
123
  "@types/koa-session": "6.4.1",
@@ -127,9 +128,9 @@
127
128
  "@types/node": "18.19.24",
128
129
  "@types/node-schedule": "2.1.7",
129
130
  "@types/statuses": "2.0.1",
130
- "eslint-config-custom": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064",
131
+ "eslint-config-custom": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744",
131
132
  "supertest": "6.3.3",
132
- "tsconfig": "0.0.0-experimental.25f7de53c1fc990bbb86d4aa43c379acfa838064"
133
+ "tsconfig": "0.0.0-experimental.26b6df1ae17be39bee0a44af52f68ae505b3e744"
133
134
  },
134
135
  "engines": {
135
136
  "node": ">=18.0.0 <=22.x.x",
@@ -1,135 +0,0 @@
1
- import { type Core, Schema, type UID } from '@strapi/types';
2
- import { z } from 'zod';
3
- /**
4
- * AbstractCoreRouteValidator provides the foundation for validating and managing core routes within a Strapi context for a specific model.
5
- *
6
- * This abstract class facilitates validation logic for scalar and populatable fields defined in a model schema.
7
- *
8
- * It uses runtime information about Strapi models to derive and expose schema validations.
9
- *
10
- * @template {UID.Schema} T Representing the schema identifier to be validated.
11
- */
12
- export declare abstract class AbstractCoreRouteValidator<T extends UID.Schema> {
13
- /**
14
- * The `_strapi` variable serves as a reference to the core Strapi instance.
15
- * It is used for interacting with the loaded model.
16
- */
17
- protected readonly _strapi: Core.Strapi;
18
- /**
19
- * A unique identifier used to represent a model within Strapi.
20
- *
21
- * The type of the identifier is generic to allow flexibility and ensure type safety
22
- * when working with either content-types or components.
23
- */
24
- protected readonly _uid: T;
25
- /**
26
- * Protected constructor for initializing the class with the provided Strapi instance and unique identifier (UID).
27
- *
28
- * @param strapi The Strapi instance to be used.
29
- * @param uid The unique identifier for the instance.
30
- */
31
- protected constructor(strapi: Core.Strapi, uid: T);
32
- /**
33
- * Retrieves an enum schema constructed from the keys of the scalar fields.
34
- *
35
- * @return A Zod enum containing the keys from the scalar fields.
36
- */
37
- get scalarFieldsEnum(): z.ZodEnum<[string]>;
38
- /**
39
- * Retrieves an enum schema constructed from the keys of fields that can be populated (relations, components, files, etc.)
40
- *
41
- * @return A ZodEnum object containing the list of populatable field keys.
42
- */
43
- get populatableFieldsEnum(): z.ZodEnum<[string]>;
44
- /**
45
- * Retrieves an array representation of the scalar fields.
46
- *
47
- * @return An array containing the scalar fields as defined by {@link scalarFieldsEnum}.
48
- */
49
- get scalarFieldsArray(): z.ZodArray<z.ZodEnum<[string]>, "many">;
50
- /**
51
- * Retrieves an array of populatable fields.
52
- *
53
- * @return A Zod array schema representing the available populatable fields as defined by {@link populatableFieldsEnum}.
54
- */
55
- get populatableFieldsArray(): z.ZodArray<z.ZodEnum<[string]>, "many">;
56
- /**
57
- * Retrieves the schema associated with the current model.
58
- *
59
- * The schema represents the structural definition of the model,
60
- * as retrieved from the Strapi model associated with the given UID.
61
- *
62
- * @return The schema of the model retrieved from Strapi.
63
- */
64
- protected get _schema(): T extends UID.ContentType ? Schema.ContentType<T> : T extends `${string}.${string}` ? Schema.Component<T> : never;
65
- /**
66
- * Retrieves scalar fields from the object's schema attributes.
67
- *
68
- * Filters the schema attributes to include only those that are scalar and not private.
69
- *
70
- * @return An object composed of scalar fields from the schema attributes.
71
- */
72
- protected get _scalarFields(): {
73
- [k: string]: Schema.Attribute.AnyAttribute;
74
- };
75
- /**
76
- * Retrieves the populatable fields from the schema attributes.
77
- *
78
- * Filters the schema attributes to include only those that are populatable and not private.
79
- *
80
- * @return An object containing the populatable fields derived from the schema attributes.
81
- */
82
- protected get _populatableFields(): {
83
- [k: string]: Schema.Attribute.AnyAttribute;
84
- };
85
- /**
86
- * Creates a Zod schema as a record with scalar fields as keys and the specified type as values.
87
- *
88
- * @param type - The Zod type to use for the record's values.
89
- * @return A Zod record schema with scalar fields as keys and the specified type as values.
90
- */
91
- fieldRecord(type: z.ZodTypeAny): z.ZodRecord<z.ZodEnum<[string]>, z.ZodTypeAny>;
92
- }
93
- /**
94
- * Maps a Strapi attribute definition to a corresponding Zod validation schema.
95
- * This function handles various Strapi attribute types and converts them into
96
- * appropriate Zod validation schemas with their respective constraints.
97
- *
98
- * @param attribute - The Strapi attribute configuration object. Contains type information
99
- * and validation rules for the attribute.
100
- *
101
- * @returns A Zod schema that corresponds to the input attribute's type and validation rules
102
- *
103
- * @example
104
- * ```typescript
105
- * // String attribute with constraints
106
- * const stringAttribute = {
107
- * type: 'string',
108
- * minLength: 3,
109
- * maxLength: 50,
110
- * required: true
111
- * };
112
- * const stringSchema = mapAttributeToSchema(stringAttribute);
113
- *
114
- * // Enumeration attribute
115
- * const enumAttribute = {
116
- * type: 'enumeration',
117
- * enum: ['draft', 'published', 'archived']
118
- * };
119
- * const enumSchema = mapAttributeToSchema(enumAttribute);
120
- *
121
- * // Media attribute with multiple files
122
- * const mediaAttribute = {
123
- * type: 'media',
124
- * multiple: true
125
- * };
126
- * const mediaSchema = mapAttributeToSchema(mediaAttribute);
127
- * ```
128
- *
129
- * @throws {Error} Throws an error if an unsupported attribute type is provided
130
- *
131
- * @remarks
132
- * - Complex types (component, relation, dynamic zone) use placeholder schemas
133
- */
134
- export declare const mapAttributeToSchema: (attribute: Schema.Attribute.AnyAttribute) => z.Schema;
135
- //# sourceMappingURL=common.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../../src/core-api/routes/validation/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,EAAE,KAAK,GAAG,EAAE,MAAM,eAAe,CAAC;AAE5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;GAQG;AACH,8BAAsB,0BAA0B,CAAC,CAAC,SAAS,GAAG,CAAC,MAAM;IACnE;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC;IAExC;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAE3B;;;;;OAKG;IACH,SAAS,aAAa,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC;IAKjD;;;;OAIG;IACH,IAAW,gBAAgB,wBAE1B;IAED;;;;OAIG;IACH,IAAW,qBAAqB,wBAE/B;IAED;;;;OAIG;IACH,IAAW,iBAAiB,4CAE3B;IAED;;;;OAIG;IACH,IAAW,sBAAsB,4CAEhC;IAED;;;;;;;OAOG;IACH,SAAS,KAAK,OAAO,sHAEpB;IAED;;;;;;OAMG;IACH,SAAS,KAAK,aAAa;;MAQ1B;IAED;;;;;;OAMG;IACH,SAAS,KAAK,kBAAkB;;MAQ/B;IAED;;;;;OAKG;IACI,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,UAAU;CAGtC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,oBAAoB,cAAe,OAAO,SAAS,CAAC,YAAY,KAAG,EAAE,MAqEjF,CAAC"}
@@ -1,201 +0,0 @@
1
- 'use strict';
2
-
3
- var strapiUtils = require('@strapi/utils');
4
- var zod = require('zod');
5
-
6
- /**
7
- * AbstractCoreRouteValidator provides the foundation for validating and managing core routes within a Strapi context for a specific model.
8
- *
9
- * This abstract class facilitates validation logic for scalar and populatable fields defined in a model schema.
10
- *
11
- * It uses runtime information about Strapi models to derive and expose schema validations.
12
- *
13
- * @template {UID.Schema} T Representing the schema identifier to be validated.
14
- */ class AbstractCoreRouteValidator {
15
- /**
16
- * Retrieves an enum schema constructed from the keys of the scalar fields.
17
- *
18
- * @return A Zod enum containing the keys from the scalar fields.
19
- */ get scalarFieldsEnum() {
20
- return zod.z.enum(Object.keys(this._scalarFields));
21
- }
22
- /**
23
- * Retrieves an enum schema constructed from the keys of fields that can be populated (relations, components, files, etc.)
24
- *
25
- * @return A ZodEnum object containing the list of populatable field keys.
26
- */ get populatableFieldsEnum() {
27
- return zod.z.enum(Object.keys(this._populatableFields));
28
- }
29
- /**
30
- * Retrieves an array representation of the scalar fields.
31
- *
32
- * @return An array containing the scalar fields as defined by {@link scalarFieldsEnum}.
33
- */ get scalarFieldsArray() {
34
- return zod.z.array(this.scalarFieldsEnum);
35
- }
36
- /**
37
- * Retrieves an array of populatable fields.
38
- *
39
- * @return A Zod array schema representing the available populatable fields as defined by {@link populatableFieldsEnum}.
40
- */ get populatableFieldsArray() {
41
- return zod.z.array(this.populatableFieldsEnum);
42
- }
43
- /**
44
- * Retrieves the schema associated with the current model.
45
- *
46
- * The schema represents the structural definition of the model,
47
- * as retrieved from the Strapi model associated with the given UID.
48
- *
49
- * @return The schema of the model retrieved from Strapi.
50
- */ get _schema() {
51
- return this._strapi.getModel(this._uid);
52
- }
53
- /**
54
- * Retrieves scalar fields from the object's schema attributes.
55
- *
56
- * Filters the schema attributes to include only those that are scalar and not private.
57
- *
58
- * @return An object composed of scalar fields from the schema attributes.
59
- */ get _scalarFields() {
60
- const attributes = Object.entries(this._schema.attributes);
61
- const scalarEntries = attributes.filter(([, attribute])=>strapiUtils.contentTypes.isScalarAttribute(attribute)).filter(([attributeName])=>!strapiUtils.contentTypes.isPrivateAttribute(this._schema, attributeName));
62
- return Object.fromEntries(scalarEntries);
63
- }
64
- /**
65
- * Retrieves the populatable fields from the schema attributes.
66
- *
67
- * Filters the schema attributes to include only those that are populatable and not private.
68
- *
69
- * @return An object containing the populatable fields derived from the schema attributes.
70
- */ get _populatableFields() {
71
- const attributes = Object.entries(this._schema.attributes);
72
- const populatableEntries = attributes.filter(([, attribute])=>!strapiUtils.contentTypes.isScalarAttribute(attribute)).filter(([attributeName])=>!strapiUtils.contentTypes.isPrivateAttribute(this._schema, attributeName));
73
- return Object.fromEntries(populatableEntries);
74
- }
75
- /**
76
- * Creates a Zod schema as a record with scalar fields as keys and the specified type as values.
77
- *
78
- * @param type - The Zod type to use for the record's values.
79
- * @return A Zod record schema with scalar fields as keys and the specified type as values.
80
- */ fieldRecord(type) {
81
- return zod.z.record(this.scalarFieldsEnum, type);
82
- }
83
- /**
84
- * Protected constructor for initializing the class with the provided Strapi instance and unique identifier (UID).
85
- *
86
- * @param strapi The Strapi instance to be used.
87
- * @param uid The unique identifier for the instance.
88
- */ constructor(strapi, uid){
89
- this._strapi = strapi;
90
- this._uid = uid;
91
- }
92
- }
93
- /**
94
- * Maps a Strapi attribute definition to a corresponding Zod validation schema.
95
- * This function handles various Strapi attribute types and converts them into
96
- * appropriate Zod validation schemas with their respective constraints.
97
- *
98
- * @param attribute - The Strapi attribute configuration object. Contains type information
99
- * and validation rules for the attribute.
100
- *
101
- * @returns A Zod schema that corresponds to the input attribute's type and validation rules
102
- *
103
- * @example
104
- * ```typescript
105
- * // String attribute with constraints
106
- * const stringAttribute = {
107
- * type: 'string',
108
- * minLength: 3,
109
- * maxLength: 50,
110
- * required: true
111
- * };
112
- * const stringSchema = mapAttributeToSchema(stringAttribute);
113
- *
114
- * // Enumeration attribute
115
- * const enumAttribute = {
116
- * type: 'enumeration',
117
- * enum: ['draft', 'published', 'archived']
118
- * };
119
- * const enumSchema = mapAttributeToSchema(enumAttribute);
120
- *
121
- * // Media attribute with multiple files
122
- * const mediaAttribute = {
123
- * type: 'media',
124
- * multiple: true
125
- * };
126
- * const mediaSchema = mapAttributeToSchema(mediaAttribute);
127
- * ```
128
- *
129
- * @throws {Error} Throws an error if an unsupported attribute type is provided
130
- *
131
- * @remarks
132
- * - Complex types (component, relation, dynamic zone) use placeholder schemas
133
- */ const mapAttributeToSchema = (attribute)=>{
134
- switch(attribute.type){
135
- case 'string':
136
- case 'text':
137
- case 'richtext':
138
- {
139
- const { minLength, maxLength, required, default: def } = attribute;
140
- return [
141
- zod.z.string()
142
- ].map((schema)=>minLength !== undefined ? schema.min(minLength) : schema).map((schema)=>maxLength !== undefined ? schema.max(maxLength) : schema).map((schema)=>required ? schema : schema.optional()).map((schema)=>{
143
- if (typeof def === 'undefined') {
144
- return schema;
145
- }
146
- // Needed to infer the type and target the correct schema.default() version
147
- return typeof def === 'function' ? schema.default(def) : schema.default(def);
148
- })[0];
149
- }
150
- case 'blocks':
151
- return zod.z.string();
152
- case 'float':
153
- return zod.z.number().describe(`A float field`);
154
- case 'decimal':
155
- return zod.z.number().describe(`A decimal field`);
156
- case 'biginteger':
157
- return zod.z.number().describe(`A biginteger field`);
158
- case 'integer':
159
- return zod.z.number().int().describe(`An integer field`);
160
- case 'boolean':
161
- return zod.z.boolean().describe(`A boolean field`);
162
- case 'date':
163
- case 'datetime':
164
- case 'timestamp':
165
- case 'time':
166
- return zod.z.string().datetime().describe(`A date field`);
167
- case 'email':
168
- return zod.z.string().email().describe(`An email field`);
169
- case 'enumeration':
170
- return zod.z.enum(attribute.enum).describe(`An enumeration field with possible values: ${attribute.enum.join(', ')}`);
171
- case 'component':
172
- {
173
- const schema = zod.z.any().describe('imagine this is a component');
174
- return attribute.repeatable ? zod.z.array(schema) : schema.nullable();
175
- }
176
- case 'relation':
177
- {
178
- const schema = zod.z.any().describe(`A related document`);
179
- return strapiUtils.relations.isAnyToMany(attribute) ? zod.z.array(schema) : schema.nullable();
180
- }
181
- case 'dynamiczone':
182
- return zod.z.array(zod.z.any()).describe('Imagine this is a dynamic zone').nullable();
183
- case 'json':
184
- return zod.z.any().describe(`A JSON field`);
185
- case 'uid':
186
- return zod.z.string().describe(`A unique identifier (UID) field`);
187
- case 'password':
188
- return zod.z.string().describe(`A password field`);
189
- case 'media':
190
- {
191
- const schema = zod.z.any().describe(`A media field`);
192
- return attribute.multiple ? zod.z.array(schema) : schema.nullable();
193
- }
194
- default:
195
- throw new Error(`Unsupported attribute type: ${attribute['type']}`);
196
- }
197
- };
198
-
199
- exports.AbstractCoreRouteValidator = AbstractCoreRouteValidator;
200
- exports.mapAttributeToSchema = mapAttributeToSchema;
201
- //# sourceMappingURL=common.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.js","sources":["../../../../src/core-api/routes/validation/common.ts"],"sourcesContent":["import { type Core, Schema, type UID } from '@strapi/types';\nimport { contentTypes, relations } from '@strapi/utils';\nimport { z } from 'zod';\n\n/**\n * AbstractCoreRouteValidator provides the foundation for validating and managing core routes within a Strapi context for a specific model.\n *\n * This abstract class facilitates validation logic for scalar and populatable fields defined in a model schema.\n *\n * It uses runtime information about Strapi models to derive and expose schema validations.\n *\n * @template {UID.Schema} T Representing the schema identifier to be validated.\n */\nexport abstract class AbstractCoreRouteValidator<T extends UID.Schema> {\n /**\n * The `_strapi` variable serves as a reference to the core Strapi instance.\n * It is used for interacting with the loaded model.\n */\n protected readonly _strapi: Core.Strapi;\n\n /**\n * A unique identifier used to represent a model within Strapi.\n *\n * The type of the identifier is generic to allow flexibility and ensure type safety\n * when working with either content-types or components.\n */\n protected readonly _uid: T;\n\n /**\n * Protected constructor for initializing the class with the provided Strapi instance and unique identifier (UID).\n *\n * @param strapi The Strapi instance to be used.\n * @param uid The unique identifier for the instance.\n */\n protected constructor(strapi: Core.Strapi, uid: T) {\n this._strapi = strapi;\n this._uid = uid;\n }\n\n /**\n * Retrieves an enum schema constructed from the keys of the scalar fields.\n *\n * @return A Zod enum containing the keys from the scalar fields.\n */\n public get scalarFieldsEnum() {\n return z.enum(Object.keys(this._scalarFields) as [string]);\n }\n\n /**\n * Retrieves an enum schema constructed from the keys of fields that can be populated (relations, components, files, etc.)\n *\n * @return A ZodEnum object containing the list of populatable field keys.\n */\n public get populatableFieldsEnum() {\n return z.enum(Object.keys(this._populatableFields) as [string]);\n }\n\n /**\n * Retrieves an array representation of the scalar fields.\n *\n * @return An array containing the scalar fields as defined by {@link scalarFieldsEnum}.\n */\n public get scalarFieldsArray() {\n return z.array(this.scalarFieldsEnum);\n }\n\n /**\n * Retrieves an array of populatable fields.\n *\n * @return A Zod array schema representing the available populatable fields as defined by {@link populatableFieldsEnum}.\n */\n public get populatableFieldsArray() {\n return z.array(this.populatableFieldsEnum);\n }\n\n /**\n * Retrieves the schema associated with the current model.\n *\n * The schema represents the structural definition of the model,\n * as retrieved from the Strapi model associated with the given UID.\n *\n * @return The schema of the model retrieved from Strapi.\n */\n protected get _schema() {\n return this._strapi.getModel(this._uid);\n }\n\n /**\n * Retrieves scalar fields from the object's schema attributes.\n *\n * Filters the schema attributes to include only those that are scalar and not private.\n *\n * @return An object composed of scalar fields from the schema attributes.\n */\n protected get _scalarFields() {\n const attributes = Object.entries(this._schema.attributes);\n\n const scalarEntries = attributes\n .filter(([, attribute]) => contentTypes.isScalarAttribute(attribute))\n .filter(([attributeName]) => !contentTypes.isPrivateAttribute(this._schema, attributeName));\n\n return Object.fromEntries(scalarEntries);\n }\n\n /**\n * Retrieves the populatable fields from the schema attributes.\n *\n * Filters the schema attributes to include only those that are populatable and not private.\n *\n * @return An object containing the populatable fields derived from the schema attributes.\n */\n protected get _populatableFields() {\n const attributes = Object.entries(this._schema.attributes);\n\n const populatableEntries = attributes\n .filter(([, attribute]) => !contentTypes.isScalarAttribute(attribute))\n .filter(([attributeName]) => !contentTypes.isPrivateAttribute(this._schema, attributeName));\n\n return Object.fromEntries(populatableEntries);\n }\n\n /**\n * Creates a Zod schema as a record with scalar fields as keys and the specified type as values.\n *\n * @param type - The Zod type to use for the record's values.\n * @return A Zod record schema with scalar fields as keys and the specified type as values.\n */\n public fieldRecord(type: z.ZodTypeAny) {\n return z.record(this.scalarFieldsEnum, type);\n }\n}\n\n/**\n * Maps a Strapi attribute definition to a corresponding Zod validation schema.\n * This function handles various Strapi attribute types and converts them into\n * appropriate Zod validation schemas with their respective constraints.\n *\n * @param attribute - The Strapi attribute configuration object. Contains type information\n * and validation rules for the attribute.\n *\n * @returns A Zod schema that corresponds to the input attribute's type and validation rules\n *\n * @example\n * ```typescript\n * // String attribute with constraints\n * const stringAttribute = {\n * type: 'string',\n * minLength: 3,\n * maxLength: 50,\n * required: true\n * };\n * const stringSchema = mapAttributeToSchema(stringAttribute);\n *\n * // Enumeration attribute\n * const enumAttribute = {\n * type: 'enumeration',\n * enum: ['draft', 'published', 'archived']\n * };\n * const enumSchema = mapAttributeToSchema(enumAttribute);\n *\n * // Media attribute with multiple files\n * const mediaAttribute = {\n * type: 'media',\n * multiple: true\n * };\n * const mediaSchema = mapAttributeToSchema(mediaAttribute);\n * ```\n *\n * @throws {Error} Throws an error if an unsupported attribute type is provided\n *\n * @remarks\n * - Complex types (component, relation, dynamic zone) use placeholder schemas\n */\nexport const mapAttributeToSchema = (attribute: Schema.Attribute.AnyAttribute): z.Schema => {\n switch (attribute.type) {\n case 'string':\n case 'text':\n case 'richtext': {\n const { minLength, maxLength, required, default: def } = attribute;\n\n return [z.string()]\n .map((schema) => (minLength !== undefined ? schema.min(minLength) : schema))\n .map((schema) => (maxLength !== undefined ? schema.max(maxLength) : schema))\n .map((schema) => (required ? schema : schema.optional()))\n .map((schema) => {\n if (typeof def === 'undefined') {\n return schema;\n }\n\n // Needed to infer the type and target the correct schema.default() version\n return typeof def === 'function' ? schema.default(def) : schema.default(def);\n })[0];\n }\n case 'blocks':\n return z.string();\n case 'float':\n return z.number().describe(`A float field`);\n case 'decimal':\n return z.number().describe(`A decimal field`);\n case 'biginteger':\n return z.number().describe(`A biginteger field`);\n case 'integer':\n return z.number().int().describe(`An integer field`);\n case 'boolean':\n return z.boolean().describe(`A boolean field`);\n case 'date':\n case 'datetime':\n case 'timestamp':\n case 'time':\n return z.string().datetime().describe(`A date field`);\n case 'email':\n return z.string().email().describe(`An email field`);\n case 'enumeration':\n return z\n .enum(attribute.enum as [string])\n .describe(`An enumeration field with possible values: ${attribute.enum.join(', ')}`);\n case 'component': {\n const schema = z.any().describe('imagine this is a component');\n\n return attribute.repeatable ? z.array(schema) : schema.nullable();\n }\n case 'relation': {\n const schema = z.any().describe(`A related document`);\n\n return relations.isAnyToMany(attribute) ? z.array(schema) : schema.nullable();\n }\n case 'dynamiczone':\n return z.array(z.any()).describe('Imagine this is a dynamic zone').nullable();\n case 'json':\n return z.any().describe(`A JSON field`);\n case 'uid':\n return z.string().describe(`A unique identifier (UID) field`);\n case 'password':\n return z.string().describe(`A password field`);\n case 'media': {\n const schema = z.any().describe(`A media field`);\n\n return attribute.multiple ? z.array(schema) : schema.nullable();\n }\n default:\n throw new Error(`Unsupported attribute type: ${attribute['type']}`);\n }\n};\n"],"names":["AbstractCoreRouteValidator","scalarFieldsEnum","z","enum","Object","keys","_scalarFields","populatableFieldsEnum","_populatableFields","scalarFieldsArray","array","populatableFieldsArray","_schema","_strapi","getModel","_uid","attributes","entries","scalarEntries","filter","attribute","contentTypes","isScalarAttribute","attributeName","isPrivateAttribute","fromEntries","populatableEntries","fieldRecord","type","record","strapi","uid","mapAttributeToSchema","minLength","maxLength","required","default","def","string","map","schema","undefined","min","max","optional","number","describe","int","boolean","datetime","email","join","any","repeatable","nullable","relations","isAnyToMany","multiple","Error"],"mappings":";;;;;AAI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OAAO5C,MAAEoC,MAAM,EAAA,CAAGQ,QAAQ,CAAC,CAAC,gBAAgB,CAAC,CAAA;QAC/C,KAAK,OAAA;AAAS,YAAA;gBACZ,MAAMN,MAAAA,GAAStC,MAAEkD,GAAG,EAAA,CAAGN,QAAQ,CAAC,CAAC,aAAa,CAAC,CAAA;gBAE/C,OAAO1B,SAAAA,CAAUqC,QAAQ,GAAGvD,KAAAA,CAAEQ,KAAK,CAAC8B,MAAAA,CAAAA,GAAUA,OAAOc,QAAQ,EAAA;AAC/D;AACA,QAAA;YACE,MAAM,IAAII,MAAM,CAAC,4BAA4B,EAAEtC,SAAS,CAAC,MAAO,CAAA,CAAC,CAAC,CAAA;AACtE;AACF;;;;;"}
@@ -1,198 +0,0 @@
1
- import { contentTypes, relations } from '@strapi/utils';
2
- import { z } from 'zod';
3
-
4
- /**
5
- * AbstractCoreRouteValidator provides the foundation for validating and managing core routes within a Strapi context for a specific model.
6
- *
7
- * This abstract class facilitates validation logic for scalar and populatable fields defined in a model schema.
8
- *
9
- * It uses runtime information about Strapi models to derive and expose schema validations.
10
- *
11
- * @template {UID.Schema} T Representing the schema identifier to be validated.
12
- */ class AbstractCoreRouteValidator {
13
- /**
14
- * Retrieves an enum schema constructed from the keys of the scalar fields.
15
- *
16
- * @return A Zod enum containing the keys from the scalar fields.
17
- */ get scalarFieldsEnum() {
18
- return z.enum(Object.keys(this._scalarFields));
19
- }
20
- /**
21
- * Retrieves an enum schema constructed from the keys of fields that can be populated (relations, components, files, etc.)
22
- *
23
- * @return A ZodEnum object containing the list of populatable field keys.
24
- */ get populatableFieldsEnum() {
25
- return z.enum(Object.keys(this._populatableFields));
26
- }
27
- /**
28
- * Retrieves an array representation of the scalar fields.
29
- *
30
- * @return An array containing the scalar fields as defined by {@link scalarFieldsEnum}.
31
- */ get scalarFieldsArray() {
32
- return z.array(this.scalarFieldsEnum);
33
- }
34
- /**
35
- * Retrieves an array of populatable fields.
36
- *
37
- * @return A Zod array schema representing the available populatable fields as defined by {@link populatableFieldsEnum}.
38
- */ get populatableFieldsArray() {
39
- return z.array(this.populatableFieldsEnum);
40
- }
41
- /**
42
- * Retrieves the schema associated with the current model.
43
- *
44
- * The schema represents the structural definition of the model,
45
- * as retrieved from the Strapi model associated with the given UID.
46
- *
47
- * @return The schema of the model retrieved from Strapi.
48
- */ get _schema() {
49
- return this._strapi.getModel(this._uid);
50
- }
51
- /**
52
- * Retrieves scalar fields from the object's schema attributes.
53
- *
54
- * Filters the schema attributes to include only those that are scalar and not private.
55
- *
56
- * @return An object composed of scalar fields from the schema attributes.
57
- */ get _scalarFields() {
58
- const attributes = Object.entries(this._schema.attributes);
59
- const scalarEntries = attributes.filter(([, attribute])=>contentTypes.isScalarAttribute(attribute)).filter(([attributeName])=>!contentTypes.isPrivateAttribute(this._schema, attributeName));
60
- return Object.fromEntries(scalarEntries);
61
- }
62
- /**
63
- * Retrieves the populatable fields from the schema attributes.
64
- *
65
- * Filters the schema attributes to include only those that are populatable and not private.
66
- *
67
- * @return An object containing the populatable fields derived from the schema attributes.
68
- */ get _populatableFields() {
69
- const attributes = Object.entries(this._schema.attributes);
70
- const populatableEntries = attributes.filter(([, attribute])=>!contentTypes.isScalarAttribute(attribute)).filter(([attributeName])=>!contentTypes.isPrivateAttribute(this._schema, attributeName));
71
- return Object.fromEntries(populatableEntries);
72
- }
73
- /**
74
- * Creates a Zod schema as a record with scalar fields as keys and the specified type as values.
75
- *
76
- * @param type - The Zod type to use for the record's values.
77
- * @return A Zod record schema with scalar fields as keys and the specified type as values.
78
- */ fieldRecord(type) {
79
- return z.record(this.scalarFieldsEnum, type);
80
- }
81
- /**
82
- * Protected constructor for initializing the class with the provided Strapi instance and unique identifier (UID).
83
- *
84
- * @param strapi The Strapi instance to be used.
85
- * @param uid The unique identifier for the instance.
86
- */ constructor(strapi, uid){
87
- this._strapi = strapi;
88
- this._uid = uid;
89
- }
90
- }
91
- /**
92
- * Maps a Strapi attribute definition to a corresponding Zod validation schema.
93
- * This function handles various Strapi attribute types and converts them into
94
- * appropriate Zod validation schemas with their respective constraints.
95
- *
96
- * @param attribute - The Strapi attribute configuration object. Contains type information
97
- * and validation rules for the attribute.
98
- *
99
- * @returns A Zod schema that corresponds to the input attribute's type and validation rules
100
- *
101
- * @example
102
- * ```typescript
103
- * // String attribute with constraints
104
- * const stringAttribute = {
105
- * type: 'string',
106
- * minLength: 3,
107
- * maxLength: 50,
108
- * required: true
109
- * };
110
- * const stringSchema = mapAttributeToSchema(stringAttribute);
111
- *
112
- * // Enumeration attribute
113
- * const enumAttribute = {
114
- * type: 'enumeration',
115
- * enum: ['draft', 'published', 'archived']
116
- * };
117
- * const enumSchema = mapAttributeToSchema(enumAttribute);
118
- *
119
- * // Media attribute with multiple files
120
- * const mediaAttribute = {
121
- * type: 'media',
122
- * multiple: true
123
- * };
124
- * const mediaSchema = mapAttributeToSchema(mediaAttribute);
125
- * ```
126
- *
127
- * @throws {Error} Throws an error if an unsupported attribute type is provided
128
- *
129
- * @remarks
130
- * - Complex types (component, relation, dynamic zone) use placeholder schemas
131
- */ const mapAttributeToSchema = (attribute)=>{
132
- switch(attribute.type){
133
- case 'string':
134
- case 'text':
135
- case 'richtext':
136
- {
137
- const { minLength, maxLength, required, default: def } = attribute;
138
- return [
139
- z.string()
140
- ].map((schema)=>minLength !== undefined ? schema.min(minLength) : schema).map((schema)=>maxLength !== undefined ? schema.max(maxLength) : schema).map((schema)=>required ? schema : schema.optional()).map((schema)=>{
141
- if (typeof def === 'undefined') {
142
- return schema;
143
- }
144
- // Needed to infer the type and target the correct schema.default() version
145
- return typeof def === 'function' ? schema.default(def) : schema.default(def);
146
- })[0];
147
- }
148
- case 'blocks':
149
- return z.string();
150
- case 'float':
151
- return z.number().describe(`A float field`);
152
- case 'decimal':
153
- return z.number().describe(`A decimal field`);
154
- case 'biginteger':
155
- return z.number().describe(`A biginteger field`);
156
- case 'integer':
157
- return z.number().int().describe(`An integer field`);
158
- case 'boolean':
159
- return z.boolean().describe(`A boolean field`);
160
- case 'date':
161
- case 'datetime':
162
- case 'timestamp':
163
- case 'time':
164
- return z.string().datetime().describe(`A date field`);
165
- case 'email':
166
- return z.string().email().describe(`An email field`);
167
- case 'enumeration':
168
- return z.enum(attribute.enum).describe(`An enumeration field with possible values: ${attribute.enum.join(', ')}`);
169
- case 'component':
170
- {
171
- const schema = z.any().describe('imagine this is a component');
172
- return attribute.repeatable ? z.array(schema) : schema.nullable();
173
- }
174
- case 'relation':
175
- {
176
- const schema = z.any().describe(`A related document`);
177
- return relations.isAnyToMany(attribute) ? z.array(schema) : schema.nullable();
178
- }
179
- case 'dynamiczone':
180
- return z.array(z.any()).describe('Imagine this is a dynamic zone').nullable();
181
- case 'json':
182
- return z.any().describe(`A JSON field`);
183
- case 'uid':
184
- return z.string().describe(`A unique identifier (UID) field`);
185
- case 'password':
186
- return z.string().describe(`A password field`);
187
- case 'media':
188
- {
189
- const schema = z.any().describe(`A media field`);
190
- return attribute.multiple ? z.array(schema) : schema.nullable();
191
- }
192
- default:
193
- throw new Error(`Unsupported attribute type: ${attribute['type']}`);
194
- }
195
- };
196
-
197
- export { AbstractCoreRouteValidator, mapAttributeToSchema };
198
- //# sourceMappingURL=common.mjs.map