@strapi/core 5.19.0 → 5.21.0
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/core-api/routes/index.d.ts +4 -22
- package/dist/core-api/routes/index.d.ts.map +1 -1
- package/dist/core-api/routes/index.js +150 -8
- package/dist/core-api/routes/index.js.map +1 -1
- package/dist/core-api/routes/index.mjs +131 -8
- package/dist/core-api/routes/index.mjs.map +1 -1
- package/dist/core-api/routes/validation/attributes.d.ts +244 -0
- package/dist/core-api/routes/validation/attributes.d.ts.map +1 -0
- package/dist/core-api/routes/validation/attributes.js +560 -0
- package/dist/core-api/routes/validation/attributes.js.map +1 -0
- package/dist/core-api/routes/validation/attributes.mjs +521 -0
- package/dist/core-api/routes/validation/attributes.mjs.map +1 -0
- package/dist/core-api/routes/validation/common.d.ts +105 -0
- package/dist/core-api/routes/validation/common.d.ts.map +1 -0
- package/dist/core-api/routes/validation/common.js +116 -0
- package/dist/core-api/routes/validation/common.js.map +1 -0
- package/dist/core-api/routes/validation/common.mjs +95 -0
- package/dist/core-api/routes/validation/common.mjs.map +1 -0
- package/dist/core-api/routes/validation/component.d.ts +34 -0
- package/dist/core-api/routes/validation/component.d.ts.map +1 -0
- package/dist/core-api/routes/validation/component.js +45 -0
- package/dist/core-api/routes/validation/component.js.map +1 -0
- package/dist/core-api/routes/validation/component.mjs +43 -0
- package/dist/core-api/routes/validation/component.mjs.map +1 -0
- package/dist/core-api/routes/validation/constants.d.ts +8 -0
- package/dist/core-api/routes/validation/constants.d.ts.map +1 -0
- package/dist/core-api/routes/validation/constants.js +18 -0
- package/dist/core-api/routes/validation/constants.js.map +1 -0
- package/dist/core-api/routes/validation/constants.mjs +16 -0
- package/dist/core-api/routes/validation/constants.mjs.map +1 -0
- package/dist/core-api/routes/validation/content-type.d.ts +128 -0
- package/dist/core-api/routes/validation/content-type.d.ts.map +1 -0
- package/dist/core-api/routes/validation/content-type.js +201 -0
- package/dist/core-api/routes/validation/content-type.js.map +1 -0
- package/dist/core-api/routes/validation/content-type.mjs +180 -0
- package/dist/core-api/routes/validation/content-type.mjs.map +1 -0
- package/dist/core-api/routes/validation/index.d.ts +5 -0
- package/dist/core-api/routes/validation/index.d.ts.map +1 -0
- package/dist/core-api/routes/validation/mappers.d.ts +105 -0
- package/dist/core-api/routes/validation/mappers.d.ts.map +1 -0
- package/dist/core-api/routes/validation/mappers.js +238 -0
- package/dist/core-api/routes/validation/mappers.js.map +1 -0
- package/dist/core-api/routes/validation/mappers.mjs +214 -0
- package/dist/core-api/routes/validation/mappers.mjs.map +1 -0
- package/dist/core-api/routes/validation/utils.d.ts +47 -0
- package/dist/core-api/routes/validation/utils.d.ts.map +1 -0
- package/dist/core-api/routes/validation/utils.js +112 -0
- package/dist/core-api/routes/validation/utils.js.map +1 -0
- package/dist/core-api/routes/validation/utils.mjs +90 -0
- package/dist/core-api/routes/validation/utils.mjs.map +1 -0
- package/dist/domain/content-type/index.d.ts.map +1 -1
- package/dist/domain/content-type/index.js +17 -1
- package/dist/domain/content-type/index.js.map +1 -1
- package/dist/domain/content-type/index.mjs +17 -1
- package/dist/domain/content-type/index.mjs.map +1 -1
- package/dist/domain/module/index.d.ts.map +1 -1
- package/dist/domain/module/index.js +3 -0
- package/dist/domain/module/index.js.map +1 -1
- package/dist/domain/module/index.mjs +3 -0
- package/dist/domain/module/index.mjs.map +1 -1
- package/dist/factories.d.ts +3 -1
- package/dist/factories.d.ts.map +1 -1
- package/dist/factories.js +10 -2
- package/dist/factories.js.map +1 -1
- package/dist/factories.mjs +10 -3
- package/dist/factories.mjs.map +1 -1
- package/dist/middlewares/cors.d.ts +9 -1
- package/dist/middlewares/cors.d.ts.map +1 -1
- package/dist/middlewares/cors.js +39 -17
- package/dist/middlewares/cors.js.map +1 -1
- package/dist/middlewares/cors.mjs +39 -18
- package/dist/middlewares/cors.mjs.map +1 -1
- package/dist/migrations/first-published-at.d.ts +4 -0
- package/dist/migrations/first-published-at.d.ts.map +1 -0
- package/dist/migrations/first-published-at.js +51 -0
- package/dist/migrations/first-published-at.js.map +1 -0
- package/dist/migrations/first-published-at.mjs +49 -0
- package/dist/migrations/first-published-at.mjs.map +1 -0
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/index.js +5 -0
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +5 -0
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/package.json.js +13 -12
- package/dist/package.json.js.map +1 -1
- package/dist/package.json.mjs +13 -12
- package/dist/package.json.mjs.map +1 -1
- package/dist/services/document-service/first-published-at.d.ts +7 -0
- package/dist/services/document-service/first-published-at.d.ts.map +1 -0
- package/dist/services/document-service/first-published-at.js +31 -0
- package/dist/services/document-service/first-published-at.js.map +1 -0
- package/dist/services/document-service/first-published-at.mjs +28 -0
- package/dist/services/document-service/first-published-at.mjs.map +1 -0
- package/dist/services/document-service/repository.d.ts.map +1 -1
- package/dist/services/document-service/repository.js +7 -4
- package/dist/services/document-service/repository.js.map +1 -1
- package/dist/services/document-service/repository.mjs +7 -4
- package/dist/services/document-service/repository.mjs.map +1 -1
- package/dist/services/metrics/index.d.ts +1 -1
- package/dist/services/metrics/index.d.ts.map +1 -1
- package/dist/services/metrics/index.js +9 -8
- package/dist/services/metrics/index.js.map +1 -1
- package/dist/services/metrics/index.mjs +9 -8
- package/dist/services/metrics/index.mjs.map +1 -1
- package/dist/services/server/register-routes.js +22 -2
- package/dist/services/server/register-routes.js.map +1 -1
- package/dist/services/server/register-routes.mjs +22 -2
- package/dist/services/server/register-routes.mjs.map +1 -1
- package/dist/services/server/routing.d.ts +10 -0
- package/dist/services/server/routing.d.ts.map +1 -1
- package/dist/services/server/routing.js +7 -1
- package/dist/services/server/routing.js.map +1 -1
- package/dist/services/server/routing.mjs +7 -1
- package/dist/services/server/routing.mjs.map +1 -1
- package/package.json +13 -12
@@ -0,0 +1,105 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview
|
3
|
+
* This file contains functions responsible for mapping Strapi attribute definitions to Zod schemas.
|
4
|
+
*/
|
5
|
+
import type { Schema } from '@strapi/types';
|
6
|
+
import * as z from 'zod/v4';
|
7
|
+
/**
|
8
|
+
* Creates a Zod schema for a collection of Strapi attributes.
|
9
|
+
*
|
10
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
11
|
+
* @returns A Zod object schema representing the combined attributes.
|
12
|
+
*
|
13
|
+
* @example
|
14
|
+
* ```typescript
|
15
|
+
* const myAttributes = [
|
16
|
+
* ['title', { type: 'string', required: true }],
|
17
|
+
* ['description', { type: 'text' }],
|
18
|
+
* ];
|
19
|
+
* const schema = createAttributesSchema(myAttributes);
|
20
|
+
* // schema will be a Zod object with 'title' and 'description' fields,
|
21
|
+
* // each mapped to their respective Zod schemas based on their Strapi attribute types.
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare const createAttributesSchema: (attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]) => z.ZodObject<{}, z.core.$strip>;
|
25
|
+
/**
|
26
|
+
* Creates a Zod input schema for a collection of Strapi attributes.
|
27
|
+
* This is typically used for validating incoming data (e.g., from API requests).
|
28
|
+
*
|
29
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
30
|
+
* @returns A Zod object schema representing the combined input attributes.
|
31
|
+
*
|
32
|
+
* @example
|
33
|
+
* ```typescript
|
34
|
+
* const myInputAttributes = [
|
35
|
+
* ['email', { type: 'email', required: true }],
|
36
|
+
* ['description', { type: 'text', minLength: 8 }],
|
37
|
+
* ];
|
38
|
+
* const inputSchema = createAttributesInputSchema(myInputAttributes);
|
39
|
+
* // inputSchema will be a Zod object with 'email' and 'description' fields,
|
40
|
+
* // mapped to Zod schemas suitable for input validation.
|
41
|
+
* ```
|
42
|
+
*/
|
43
|
+
export declare const createAttributesInputSchema: (attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]) => z.ZodObject<{}, z.core.$strip>;
|
44
|
+
/**
|
45
|
+
* Maps a Strapi attribute definition to a corresponding Zod validation schema.
|
46
|
+
*
|
47
|
+
* This function handles every Strapi attribute types and converts them into
|
48
|
+
* appropriate Zod validation schemas.
|
49
|
+
*
|
50
|
+
* @param attribute - The Strapi attribute configuration object.
|
51
|
+
* @returns A Zod schema that corresponds to the input attribute's type.
|
52
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided.
|
53
|
+
*
|
54
|
+
* @example
|
55
|
+
* ```typescript
|
56
|
+
* const stringAttribute = { type: 'string', minLength: 3 };
|
57
|
+
* const stringSchema = mapAttributeToSchema(stringAttribute); // Returns a Zod string schema with minLength.
|
58
|
+
*
|
59
|
+
* const booleanAttribute = { type: 'boolean', default: false };
|
60
|
+
* const booleanSchema = mapAttributeToSchema(booleanAttribute); // Returns a Zod boolean schema with a default.
|
61
|
+
* ```
|
62
|
+
*/
|
63
|
+
export declare const mapAttributeToSchema: (attribute: Schema.Attribute.AnyAttribute) => z.ZodTypeAny;
|
64
|
+
/**
|
65
|
+
* Maps a Strapi attribute definition to a corresponding Zod input validation schema.
|
66
|
+
*
|
67
|
+
* This function handles every Strapi attribute types and converts them into
|
68
|
+
* appropriate Zod validation schemas with their respective constraints.
|
69
|
+
*
|
70
|
+
* @param attribute - The Strapi attribute configuration object. Contains type information
|
71
|
+
* and validation rules for the attribute.
|
72
|
+
*
|
73
|
+
* @returns A Zod schema that corresponds to the input attribute's type and validation rules
|
74
|
+
*
|
75
|
+
* @example
|
76
|
+
* ```typescript
|
77
|
+
* // String attribute with constraints
|
78
|
+
* const stringAttribute = {
|
79
|
+
* type: 'string',
|
80
|
+
* minLength: 3,
|
81
|
+
* maxLength: 50,
|
82
|
+
* required: true
|
83
|
+
* };
|
84
|
+
* const stringSchema = mapAttributeToInputSchema(stringAttribute);
|
85
|
+
*
|
86
|
+
* // Enumeration attribute
|
87
|
+
* const enumAttribute = {
|
88
|
+
* type: 'enumeration',
|
89
|
+
* enum: ['draft', 'published', 'archived']
|
90
|
+
* };
|
91
|
+
* const enumSchema = mapAttributeToInputSchema(enumAttribute);
|
92
|
+
*
|
93
|
+
* // Media attribute with multiple files
|
94
|
+
* const mediaAttribute = {
|
95
|
+
* type: 'media',
|
96
|
+
* multiple: true
|
97
|
+
* };
|
98
|
+
* const mediaSchema = mapAttributeToInputSchema(mediaAttribute);
|
99
|
+
* ```
|
100
|
+
*
|
101
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided
|
102
|
+
*
|
103
|
+
*/
|
104
|
+
export declare const mapAttributeToInputSchema: (attribute: Schema.Attribute.AnyAttribute) => z.ZodTypeAny;
|
105
|
+
//# sourceMappingURL=mappers.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mappers.d.ts","sourceRoot":"","sources":["../../../../src/core-api/routes/validation/mappers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAK5B;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,sBAAsB,eACrB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,SAAS,CAAC,YAAY,CAAC,EAAE,mCASvE,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,2BAA2B,eAC1B,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,SAAS,CAAC,YAAY,CAAC,EAAE,mCASvE,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,oBAAoB,cAAe,OAAO,SAAS,CAAC,YAAY,KAAG,EAAE,UAiDjF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,eAAO,MAAM,yBAAyB,cACzB,OAAO,SAAS,CAAC,YAAY,KACvC,EAAE,UAiDJ,CAAC"}
|
@@ -0,0 +1,238 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var z = require('zod/v4');
|
4
|
+
var attributes = require('./attributes.js');
|
5
|
+
|
6
|
+
function _interopNamespaceDefault(e) {
|
7
|
+
var n = Object.create(null);
|
8
|
+
if (e) {
|
9
|
+
Object.keys(e).forEach(function (k) {
|
10
|
+
if (k !== 'default') {
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
13
|
+
enumerable: true,
|
14
|
+
get: function () { return e[k]; }
|
15
|
+
});
|
16
|
+
}
|
17
|
+
});
|
18
|
+
}
|
19
|
+
n.default = e;
|
20
|
+
return Object.freeze(n);
|
21
|
+
}
|
22
|
+
|
23
|
+
var z__namespace = /*#__PURE__*/_interopNamespaceDefault(z);
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Creates a Zod schema for a collection of Strapi attributes.
|
27
|
+
*
|
28
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
29
|
+
* @returns A Zod object schema representing the combined attributes.
|
30
|
+
*
|
31
|
+
* @example
|
32
|
+
* ```typescript
|
33
|
+
* const myAttributes = [
|
34
|
+
* ['title', { type: 'string', required: true }],
|
35
|
+
* ['description', { type: 'text' }],
|
36
|
+
* ];
|
37
|
+
* const schema = createAttributesSchema(myAttributes);
|
38
|
+
* // schema will be a Zod object with 'title' and 'description' fields,
|
39
|
+
* // each mapped to their respective Zod schemas based on their Strapi attribute types.
|
40
|
+
* ```
|
41
|
+
*/ const createAttributesSchema = (attributes)=>{
|
42
|
+
return attributes.reduce((acc, [name, attribute])=>{
|
43
|
+
return acc.extend({
|
44
|
+
get [name] () {
|
45
|
+
return mapAttributeToSchema(attribute);
|
46
|
+
}
|
47
|
+
});
|
48
|
+
}, z__namespace.object({}));
|
49
|
+
};
|
50
|
+
/**
|
51
|
+
* Creates a Zod input schema for a collection of Strapi attributes.
|
52
|
+
* This is typically used for validating incoming data (e.g., from API requests).
|
53
|
+
*
|
54
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
55
|
+
* @returns A Zod object schema representing the combined input attributes.
|
56
|
+
*
|
57
|
+
* @example
|
58
|
+
* ```typescript
|
59
|
+
* const myInputAttributes = [
|
60
|
+
* ['email', { type: 'email', required: true }],
|
61
|
+
* ['description', { type: 'text', minLength: 8 }],
|
62
|
+
* ];
|
63
|
+
* const inputSchema = createAttributesInputSchema(myInputAttributes);
|
64
|
+
* // inputSchema will be a Zod object with 'email' and 'description' fields,
|
65
|
+
* // mapped to Zod schemas suitable for input validation.
|
66
|
+
* ```
|
67
|
+
*/ const createAttributesInputSchema = (attributes)=>{
|
68
|
+
return attributes.reduce((acc, [name, attribute])=>{
|
69
|
+
return acc.extend({
|
70
|
+
get [name] () {
|
71
|
+
return mapAttributeToInputSchema(attribute);
|
72
|
+
}
|
73
|
+
});
|
74
|
+
}, z__namespace.object({}));
|
75
|
+
};
|
76
|
+
/**
|
77
|
+
* Maps a Strapi attribute definition to a corresponding Zod validation schema.
|
78
|
+
*
|
79
|
+
* This function handles every Strapi attribute types and converts them into
|
80
|
+
* appropriate Zod validation schemas.
|
81
|
+
*
|
82
|
+
* @param attribute - The Strapi attribute configuration object.
|
83
|
+
* @returns A Zod schema that corresponds to the input attribute's type.
|
84
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided.
|
85
|
+
*
|
86
|
+
* @example
|
87
|
+
* ```typescript
|
88
|
+
* const stringAttribute = { type: 'string', minLength: 3 };
|
89
|
+
* const stringSchema = mapAttributeToSchema(stringAttribute); // Returns a Zod string schema with minLength.
|
90
|
+
*
|
91
|
+
* const booleanAttribute = { type: 'boolean', default: false };
|
92
|
+
* const booleanSchema = mapAttributeToSchema(booleanAttribute); // Returns a Zod boolean schema with a default.
|
93
|
+
* ```
|
94
|
+
*/ const mapAttributeToSchema = (attribute)=>{
|
95
|
+
switch(attribute.type){
|
96
|
+
case 'biginteger':
|
97
|
+
return attributes.bigIntegerToSchema(attribute);
|
98
|
+
case 'blocks':
|
99
|
+
return attributes.blocksToSchema();
|
100
|
+
case 'boolean':
|
101
|
+
return attributes.booleanToSchema(attribute);
|
102
|
+
case 'component':
|
103
|
+
return attributes.componentToSchema(attribute);
|
104
|
+
case 'date':
|
105
|
+
return attributes.dateToSchema(attribute);
|
106
|
+
case 'datetime':
|
107
|
+
return attributes.datetimeToSchema(attribute);
|
108
|
+
case 'decimal':
|
109
|
+
return attributes.decimalToSchema(attribute);
|
110
|
+
case 'dynamiczone':
|
111
|
+
return attributes.dynamicZoneToSchema(attribute);
|
112
|
+
case 'email':
|
113
|
+
return attributes.emailToSchema(attribute);
|
114
|
+
case 'enumeration':
|
115
|
+
return attributes.enumToSchema(attribute);
|
116
|
+
case 'float':
|
117
|
+
return attributes.floatToSchema(attribute);
|
118
|
+
case 'integer':
|
119
|
+
return attributes.integerToSchema(attribute);
|
120
|
+
case 'json':
|
121
|
+
return attributes.jsonToSchema(attribute);
|
122
|
+
case 'media':
|
123
|
+
return attributes.mediaToSchema(attribute);
|
124
|
+
case 'relation':
|
125
|
+
return attributes.relationToSchema(attribute);
|
126
|
+
case 'password':
|
127
|
+
case 'text':
|
128
|
+
case 'richtext':
|
129
|
+
case 'string':
|
130
|
+
return attributes.stringToSchema(attribute);
|
131
|
+
case 'time':
|
132
|
+
return attributes.timeToSchema(attribute);
|
133
|
+
case 'timestamp':
|
134
|
+
return attributes.timestampToSchema(attribute);
|
135
|
+
case 'uid':
|
136
|
+
return attributes.uidToSchema(attribute);
|
137
|
+
default:
|
138
|
+
{
|
139
|
+
const { type } = attribute;
|
140
|
+
throw new Error(`Unsupported attribute type: ${type}`);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
};
|
144
|
+
/**
|
145
|
+
* Maps a Strapi attribute definition to a corresponding Zod input validation schema.
|
146
|
+
*
|
147
|
+
* This function handles every Strapi attribute types and converts them into
|
148
|
+
* appropriate Zod validation schemas with their respective constraints.
|
149
|
+
*
|
150
|
+
* @param attribute - The Strapi attribute configuration object. Contains type information
|
151
|
+
* and validation rules for the attribute.
|
152
|
+
*
|
153
|
+
* @returns A Zod schema that corresponds to the input attribute's type and validation rules
|
154
|
+
*
|
155
|
+
* @example
|
156
|
+
* ```typescript
|
157
|
+
* // String attribute with constraints
|
158
|
+
* const stringAttribute = {
|
159
|
+
* type: 'string',
|
160
|
+
* minLength: 3,
|
161
|
+
* maxLength: 50,
|
162
|
+
* required: true
|
163
|
+
* };
|
164
|
+
* const stringSchema = mapAttributeToInputSchema(stringAttribute);
|
165
|
+
*
|
166
|
+
* // Enumeration attribute
|
167
|
+
* const enumAttribute = {
|
168
|
+
* type: 'enumeration',
|
169
|
+
* enum: ['draft', 'published', 'archived']
|
170
|
+
* };
|
171
|
+
* const enumSchema = mapAttributeToInputSchema(enumAttribute);
|
172
|
+
*
|
173
|
+
* // Media attribute with multiple files
|
174
|
+
* const mediaAttribute = {
|
175
|
+
* type: 'media',
|
176
|
+
* multiple: true
|
177
|
+
* };
|
178
|
+
* const mediaSchema = mapAttributeToInputSchema(mediaAttribute);
|
179
|
+
* ```
|
180
|
+
*
|
181
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided
|
182
|
+
*
|
183
|
+
*/ const mapAttributeToInputSchema = (attribute)=>{
|
184
|
+
switch(attribute.type){
|
185
|
+
case 'biginteger':
|
186
|
+
return attributes.bigIntegerToInputSchema(attribute);
|
187
|
+
case 'blocks':
|
188
|
+
return attributes.blocksToInputSchema();
|
189
|
+
case 'boolean':
|
190
|
+
return attributes.booleanToInputSchema(attribute);
|
191
|
+
case 'component':
|
192
|
+
return attributes.componentToInputSchema(attribute);
|
193
|
+
case 'date':
|
194
|
+
return attributes.dateToInputSchema(attribute);
|
195
|
+
case 'datetime':
|
196
|
+
return attributes.datetimeToInputSchema(attribute);
|
197
|
+
case 'decimal':
|
198
|
+
return attributes.decimalToInputSchema(attribute);
|
199
|
+
case 'dynamiczone':
|
200
|
+
return attributes.dynamicZoneToInputSchema(attribute);
|
201
|
+
case 'email':
|
202
|
+
return attributes.emailToInputSchema(attribute);
|
203
|
+
case 'enumeration':
|
204
|
+
return attributes.enumerationToInputSchema(attribute);
|
205
|
+
case 'float':
|
206
|
+
return attributes.floatToInputSchema(attribute);
|
207
|
+
case 'integer':
|
208
|
+
return attributes.integerToInputSchema(attribute);
|
209
|
+
case 'json':
|
210
|
+
return attributes.jsonToInputSchema(attribute);
|
211
|
+
case 'media':
|
212
|
+
return attributes.mediaToInputSchema(attribute);
|
213
|
+
case 'relation':
|
214
|
+
return attributes.relationToInputSchema(attribute);
|
215
|
+
case 'password':
|
216
|
+
case 'text':
|
217
|
+
case 'richtext':
|
218
|
+
case 'string':
|
219
|
+
return attributes.textToInputSchema(attribute);
|
220
|
+
case 'time':
|
221
|
+
return attributes.timeToInputSchema(attribute);
|
222
|
+
case 'timestamp':
|
223
|
+
return attributes.timestampToInputSchema(attribute);
|
224
|
+
case 'uid':
|
225
|
+
return attributes.uidToInputSchema(attribute);
|
226
|
+
default:
|
227
|
+
{
|
228
|
+
const { type } = attribute;
|
229
|
+
throw new Error(`Unsupported attribute type: ${type}`);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
};
|
233
|
+
|
234
|
+
exports.createAttributesInputSchema = createAttributesInputSchema;
|
235
|
+
exports.createAttributesSchema = createAttributesSchema;
|
236
|
+
exports.mapAttributeToInputSchema = mapAttributeToInputSchema;
|
237
|
+
exports.mapAttributeToSchema = mapAttributeToSchema;
|
238
|
+
//# sourceMappingURL=mappers.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mappers.js","sources":["../../../../src/core-api/routes/validation/mappers.ts"],"sourcesContent":["/**\n * @fileoverview\n * This file contains functions responsible for mapping Strapi attribute definitions to Zod schemas.\n */\n\nimport type { Schema } from '@strapi/types';\nimport * as z from 'zod/v4';\n\n// eslint-disable-next-line import/no-cycle\nimport * as attributes from './attributes';\n\n/**\n * Creates a Zod schema for a collection of Strapi attributes.\n *\n * @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.\n * @returns A Zod object schema representing the combined attributes.\n *\n * @example\n * ```typescript\n * const myAttributes = [\n * ['title', { type: 'string', required: true }],\n * ['description', { type: 'text' }],\n * ];\n * const schema = createAttributesSchema(myAttributes);\n * // schema will be a Zod object with 'title' and 'description' fields,\n * // each mapped to their respective Zod schemas based on their Strapi attribute types.\n * ```\n */\nexport const createAttributesSchema = (\n attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]\n) => {\n return attributes.reduce((acc, [name, attribute]) => {\n return acc.extend({\n get [name]() {\n return mapAttributeToSchema(attribute);\n },\n });\n }, z.object({}));\n};\n\n/**\n * Creates a Zod input schema for a collection of Strapi attributes.\n * This is typically used for validating incoming data (e.g., from API requests).\n *\n * @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.\n * @returns A Zod object schema representing the combined input attributes.\n *\n * @example\n * ```typescript\n * const myInputAttributes = [\n * ['email', { type: 'email', required: true }],\n * ['description', { type: 'text', minLength: 8 }],\n * ];\n * const inputSchema = createAttributesInputSchema(myInputAttributes);\n * // inputSchema will be a Zod object with 'email' and 'description' fields,\n * // mapped to Zod schemas suitable for input validation.\n * ```\n */\nexport const createAttributesInputSchema = (\n attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]\n) => {\n return attributes.reduce((acc, [name, attribute]) => {\n return acc.extend({\n get [name]() {\n return mapAttributeToInputSchema(attribute);\n },\n });\n }, z.object({}));\n};\n\n/**\n * Maps a Strapi attribute definition to a corresponding Zod validation schema.\n *\n * This function handles every Strapi attribute types and converts them into\n * appropriate Zod validation schemas.\n *\n * @param attribute - The Strapi attribute configuration object.\n * @returns A Zod schema that corresponds to the input attribute's type.\n * @throws {Error} Throws an error if an unsupported attribute type is provided.\n *\n * @example\n * ```typescript\n * const stringAttribute = { type: 'string', minLength: 3 };\n * const stringSchema = mapAttributeToSchema(stringAttribute); // Returns a Zod string schema with minLength.\n *\n * const booleanAttribute = { type: 'boolean', default: false };\n * const booleanSchema = mapAttributeToSchema(booleanAttribute); // Returns a Zod boolean schema with a default.\n * ```\n */\nexport const mapAttributeToSchema = (attribute: Schema.Attribute.AnyAttribute): z.ZodTypeAny => {\n switch (attribute.type) {\n case 'biginteger':\n return attributes.bigIntegerToSchema(attribute);\n case 'blocks':\n return attributes.blocksToSchema();\n case 'boolean':\n return attributes.booleanToSchema(attribute);\n case 'component':\n return attributes.componentToSchema(attribute);\n case 'date':\n return attributes.dateToSchema(attribute);\n case 'datetime':\n return attributes.datetimeToSchema(attribute);\n case 'decimal':\n return attributes.decimalToSchema(attribute);\n case 'dynamiczone':\n return attributes.dynamicZoneToSchema(attribute);\n case 'email':\n return attributes.emailToSchema(attribute);\n case 'enumeration':\n return attributes.enumToSchema(attribute);\n case 'float':\n return attributes.floatToSchema(attribute);\n case 'integer':\n return attributes.integerToSchema(attribute);\n case 'json':\n return attributes.jsonToSchema(attribute);\n case 'media':\n return attributes.mediaToSchema(attribute);\n case 'relation':\n return attributes.relationToSchema(attribute);\n case 'password':\n case 'text':\n case 'richtext':\n case 'string':\n return attributes.stringToSchema(attribute);\n case 'time':\n return attributes.timeToSchema(attribute);\n case 'timestamp':\n return attributes.timestampToSchema(attribute);\n case 'uid':\n return attributes.uidToSchema(attribute);\n default: {\n const { type } = attribute as Schema.Attribute.AnyAttribute;\n\n throw new Error(`Unsupported attribute type: ${type}`);\n }\n }\n};\n\n/**\n * Maps a Strapi attribute definition to a corresponding Zod input validation schema.\n *\n * This function handles every 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 = mapAttributeToInputSchema(stringAttribute);\n *\n * // Enumeration attribute\n * const enumAttribute = {\n * type: 'enumeration',\n * enum: ['draft', 'published', 'archived']\n * };\n * const enumSchema = mapAttributeToInputSchema(enumAttribute);\n *\n * // Media attribute with multiple files\n * const mediaAttribute = {\n * type: 'media',\n * multiple: true\n * };\n * const mediaSchema = mapAttributeToInputSchema(mediaAttribute);\n * ```\n *\n * @throws {Error} Throws an error if an unsupported attribute type is provided\n *\n */\nexport const mapAttributeToInputSchema = (\n attribute: Schema.Attribute.AnyAttribute\n): z.ZodTypeAny => {\n switch (attribute.type) {\n case 'biginteger':\n return attributes.bigIntegerToInputSchema(attribute);\n case 'blocks':\n return attributes.blocksToInputSchema();\n case 'boolean':\n return attributes.booleanToInputSchema(attribute);\n case 'component':\n return attributes.componentToInputSchema(attribute);\n case 'date':\n return attributes.dateToInputSchema(attribute);\n case 'datetime':\n return attributes.datetimeToInputSchema(attribute);\n case 'decimal':\n return attributes.decimalToInputSchema(attribute);\n case 'dynamiczone':\n return attributes.dynamicZoneToInputSchema(attribute);\n case 'email':\n return attributes.emailToInputSchema(attribute);\n case 'enumeration':\n return attributes.enumerationToInputSchema(attribute);\n case 'float':\n return attributes.floatToInputSchema(attribute);\n case 'integer':\n return attributes.integerToInputSchema(attribute);\n case 'json':\n return attributes.jsonToInputSchema(attribute);\n case 'media':\n return attributes.mediaToInputSchema(attribute);\n case 'relation':\n return attributes.relationToInputSchema(attribute);\n case 'password':\n case 'text':\n case 'richtext':\n case 'string':\n return attributes.textToInputSchema(attribute);\n case 'time':\n return attributes.timeToInputSchema(attribute);\n case 'timestamp':\n return attributes.timestampToInputSchema(attribute);\n case 'uid':\n return attributes.uidToInputSchema(attribute);\n default: {\n const { type } = attribute as Schema.Attribute.AnyAttribute;\n\n throw new Error(`Unsupported attribute type: ${type}`);\n }\n }\n};\n"],"names":["createAttributesSchema","attributes","reduce","acc","name","attribute","extend","mapAttributeToSchema","z","object","createAttributesInputSchema","mapAttributeToInputSchema","type","Error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAWA;;;;;;;;;;;;;;;;IAiBaA,MAAAA,sBAAAA,GAA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4';
|
2
|
+
import { uidToSchema, timestampToSchema, timeToSchema, stringToSchema, relationToSchema, mediaToSchema, jsonToSchema, integerToSchema, floatToSchema, enumToSchema, emailToSchema, dynamicZoneToSchema, decimalToSchema, datetimeToSchema, dateToSchema, componentToSchema, booleanToSchema, blocksToSchema, bigIntegerToSchema, uidToInputSchema, timestampToInputSchema, timeToInputSchema, textToInputSchema, relationToInputSchema, mediaToInputSchema, jsonToInputSchema, integerToInputSchema, floatToInputSchema, enumerationToInputSchema, emailToInputSchema, dynamicZoneToInputSchema, decimalToInputSchema, datetimeToInputSchema, dateToInputSchema, componentToInputSchema, booleanToInputSchema, blocksToInputSchema, bigIntegerToInputSchema } from './attributes.mjs';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Creates a Zod schema for a collection of Strapi attributes.
|
6
|
+
*
|
7
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
8
|
+
* @returns A Zod object schema representing the combined attributes.
|
9
|
+
*
|
10
|
+
* @example
|
11
|
+
* ```typescript
|
12
|
+
* const myAttributes = [
|
13
|
+
* ['title', { type: 'string', required: true }],
|
14
|
+
* ['description', { type: 'text' }],
|
15
|
+
* ];
|
16
|
+
* const schema = createAttributesSchema(myAttributes);
|
17
|
+
* // schema will be a Zod object with 'title' and 'description' fields,
|
18
|
+
* // each mapped to their respective Zod schemas based on their Strapi attribute types.
|
19
|
+
* ```
|
20
|
+
*/ const createAttributesSchema = (attributes)=>{
|
21
|
+
return attributes.reduce((acc, [name, attribute])=>{
|
22
|
+
return acc.extend({
|
23
|
+
get [name] () {
|
24
|
+
return mapAttributeToSchema(attribute);
|
25
|
+
}
|
26
|
+
});
|
27
|
+
}, z.object({}));
|
28
|
+
};
|
29
|
+
/**
|
30
|
+
* Creates a Zod input schema for a collection of Strapi attributes.
|
31
|
+
* This is typically used for validating incoming data (e.g., from API requests).
|
32
|
+
*
|
33
|
+
* @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.
|
34
|
+
* @returns A Zod object schema representing the combined input attributes.
|
35
|
+
*
|
36
|
+
* @example
|
37
|
+
* ```typescript
|
38
|
+
* const myInputAttributes = [
|
39
|
+
* ['email', { type: 'email', required: true }],
|
40
|
+
* ['description', { type: 'text', minLength: 8 }],
|
41
|
+
* ];
|
42
|
+
* const inputSchema = createAttributesInputSchema(myInputAttributes);
|
43
|
+
* // inputSchema will be a Zod object with 'email' and 'description' fields,
|
44
|
+
* // mapped to Zod schemas suitable for input validation.
|
45
|
+
* ```
|
46
|
+
*/ const createAttributesInputSchema = (attributes)=>{
|
47
|
+
return attributes.reduce((acc, [name, attribute])=>{
|
48
|
+
return acc.extend({
|
49
|
+
get [name] () {
|
50
|
+
return mapAttributeToInputSchema(attribute);
|
51
|
+
}
|
52
|
+
});
|
53
|
+
}, z.object({}));
|
54
|
+
};
|
55
|
+
/**
|
56
|
+
* Maps a Strapi attribute definition to a corresponding Zod validation schema.
|
57
|
+
*
|
58
|
+
* This function handles every Strapi attribute types and converts them into
|
59
|
+
* appropriate Zod validation schemas.
|
60
|
+
*
|
61
|
+
* @param attribute - The Strapi attribute configuration object.
|
62
|
+
* @returns A Zod schema that corresponds to the input attribute's type.
|
63
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided.
|
64
|
+
*
|
65
|
+
* @example
|
66
|
+
* ```typescript
|
67
|
+
* const stringAttribute = { type: 'string', minLength: 3 };
|
68
|
+
* const stringSchema = mapAttributeToSchema(stringAttribute); // Returns a Zod string schema with minLength.
|
69
|
+
*
|
70
|
+
* const booleanAttribute = { type: 'boolean', default: false };
|
71
|
+
* const booleanSchema = mapAttributeToSchema(booleanAttribute); // Returns a Zod boolean schema with a default.
|
72
|
+
* ```
|
73
|
+
*/ const mapAttributeToSchema = (attribute)=>{
|
74
|
+
switch(attribute.type){
|
75
|
+
case 'biginteger':
|
76
|
+
return bigIntegerToSchema(attribute);
|
77
|
+
case 'blocks':
|
78
|
+
return blocksToSchema();
|
79
|
+
case 'boolean':
|
80
|
+
return booleanToSchema(attribute);
|
81
|
+
case 'component':
|
82
|
+
return componentToSchema(attribute);
|
83
|
+
case 'date':
|
84
|
+
return dateToSchema(attribute);
|
85
|
+
case 'datetime':
|
86
|
+
return datetimeToSchema(attribute);
|
87
|
+
case 'decimal':
|
88
|
+
return decimalToSchema(attribute);
|
89
|
+
case 'dynamiczone':
|
90
|
+
return dynamicZoneToSchema(attribute);
|
91
|
+
case 'email':
|
92
|
+
return emailToSchema(attribute);
|
93
|
+
case 'enumeration':
|
94
|
+
return enumToSchema(attribute);
|
95
|
+
case 'float':
|
96
|
+
return floatToSchema(attribute);
|
97
|
+
case 'integer':
|
98
|
+
return integerToSchema(attribute);
|
99
|
+
case 'json':
|
100
|
+
return jsonToSchema(attribute);
|
101
|
+
case 'media':
|
102
|
+
return mediaToSchema(attribute);
|
103
|
+
case 'relation':
|
104
|
+
return relationToSchema(attribute);
|
105
|
+
case 'password':
|
106
|
+
case 'text':
|
107
|
+
case 'richtext':
|
108
|
+
case 'string':
|
109
|
+
return stringToSchema(attribute);
|
110
|
+
case 'time':
|
111
|
+
return timeToSchema(attribute);
|
112
|
+
case 'timestamp':
|
113
|
+
return timestampToSchema(attribute);
|
114
|
+
case 'uid':
|
115
|
+
return uidToSchema(attribute);
|
116
|
+
default:
|
117
|
+
{
|
118
|
+
const { type } = attribute;
|
119
|
+
throw new Error(`Unsupported attribute type: ${type}`);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
};
|
123
|
+
/**
|
124
|
+
* Maps a Strapi attribute definition to a corresponding Zod input validation schema.
|
125
|
+
*
|
126
|
+
* This function handles every Strapi attribute types and converts them into
|
127
|
+
* appropriate Zod validation schemas with their respective constraints.
|
128
|
+
*
|
129
|
+
* @param attribute - The Strapi attribute configuration object. Contains type information
|
130
|
+
* and validation rules for the attribute.
|
131
|
+
*
|
132
|
+
* @returns A Zod schema that corresponds to the input attribute's type and validation rules
|
133
|
+
*
|
134
|
+
* @example
|
135
|
+
* ```typescript
|
136
|
+
* // String attribute with constraints
|
137
|
+
* const stringAttribute = {
|
138
|
+
* type: 'string',
|
139
|
+
* minLength: 3,
|
140
|
+
* maxLength: 50,
|
141
|
+
* required: true
|
142
|
+
* };
|
143
|
+
* const stringSchema = mapAttributeToInputSchema(stringAttribute);
|
144
|
+
*
|
145
|
+
* // Enumeration attribute
|
146
|
+
* const enumAttribute = {
|
147
|
+
* type: 'enumeration',
|
148
|
+
* enum: ['draft', 'published', 'archived']
|
149
|
+
* };
|
150
|
+
* const enumSchema = mapAttributeToInputSchema(enumAttribute);
|
151
|
+
*
|
152
|
+
* // Media attribute with multiple files
|
153
|
+
* const mediaAttribute = {
|
154
|
+
* type: 'media',
|
155
|
+
* multiple: true
|
156
|
+
* };
|
157
|
+
* const mediaSchema = mapAttributeToInputSchema(mediaAttribute);
|
158
|
+
* ```
|
159
|
+
*
|
160
|
+
* @throws {Error} Throws an error if an unsupported attribute type is provided
|
161
|
+
*
|
162
|
+
*/ const mapAttributeToInputSchema = (attribute)=>{
|
163
|
+
switch(attribute.type){
|
164
|
+
case 'biginteger':
|
165
|
+
return bigIntegerToInputSchema(attribute);
|
166
|
+
case 'blocks':
|
167
|
+
return blocksToInputSchema();
|
168
|
+
case 'boolean':
|
169
|
+
return booleanToInputSchema(attribute);
|
170
|
+
case 'component':
|
171
|
+
return componentToInputSchema(attribute);
|
172
|
+
case 'date':
|
173
|
+
return dateToInputSchema(attribute);
|
174
|
+
case 'datetime':
|
175
|
+
return datetimeToInputSchema(attribute);
|
176
|
+
case 'decimal':
|
177
|
+
return decimalToInputSchema(attribute);
|
178
|
+
case 'dynamiczone':
|
179
|
+
return dynamicZoneToInputSchema(attribute);
|
180
|
+
case 'email':
|
181
|
+
return emailToInputSchema(attribute);
|
182
|
+
case 'enumeration':
|
183
|
+
return enumerationToInputSchema(attribute);
|
184
|
+
case 'float':
|
185
|
+
return floatToInputSchema(attribute);
|
186
|
+
case 'integer':
|
187
|
+
return integerToInputSchema(attribute);
|
188
|
+
case 'json':
|
189
|
+
return jsonToInputSchema(attribute);
|
190
|
+
case 'media':
|
191
|
+
return mediaToInputSchema(attribute);
|
192
|
+
case 'relation':
|
193
|
+
return relationToInputSchema(attribute);
|
194
|
+
case 'password':
|
195
|
+
case 'text':
|
196
|
+
case 'richtext':
|
197
|
+
case 'string':
|
198
|
+
return textToInputSchema(attribute);
|
199
|
+
case 'time':
|
200
|
+
return timeToInputSchema(attribute);
|
201
|
+
case 'timestamp':
|
202
|
+
return timestampToInputSchema(attribute);
|
203
|
+
case 'uid':
|
204
|
+
return uidToInputSchema(attribute);
|
205
|
+
default:
|
206
|
+
{
|
207
|
+
const { type } = attribute;
|
208
|
+
throw new Error(`Unsupported attribute type: ${type}`);
|
209
|
+
}
|
210
|
+
}
|
211
|
+
};
|
212
|
+
|
213
|
+
export { createAttributesInputSchema, createAttributesSchema, mapAttributeToInputSchema, mapAttributeToSchema };
|
214
|
+
//# sourceMappingURL=mappers.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mappers.mjs","sources":["../../../../src/core-api/routes/validation/mappers.ts"],"sourcesContent":["/**\n * @fileoverview\n * This file contains functions responsible for mapping Strapi attribute definitions to Zod schemas.\n */\n\nimport type { Schema } from '@strapi/types';\nimport * as z from 'zod/v4';\n\n// eslint-disable-next-line import/no-cycle\nimport * as attributes from './attributes';\n\n/**\n * Creates a Zod schema for a collection of Strapi attributes.\n *\n * @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.\n * @returns A Zod object schema representing the combined attributes.\n *\n * @example\n * ```typescript\n * const myAttributes = [\n * ['title', { type: 'string', required: true }],\n * ['description', { type: 'text' }],\n * ];\n * const schema = createAttributesSchema(myAttributes);\n * // schema will be a Zod object with 'title' and 'description' fields,\n * // each mapped to their respective Zod schemas based on their Strapi attribute types.\n * ```\n */\nexport const createAttributesSchema = (\n attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]\n) => {\n return attributes.reduce((acc, [name, attribute]) => {\n return acc.extend({\n get [name]() {\n return mapAttributeToSchema(attribute);\n },\n });\n }, z.object({}));\n};\n\n/**\n * Creates a Zod input schema for a collection of Strapi attributes.\n * This is typically used for validating incoming data (e.g., from API requests).\n *\n * @param attributes - An array of tuples, where each tuple contains the attribute name and its schema definition.\n * @returns A Zod object schema representing the combined input attributes.\n *\n * @example\n * ```typescript\n * const myInputAttributes = [\n * ['email', { type: 'email', required: true }],\n * ['description', { type: 'text', minLength: 8 }],\n * ];\n * const inputSchema = createAttributesInputSchema(myInputAttributes);\n * // inputSchema will be a Zod object with 'email' and 'description' fields,\n * // mapped to Zod schemas suitable for input validation.\n * ```\n */\nexport const createAttributesInputSchema = (\n attributes: [name: string, attribute: Schema.Attribute.AnyAttribute][]\n) => {\n return attributes.reduce((acc, [name, attribute]) => {\n return acc.extend({\n get [name]() {\n return mapAttributeToInputSchema(attribute);\n },\n });\n }, z.object({}));\n};\n\n/**\n * Maps a Strapi attribute definition to a corresponding Zod validation schema.\n *\n * This function handles every Strapi attribute types and converts them into\n * appropriate Zod validation schemas.\n *\n * @param attribute - The Strapi attribute configuration object.\n * @returns A Zod schema that corresponds to the input attribute's type.\n * @throws {Error} Throws an error if an unsupported attribute type is provided.\n *\n * @example\n * ```typescript\n * const stringAttribute = { type: 'string', minLength: 3 };\n * const stringSchema = mapAttributeToSchema(stringAttribute); // Returns a Zod string schema with minLength.\n *\n * const booleanAttribute = { type: 'boolean', default: false };\n * const booleanSchema = mapAttributeToSchema(booleanAttribute); // Returns a Zod boolean schema with a default.\n * ```\n */\nexport const mapAttributeToSchema = (attribute: Schema.Attribute.AnyAttribute): z.ZodTypeAny => {\n switch (attribute.type) {\n case 'biginteger':\n return attributes.bigIntegerToSchema(attribute);\n case 'blocks':\n return attributes.blocksToSchema();\n case 'boolean':\n return attributes.booleanToSchema(attribute);\n case 'component':\n return attributes.componentToSchema(attribute);\n case 'date':\n return attributes.dateToSchema(attribute);\n case 'datetime':\n return attributes.datetimeToSchema(attribute);\n case 'decimal':\n return attributes.decimalToSchema(attribute);\n case 'dynamiczone':\n return attributes.dynamicZoneToSchema(attribute);\n case 'email':\n return attributes.emailToSchema(attribute);\n case 'enumeration':\n return attributes.enumToSchema(attribute);\n case 'float':\n return attributes.floatToSchema(attribute);\n case 'integer':\n return attributes.integerToSchema(attribute);\n case 'json':\n return attributes.jsonToSchema(attribute);\n case 'media':\n return attributes.mediaToSchema(attribute);\n case 'relation':\n return attributes.relationToSchema(attribute);\n case 'password':\n case 'text':\n case 'richtext':\n case 'string':\n return attributes.stringToSchema(attribute);\n case 'time':\n return attributes.timeToSchema(attribute);\n case 'timestamp':\n return attributes.timestampToSchema(attribute);\n case 'uid':\n return attributes.uidToSchema(attribute);\n default: {\n const { type } = attribute as Schema.Attribute.AnyAttribute;\n\n throw new Error(`Unsupported attribute type: ${type}`);\n }\n }\n};\n\n/**\n * Maps a Strapi attribute definition to a corresponding Zod input validation schema.\n *\n * This function handles every 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 = mapAttributeToInputSchema(stringAttribute);\n *\n * // Enumeration attribute\n * const enumAttribute = {\n * type: 'enumeration',\n * enum: ['draft', 'published', 'archived']\n * };\n * const enumSchema = mapAttributeToInputSchema(enumAttribute);\n *\n * // Media attribute with multiple files\n * const mediaAttribute = {\n * type: 'media',\n * multiple: true\n * };\n * const mediaSchema = mapAttributeToInputSchema(mediaAttribute);\n * ```\n *\n * @throws {Error} Throws an error if an unsupported attribute type is provided\n *\n */\nexport const mapAttributeToInputSchema = (\n attribute: Schema.Attribute.AnyAttribute\n): z.ZodTypeAny => {\n switch (attribute.type) {\n case 'biginteger':\n return attributes.bigIntegerToInputSchema(attribute);\n case 'blocks':\n return attributes.blocksToInputSchema();\n case 'boolean':\n return attributes.booleanToInputSchema(attribute);\n case 'component':\n return attributes.componentToInputSchema(attribute);\n case 'date':\n return attributes.dateToInputSchema(attribute);\n case 'datetime':\n return attributes.datetimeToInputSchema(attribute);\n case 'decimal':\n return attributes.decimalToInputSchema(attribute);\n case 'dynamiczone':\n return attributes.dynamicZoneToInputSchema(attribute);\n case 'email':\n return attributes.emailToInputSchema(attribute);\n case 'enumeration':\n return attributes.enumerationToInputSchema(attribute);\n case 'float':\n return attributes.floatToInputSchema(attribute);\n case 'integer':\n return attributes.integerToInputSchema(attribute);\n case 'json':\n return attributes.jsonToInputSchema(attribute);\n case 'media':\n return attributes.mediaToInputSchema(attribute);\n case 'relation':\n return attributes.relationToInputSchema(attribute);\n case 'password':\n case 'text':\n case 'richtext':\n case 'string':\n return attributes.textToInputSchema(attribute);\n case 'time':\n return attributes.timeToInputSchema(attribute);\n case 'timestamp':\n return attributes.timestampToInputSchema(attribute);\n case 'uid':\n return attributes.uidToInputSchema(attribute);\n default: {\n const { type } = attribute as Schema.Attribute.AnyAttribute;\n\n throw new Error(`Unsupported attribute type: ${type}`);\n }\n }\n};\n"],"names":["createAttributesSchema","attributes","reduce","acc","name","attribute","extend","mapAttributeToSchema","z","object","createAttributesInputSchema","mapAttributeToInputSchema","type","Error"],"mappings":";;;AAWA;;;;;;;;;;;;;;;;IAiBaA,MAAAA,sBAAAA,GAAyB,CACpCC,UAAAA,GAAAA;AAEA,IAAA,OAAOA,WAAWC,MAAM,CAAC,CAACC,GAAK,EAAA,CAACC,MAAMC,SAAU,CAAA,GAAA;QAC9C,OAAOF,GAAAA,CAAIG,MAAM,CAAC;YAChB,KAAKF,MAAQ,GAAA;AACX,gBAAA,OAAOG,oBAAqBF,CAAAA,SAAAA,CAAAA;AAC9B;AACF,SAAA,CAAA;KACCG,EAAAA,CAAAA,CAAEC,MAAM,CAAC,EAAC,CAAA,CAAA;AACf;AAEA;;;;;;;;;;;;;;;;;IAkB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K,aAAA;YACH,OAAOJ,wBAAmC,CAACI,SAAAA,CAAAA;QAC7C,KAAK,OAAA;YACH,OAAOJ,kBAA6B,CAACI,SAAAA,CAAAA;QACvC,KAAK,SAAA;YACH,OAAOJ,oBAA+B,CAACI,SAAAA,CAAAA;QACzC,KAAK,MAAA;YACH,OAAOJ,iBAA4B,CAACI,SAAAA,CAAAA;QACtC,KAAK,OAAA;YACH,OAAOJ,kBAA6B,CAACI,SAAAA,CAAAA;QACvC,KAAK,UAAA;YACH,OAAOJ,qBAAgC,CAACI,SAAAA,CAAAA;QAC1C,KAAK,UAAA;QACL,KAAK,MAAA;QACL,KAAK,UAAA;QACL,KAAK,QAAA;YACH,OAAOJ,iBAA4B,CAACI,SAAAA,CAAAA;QACtC,KAAK,MAAA;YACH,OAAOJ,iBAA4B,CAACI,SAAAA,CAAAA;QACtC,KAAK,WAAA;YACH,OAAOJ,sBAAiC,CAACI,SAAAA,CAAAA;QAC3C,KAAK,KAAA;YACH,OAAOJ,gBAA2B,CAACI,SAAAA,CAAAA;AACrC,QAAA;AAAS,YAAA;gBACP,MAAM,EAAEO,IAAI,EAAE,GAAGP,SAAAA;AAEjB,gBAAA,MAAM,IAAIQ,KAAM,CAAA,CAAC,4BAA4B,EAAED,KAAK,CAAC,CAAA;AACvD;AACF;AACF;;;;"}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import type { Internal } from '@strapi/types';
|
2
|
+
import * as z from 'zod/v4';
|
3
|
+
/**
|
4
|
+
* Safely adds or updates a schema in Zod's global registry.
|
5
|
+
*
|
6
|
+
* If a schema with the given `id` already exists, it will be removed before adding the new one.
|
7
|
+
*
|
8
|
+
* This is useful for hot-reloading or preventing issues with cyclical dependencies.
|
9
|
+
*
|
10
|
+
* @param id - The unique identifier for the schema in the global registry.
|
11
|
+
* @param schema - The Zod schema to register.
|
12
|
+
* @example
|
13
|
+
* ```typescript
|
14
|
+
* safeGlobalRegistrySet("mySchema", z.object({ name: z.string() }));
|
15
|
+
* ```
|
16
|
+
*/
|
17
|
+
export declare const safeGlobalRegistrySet: (id: Internal.UID.Schema, schema: z.ZodType) => void;
|
18
|
+
/**
|
19
|
+
* Safely creates and registers a Zod schema in the global registry, particularly useful for handling cyclical data structures.
|
20
|
+
*
|
21
|
+
* If a schema with the given `id` already exists in the global registry, it returns the existing schema.
|
22
|
+
*
|
23
|
+
* Otherwise, it registers a temporary `z.any()` schema, calls the provided `callback` to create the actual schema,
|
24
|
+
* and then replaces the temporary schema with the actual one in the registry.
|
25
|
+
*
|
26
|
+
* This prevents infinite loops in cases of cyclical dependencies.
|
27
|
+
*
|
28
|
+
* @param id - The unique identifier for the schema in the global registry.
|
29
|
+
* @param callback - A function that returns the Zod schema to be created and registered.
|
30
|
+
* @returns The created or retrieved Zod schema.
|
31
|
+
* @example
|
32
|
+
* ```typescript
|
33
|
+
* const CategorySchema = safeSchemaCreation("Category", () =>
|
34
|
+
* z.object({
|
35
|
+
* name: z.string(),
|
36
|
+
* products: z.array(safeSchemaCreation("Product", () =>
|
37
|
+
* z.object({
|
38
|
+
* name: z.string(),
|
39
|
+
* category: z.lazy(() => CategorySchema) // Cyclical reference
|
40
|
+
* })
|
41
|
+
* ))
|
42
|
+
* })
|
43
|
+
* );
|
44
|
+
* ```
|
45
|
+
*/
|
46
|
+
export declare const safeSchemaCreation: (id: Internal.UID.Schema, callback: () => z.ZodType) => z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
47
|
+
//# sourceMappingURL=utils.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/core-api/routes/validation/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,qBAAqB,OAAQ,SAAS,GAAG,CAAC,MAAM,UAAU,EAAE,OAAO,SAsB/E,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,kBAAkB,OAAQ,SAAS,GAAG,CAAC,MAAM,YAAY,MAAM,EAAE,OAAO,kFAmCpF,CAAC"}
|