Skip to content

Commit 9a3bd93

Browse files
committed
feat: strapi conditional fields support
1 parent 270d189 commit 9a3bd93

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sensinum/astro-strapi-loader",
3-
"version": "1.0.8",
3+
"version": "1.0.9-beta.1",
44
"description": "Astro loader for Strapi CMS",
55
"keywords": [
66
"astro",

src/types/strapi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface StrapiAttribute {
6363
regex?: string;
6464
enum?: string[];
6565
component?: string;
66+
conditions?: Record<string, unknown>;
6667
repeatable?: boolean;
6768
target?: string;
6869
targetAttribute?: string;

src/utils/schema.ts

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,64 @@ export class StrapiSchemaGenerator {
1414

1515
private generateAttributeSchema(attribute: StrapiAttribute, pupulatedRelations: Array<string> = []): z.ZodType<unknown> | null {
1616
const ref = this;
17+
let schema;
1718
switch (attribute.type) {
1819
case "string":
19-
let schema = z.string();
20+
schema = z.string();
2021
if (attribute.required) schema = schema.min(1);
2122
if (attribute.minLength) schema = schema.min(attribute.minLength);
2223
if (attribute.maxLength) schema = schema.max(attribute.maxLength);
2324
if (attribute.regex) schema = schema.regex(new RegExp(attribute.regex));
24-
return schema;
25-
25+
break;
2626
case "text":
2727
case "richtext":
28-
return z.string();
29-
28+
schema = z.string();
29+
break;
3030
case "email":
31-
return z.string().email();
32-
31+
schema = z.string().email();
32+
break;
3333
case "password":
34-
return z.string();
35-
34+
schema = z.string();
35+
break;
3636
case "integer":
3737
let intSchema = z.number().int();
3838
if (attribute.required) intSchema = intSchema.min(1);
3939
if (attribute.min) intSchema = intSchema.min(attribute.min);
4040
if (attribute.max) intSchema = intSchema.max(attribute.max);
41-
return intSchema;
42-
41+
schema = intSchema;
42+
break;
4343
case "biginteger":
4444
case "float":
4545
case "decimal":
4646
let numberSchema = z.number();
4747
if (attribute.required) numberSchema = numberSchema.min(0);
4848
if (attribute.min) numberSchema = numberSchema.min(attribute.min);
4949
if (attribute.max) numberSchema = numberSchema.max(attribute.max);
50-
return numberSchema;
51-
50+
schema = numberSchema;
51+
break;
5252
case "boolean":
53-
return z.boolean();
54-
53+
schema = z.boolean();
54+
break;
5555
case "date":
5656
case "datetime":
57-
return z.string();
58-
57+
schema = z.string();
58+
break;
5959
case "time":
60-
return z.string();
61-
60+
schema = z.string();
61+
break;
6262
case "timestamp":
63-
return z.number();
64-
63+
schema = z.number();
64+
break;
6565
case "json":
66-
return z.any();
67-
66+
schema = z.any();
67+
break;
6868
case "enumeration":
6969
if (!attribute.enum)
7070
throw new Error("Enumeration type requires enum values");
71-
return z.enum(attribute.enum as [string, ...string[]]).nullable();
72-
71+
schema = z.enum(attribute.enum as [string, ...string[]]).nullable();
72+
break;
7373
case "media":
74-
return z.object({
74+
schema = z.object({
7575
name: z.string(),
7676
alternativeText: z.string().optional().nullable(),
7777
caption: z.string().optional().nullable(),
@@ -88,6 +88,7 @@ export class StrapiSchemaGenerator {
8888
createdAt: z.string(),
8989
updatedAt: z.string(),
9090
});
91+
break;
9192
case "relation":
9293
if (!attribute.relation)
9394
throw new Error("Relation type requires relation target");
@@ -112,16 +113,18 @@ export class StrapiSchemaGenerator {
112113
switch (attribute.relation) {
113114
case "oneToOne":
114115
case "manyToOne":
115-
return relationSchema;
116+
schema = relationSchema;
117+
break;
116118
case "oneToMany":
117119
case "manyToMany":
118-
return z.array(relationSchema);
120+
schema = z.array(relationSchema);
121+
break;
119122
default:
120123
throw new Error(
121124
`Unsupported relation type: ${attribute.relationType}`,
122125
);
123126
}
124-
127+
break;
125128
case "component":
126129
if (!attribute.component)
127130
throw new Error("Component type requires component name");
@@ -132,17 +135,23 @@ export class StrapiSchemaGenerator {
132135
throw new Error(`Component ${attribute.component} not found`);
133136

134137
if (attribute.repeatable) {
135-
return z.array(this.generateComponentSchema(component.schema));
138+
schema = z.array(this.generateComponentSchema(component.schema));
139+
} else {
140+
schema = this.generateComponentSchema(component.schema);
136141
}
137-
return this.generateComponentSchema(component.schema);
138-
142+
break;
139143
case "dynamiczone":
140144
// TODO: Implement dynamiczone schema generation
141-
return z.any();
142-
145+
schema = z.any();
146+
break;
143147
default:
144-
return z.any();
148+
schema = z.any();
149+
break;
150+
}
151+
if (attribute.conditions) {
152+
schema = schema.nullable();
145153
}
154+
return schema;
146155
}
147156

148157
private generateContentTypeSchema(

0 commit comments

Comments
 (0)