@contentful/experiences-validators 3.7.0-prerelease-20250917T1034-42f0486.0 → 3.7.1-beta.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/index.d.ts CHANGED
@@ -183,20 +183,20 @@ type BreakpointQuery = '*' | `>${number}px` | `<${number}px`;
183
183
  declare const BreakpointSchema: z.ZodObject<{
184
184
  id: z.ZodString;
185
185
  query: z.ZodEffects<z.ZodString, BreakpointQuery, string>;
186
- previewSize: z.ZodString;
186
+ previewSize: z.ZodOptional<z.ZodString>;
187
187
  displayName: z.ZodString;
188
188
  displayIcon: z.ZodOptional<z.ZodEnum<["desktop", "tablet", "mobile"]>>;
189
189
  }, "strict", z.ZodTypeAny, {
190
190
  id: string;
191
191
  query: BreakpointQuery;
192
- previewSize: string;
193
192
  displayName: string;
193
+ previewSize?: string | undefined;
194
194
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
195
195
  }, {
196
196
  id: string;
197
197
  query: string;
198
- previewSize: string;
199
198
  displayName: string;
199
+ previewSize?: string | undefined;
200
200
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
201
201
  }>;
202
202
  declare const BaseComponentTreeNodeSchema: z.ZodObject<{
@@ -337,32 +337,32 @@ declare const ComponentTreeSchema: z.ZodObject<{
337
337
  breakpoints: z.ZodEffects<z.ZodArray<z.ZodObject<{
338
338
  id: z.ZodString;
339
339
  query: z.ZodEffects<z.ZodString, BreakpointQuery, string>;
340
- previewSize: z.ZodString;
340
+ previewSize: z.ZodOptional<z.ZodString>;
341
341
  displayName: z.ZodString;
342
342
  displayIcon: z.ZodOptional<z.ZodEnum<["desktop", "tablet", "mobile"]>>;
343
343
  }, "strict", z.ZodTypeAny, {
344
344
  id: string;
345
345
  query: BreakpointQuery;
346
- previewSize: string;
347
346
  displayName: string;
347
+ previewSize?: string | undefined;
348
348
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
349
349
  }, {
350
350
  id: string;
351
351
  query: string;
352
- previewSize: string;
353
352
  displayName: string;
353
+ previewSize?: string | undefined;
354
354
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
355
355
  }>, "many">, {
356
356
  id: string;
357
357
  query: BreakpointQuery;
358
- previewSize: string;
359
358
  displayName: string;
359
+ previewSize?: string | undefined;
360
360
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
361
361
  }[], {
362
362
  id: string;
363
363
  query: string;
364
- previewSize: string;
365
364
  displayName: string;
365
+ previewSize?: string | undefined;
366
366
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
367
367
  }[]>;
368
368
  children: z.ZodArray<z.ZodType<ComponentTreeNode, z.ZodTypeDef, ComponentTreeNode>, "many">;
@@ -372,8 +372,8 @@ declare const ComponentTreeSchema: z.ZodObject<{
372
372
  breakpoints: {
373
373
  id: string;
374
374
  query: BreakpointQuery;
375
- previewSize: string;
376
375
  displayName: string;
376
+ previewSize?: string | undefined;
377
377
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
378
378
  }[];
379
379
  schemaVersion: "2023-09-28";
@@ -382,8 +382,8 @@ declare const ComponentTreeSchema: z.ZodObject<{
382
382
  breakpoints: {
383
383
  id: string;
384
384
  query: string;
385
- previewSize: string;
386
385
  displayName: string;
386
+ previewSize?: string | undefined;
387
387
  displayIcon?: "desktop" | "tablet" | "mobile" | undefined;
388
388
  }[];
389
389
  schemaVersion: "2023-09-28";
@@ -424,12 +424,16 @@ type Parameter = z.infer<typeof ParameterSchema>;
424
424
  declare const VariableMappingSchema: z.ZodObject<{
425
425
  parameterId: z.ZodString;
426
426
  type: z.ZodLiteral<"ContentTypeMapping">;
427
- pathsByContentType: z.ZodRecord<z.ZodString, z.ZodObject<{
427
+ pathsByContentType: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodObject<{
428
428
  path: z.ZodString;
429
429
  }, "strip", z.ZodTypeAny, {
430
430
  path: string;
431
431
  }, {
432
432
  path: string;
433
+ }>>, Record<string, {
434
+ path: string;
435
+ }>, Record<string, {
436
+ path: string;
433
437
  }>>;
434
438
  }, "strip", z.ZodTypeAny, {
435
439
  type: "ContentTypeMapping";
@@ -547,7 +551,7 @@ declare const ParameterDefinitionSchema: z.ZodObject<{
547
551
  nodeId: string;
548
552
  }[] | undefined;
549
553
  }>;
550
- declare const ComponentSettingsSchema: z.ZodObject<{
554
+ declare const ComponentSettingsSchema: z.ZodEffects<z.ZodObject<{
551
555
  variableDefinitions: z.ZodRecord<z.ZodString, z.ZodObject<{
552
556
  displayName: z.ZodOptional<z.ZodString>;
553
557
  type: z.ZodEnum<["Text", "RichText", "Number", "Date", "Boolean", "Location", "Media", "Object", "Hyperlink", "Array", "Link"]>;
@@ -816,12 +820,16 @@ declare const ComponentSettingsSchema: z.ZodObject<{
816
820
  variableMappings: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
817
821
  parameterId: z.ZodString;
818
822
  type: z.ZodLiteral<"ContentTypeMapping">;
819
- pathsByContentType: z.ZodRecord<z.ZodString, z.ZodObject<{
823
+ pathsByContentType: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodObject<{
820
824
  path: z.ZodString;
821
825
  }, "strip", z.ZodTypeAny, {
822
826
  path: string;
823
827
  }, {
824
828
  path: string;
829
+ }>>, Record<string, {
830
+ path: string;
831
+ }>, Record<string, {
832
+ path: string;
825
833
  }>>;
826
834
  }, "strip", z.ZodTypeAny, {
827
835
  type: "ContentTypeMapping";
@@ -1030,8 +1038,142 @@ declare const ComponentSettingsSchema: z.ZodObject<{
1030
1038
  }> | undefined;
1031
1039
  allowedVariableOverrides?: string[] | undefined;
1032
1040
  }[] | undefined;
1041
+ }>, {
1042
+ variableDefinitions: Record<string, {
1043
+ type: "Text" | "RichText" | "Number" | "Date" | "Boolean" | "Location" | "Media" | "Object" | "Hyperlink" | "Array" | "Link";
1044
+ displayName?: string | undefined;
1045
+ description?: string | undefined;
1046
+ group?: string | undefined;
1047
+ defaultValue?: string | number | boolean | Record<any, any> | {
1048
+ type: "DesignValue";
1049
+ valuesByBreakpoint: Record<string, string | number | boolean | Record<any, any> | undefined>;
1050
+ } | {
1051
+ path: string;
1052
+ type: "BoundValue";
1053
+ } | {
1054
+ type: "HyperlinkValue";
1055
+ linkTargetKey: string;
1056
+ overrides?: {} | undefined;
1057
+ } | {
1058
+ type: "UnboundValue";
1059
+ key: string;
1060
+ } | {
1061
+ type: "ComponentValue";
1062
+ key: string;
1063
+ } | {
1064
+ type: "NoValue";
1065
+ } | undefined;
1066
+ validations?: {
1067
+ required?: boolean | undefined;
1068
+ bindingSourceType?: ["entry" | "asset" | "manual" | "experience", ...("entry" | "asset" | "manual" | "experience")[]] | undefined;
1069
+ format?: "URL" | undefined;
1070
+ in?: {
1071
+ value: string | number;
1072
+ displayName?: string | undefined;
1073
+ }[] | undefined;
1074
+ } | undefined;
1075
+ }>;
1076
+ thumbnailId?: "columns" | "columnsPlusRight" | "imagesSquare" | "subtitles" | "rowsPlusBottom" | "userRectangle" | "textbox" | "monitorPlay" | "article" | "table" | "star" | "heartStraight" | "frameCorners" | "rows" | "dotsThreeOutline" | "listDashes" | "checkerBoard" | "gridFour" | "slideshow" | "diamondsFour" | "cards" | "textColumns" | "duplex" | undefined;
1077
+ category?: string | undefined;
1078
+ prebindingDefinitions?: {
1079
+ id: string;
1080
+ parameterDefinitions: Record<string, {
1081
+ contentTypes: string[];
1082
+ defaultSource?: {
1083
+ type: "Entry";
1084
+ contentTypeId: string;
1085
+ link: {
1086
+ sys: {
1087
+ type: "Link";
1088
+ id: string;
1089
+ linkType: "Entry";
1090
+ };
1091
+ };
1092
+ } | undefined;
1093
+ passToNodes?: {
1094
+ prebindingId: string;
1095
+ parameterId: string;
1096
+ nodeId: string;
1097
+ }[] | undefined;
1098
+ }>;
1099
+ variableMappings?: Record<string, {
1100
+ type: "ContentTypeMapping";
1101
+ parameterId: string;
1102
+ pathsByContentType: Record<string, {
1103
+ path: string;
1104
+ }>;
1105
+ }> | undefined;
1106
+ allowedVariableOverrides?: string[] | undefined;
1107
+ }[] | undefined;
1108
+ }, {
1109
+ variableDefinitions: Record<string, {
1110
+ type: "Text" | "RichText" | "Number" | "Date" | "Boolean" | "Location" | "Media" | "Object" | "Hyperlink" | "Array" | "Link";
1111
+ displayName?: string | undefined;
1112
+ description?: string | undefined;
1113
+ group?: string | undefined;
1114
+ defaultValue?: string | number | boolean | Record<any, any> | {
1115
+ type: "DesignValue";
1116
+ valuesByBreakpoint: Record<string, string | number | boolean | Record<any, any> | undefined>;
1117
+ } | {
1118
+ path: string;
1119
+ type: "BoundValue";
1120
+ } | {
1121
+ type: "HyperlinkValue";
1122
+ linkTargetKey: string;
1123
+ overrides?: {} | undefined;
1124
+ } | {
1125
+ type: "UnboundValue";
1126
+ key: string;
1127
+ } | {
1128
+ type: "ComponentValue";
1129
+ key: string;
1130
+ } | {
1131
+ type: "NoValue";
1132
+ } | undefined;
1133
+ validations?: {
1134
+ required?: boolean | undefined;
1135
+ bindingSourceType?: ["entry" | "asset" | "manual" | "experience", ...("entry" | "asset" | "manual" | "experience")[]] | undefined;
1136
+ format?: "URL" | undefined;
1137
+ in?: {
1138
+ value: string | number;
1139
+ displayName?: string | undefined;
1140
+ }[] | undefined;
1141
+ } | undefined;
1142
+ }>;
1143
+ thumbnailId?: "columns" | "columnsPlusRight" | "imagesSquare" | "subtitles" | "rowsPlusBottom" | "userRectangle" | "textbox" | "monitorPlay" | "article" | "table" | "star" | "heartStraight" | "frameCorners" | "rows" | "dotsThreeOutline" | "listDashes" | "checkerBoard" | "gridFour" | "slideshow" | "diamondsFour" | "cards" | "textColumns" | "duplex" | undefined;
1144
+ category?: string | undefined;
1145
+ prebindingDefinitions?: {
1146
+ id: string;
1147
+ parameterDefinitions: Record<string, {
1148
+ contentTypes: string[];
1149
+ defaultSource?: {
1150
+ type: "Entry";
1151
+ contentTypeId: string;
1152
+ link: {
1153
+ sys: {
1154
+ type: "Link";
1155
+ id: string;
1156
+ linkType: "Entry";
1157
+ };
1158
+ };
1159
+ } | undefined;
1160
+ passToNodes?: {
1161
+ prebindingId: string;
1162
+ parameterId: string;
1163
+ nodeId: string;
1164
+ }[] | undefined;
1165
+ }>;
1166
+ variableMappings?: Record<string, {
1167
+ type: "ContentTypeMapping";
1168
+ parameterId: string;
1169
+ pathsByContentType: Record<string, {
1170
+ path: string;
1171
+ }>;
1172
+ }> | undefined;
1173
+ allowedVariableOverrides?: string[] | undefined;
1174
+ }[] | undefined;
1033
1175
  }>;
1034
- declare const PatternFieldsCMAShapeSchema: z.ZodObject<{
1176
+ declare const PatternFieldsCMAShapeSchema: z.ZodEffects<z.ZodObject<{
1035
1177
  componentTree: z.ZodRecord<z.ZodString, z.ZodTypeAny>;
1036
1178
  dataSource: z.ZodRecord<z.ZodString, z.ZodTypeAny>;
1037
1179
  unboundValues: z.ZodRecord<z.ZodString, z.ZodTypeAny>;
@@ -1049,6 +1191,18 @@ declare const PatternFieldsCMAShapeSchema: z.ZodObject<{
1049
1191
  unboundValues: Record<string, any>;
1050
1192
  componentSettings: Record<string, any>;
1051
1193
  usedComponents?: Record<string, any> | undefined;
1194
+ }>, {
1195
+ componentTree: Record<string, any>;
1196
+ dataSource: Record<string, any>;
1197
+ unboundValues: Record<string, any>;
1198
+ componentSettings: Record<string, any>;
1199
+ usedComponents?: Record<string, any> | undefined;
1200
+ }, {
1201
+ componentTree: Record<string, any>;
1202
+ dataSource: Record<string, any>;
1203
+ unboundValues: Record<string, any>;
1204
+ componentSettings: Record<string, any>;
1205
+ usedComponents?: Record<string, any> | undefined;
1052
1206
  }>;
1053
1207
  type PatternFields = z.infer<typeof PatternFieldsCMAShapeSchema>;
1054
1208
  type ParameterDefinition = z.infer<typeof ParameterDefinitionSchema>;
@@ -1393,7 +1547,7 @@ type ValidatorReturnValue = {
1393
1547
  errors?: ContentfulErrorDetails[];
1394
1548
  };
1395
1549
 
1396
- declare const validateBreakpointsDefinition: (breakpoints: any) => ValidatorReturnValue;
1550
+ declare const validateBreakpointsDefinition: (breakpoints: Breakpoint[]) => ValidatorReturnValue;
1397
1551
 
1398
1552
  declare const validateComponentDefinition: (definition: any) => ValidatorReturnValue;
1399
1553
 
package/dist/index.js CHANGED
@@ -193,7 +193,7 @@ const BreakpointSchema = z
193
193
  id: propertyKeySchema,
194
194
  // Can be replace with z.templateLiteral when upgrading to zod v4
195
195
  query: z.string().refine((s) => BREAKPOINT_QUERY_REGEX.test(s)),
196
- previewSize: z.string(),
196
+ previewSize: z.string().optional(),
197
197
  displayName: z.string(),
198
198
  displayIcon: z.enum(['desktop', 'tablet', 'mobile']).optional(),
199
199
  })
@@ -260,6 +260,25 @@ const ExperienceFieldsCMAShapeSchema = z.object({
260
260
  usedComponents: localeWrapper(UsedComponentsSchema).optional(),
261
261
  });
262
262
 
263
+ function treeVisit(initialNode, onNode) {
264
+ const _treeVisit = (currentNode) => {
265
+ const children = [...currentNode.children];
266
+ onNode(currentNode);
267
+ for (const child of children) {
268
+ _treeVisit(child);
269
+ }
270
+ };
271
+ if (Array.isArray(initialNode)) {
272
+ for (const node of initialNode) {
273
+ _treeVisit(node);
274
+ }
275
+ }
276
+ else {
277
+ _treeVisit(initialNode);
278
+ }
279
+ }
280
+
281
+ const MAX_ALLOWED_PATHS = 200;
263
282
  const THUMBNAIL_IDS = [
264
283
  'columns',
265
284
  'columnsPlusRight',
@@ -290,7 +309,17 @@ const THUMBNAIL_IDS = [
290
309
  const VariableMappingSchema = z.object({
291
310
  parameterId: propertyKeySchema,
292
311
  type: z.literal('ContentTypeMapping'),
293
- pathsByContentType: z.record(z.string(), z.object({ path: z.string() })),
312
+ pathsByContentType: z
313
+ .record(z.string(), z.object({ path: z.string() }))
314
+ .superRefine((paths, ctx) => {
315
+ const variableId = ctx.path[ctx.path.length - 2];
316
+ if (Object.keys(paths).length > MAX_ALLOWED_PATHS) {
317
+ ctx.addIssue({
318
+ code: z.ZodIssueCode.custom,
319
+ message: `Too many paths defined for variable mapping with id "${variableId}", maximum allowed is ${MAX_ALLOWED_PATHS}`,
320
+ });
321
+ }
322
+ }),
294
323
  });
295
324
  const PassToNodeSchema = z
296
325
  .object({
@@ -314,7 +343,10 @@ const ParameterDefinitionSchema = z.object({
314
343
  })
315
344
  .optional(),
316
345
  contentTypes: z.array(z.string()).min(1),
317
- passToNodes: z.array(PassToNodeSchema).optional(),
346
+ passToNodes: z
347
+ .array(PassToNodeSchema)
348
+ .max(1, 'At most one "passToNodes" element is allowed per parameter definition.')
349
+ .optional(), // we might change this to be empty array for native parameter definitions, that's why we don't use .length(1)
318
350
  });
319
351
  const ParameterDefinitionsSchema = z.record(propertyKeySchema, ParameterDefinitionSchema);
320
352
  const VariableMappingsSchema = z.record(propertyKeySchema, VariableMappingSchema);
@@ -335,14 +367,108 @@ const ComponentSettingsSchema = z
335
367
  category: z.string().max(50, 'Category must contain at most 50 characters').optional(),
336
368
  prebindingDefinitions: z.array(PrebindingDefinitionSchema).length(1).optional(),
337
369
  })
338
- .strict();
339
- const PatternFieldsCMAShapeSchema = z.object({
370
+ .strict()
371
+ .superRefine((componentSettings, ctx) => {
372
+ const { variableDefinitions, prebindingDefinitions } = componentSettings;
373
+ if (!prebindingDefinitions || prebindingDefinitions.length === 0) {
374
+ return;
375
+ }
376
+ const { parameterDefinitions, variableMappings, allowedVariableOverrides } = prebindingDefinitions[0];
377
+ validateAtMostOneNativeParameterDefinition(parameterDefinitions, ctx);
378
+ validateNoOverlapBetweenMappingAndOverrides(variableMappings, allowedVariableOverrides, ctx);
379
+ validateMappingsAgainstVariableDefinitions(variableMappings, allowedVariableOverrides, variableDefinitions, ctx);
380
+ validateMappingsAgainstParameterDefinitions(variableMappings, parameterDefinitions, ctx);
381
+ });
382
+ const PatternFieldsCMAShapeSchema = z
383
+ .object({
340
384
  componentTree: localeWrapper(ComponentTreeSchema),
341
385
  dataSource: localeWrapper(DataSourceSchema),
342
386
  unboundValues: localeWrapper(UnboundValuesSchema),
343
387
  usedComponents: localeWrapper(UsedComponentsSchema).optional(),
344
388
  componentSettings: localeWrapper(ComponentSettingsSchema),
389
+ })
390
+ .superRefine((patternFields, ctx) => {
391
+ const { componentTree, componentSettings } = patternFields;
392
+ // values at this point are wrapped under locale code
393
+ const nonLocalisedComponentTree = Object.values(componentTree)[0];
394
+ const nonLocalisedComponentSettings = Object.values(componentSettings)[0];
395
+ if (!nonLocalisedComponentSettings || !nonLocalisedComponentTree) {
396
+ return;
397
+ }
398
+ validatePassToNodes(nonLocalisedComponentTree.children || [], nonLocalisedComponentSettings || {}, ctx);
345
399
  });
400
+ const validateAtMostOneNativeParameterDefinition = (parameterDefinitions, ctx) => {
401
+ const nativeParamDefinitions = Object.values(parameterDefinitions).filter((paramDefinition) => !(paramDefinition.passToNodes && paramDefinition.passToNodes.length > 0));
402
+ if (nativeParamDefinitions.length > 1) {
403
+ ctx.addIssue({
404
+ code: z.ZodIssueCode.custom,
405
+ message: `Only one native parameter definition (parameter definition without passToNodes) is allowed per prebinding definition.`,
406
+ });
407
+ }
408
+ };
409
+ const validateNoOverlapBetweenMappingAndOverrides = (variableMappings, allowedVariableOverrides, ctx) => {
410
+ const variableMappingKeys = Object.keys(variableMappings || {});
411
+ const overridesSet = new Set(allowedVariableOverrides || []);
412
+ const overlap = variableMappingKeys.filter((key) => overridesSet.has(key));
413
+ if (overlap.length > 0) {
414
+ ctx.addIssue({
415
+ code: z.ZodIssueCode.custom,
416
+ message: `Found both variable mapping and allowed override for the following keys: ${overlap.map((key) => `"${key}"`).join(', ')}.`,
417
+ });
418
+ }
419
+ };
420
+ const validateMappingsAgainstVariableDefinitions = (variableMappings, allowedVariableOverrides, variableDefinitions, ctx) => {
421
+ const nonDesignVariableDefinitionKeys = Object.entries(variableDefinitions)
422
+ .filter(([_, def]) => def.group !== 'style')
423
+ .map(([key]) => key);
424
+ const variableMappingKeys = Object.keys(variableMappings || {});
425
+ const allKeys = [...variableMappingKeys, ...(allowedVariableOverrides || [])];
426
+ const invalidMappings = allKeys.filter((key) => !nonDesignVariableDefinitionKeys.includes(key));
427
+ if (invalidMappings.length > 0) {
428
+ ctx.addIssue({
429
+ code: z.ZodIssueCode.custom,
430
+ message: `The following variable mappings or overrides are missing from the variable definitions: ${invalidMappings.map((key) => `"${key}"`).join(', ')}.`,
431
+ });
432
+ }
433
+ };
434
+ const validateMappingsAgainstParameterDefinitions = (variableMappings, parameterDefinitions, ctx) => {
435
+ const parameterDefinitionKeys = Object.keys(parameterDefinitions || {});
436
+ for (const [mappingKey, mappingValue] of Object.entries(variableMappings || {})) {
437
+ if (!parameterDefinitionKeys.includes(mappingValue.parameterId)) {
438
+ ctx.addIssue({
439
+ code: z.ZodIssueCode.custom,
440
+ message: `The variable mapping with id "${mappingKey}" references a non-existing parameterId "${mappingValue.parameterId}".`,
441
+ });
442
+ }
443
+ }
444
+ };
445
+ const validatePassToNodes = (rootChildren, componentSettings, ctx) => {
446
+ if (!componentSettings.prebindingDefinitions ||
447
+ componentSettings.prebindingDefinitions.length === 0) {
448
+ return;
449
+ }
450
+ const { parameterDefinitions } = componentSettings.prebindingDefinitions[0];
451
+ let nodeIds = new Set();
452
+ for (const paramDef of Object.values(parameterDefinitions || {})) {
453
+ paramDef.passToNodes?.forEach((n) => nodeIds.add(n.nodeId));
454
+ }
455
+ treeVisit(rootChildren, (node) => {
456
+ if (!node.id)
457
+ return;
458
+ if (nodeIds.has(node.id)) {
459
+ nodeIds.delete(node.id);
460
+ }
461
+ });
462
+ if (nodeIds.size > 0) {
463
+ const stringifiedNodeIds = Array.from(nodeIds)
464
+ .map((id) => `"${id}"`)
465
+ .join(', ');
466
+ ctx.addIssue({
467
+ code: z.ZodIssueCode.custom,
468
+ message: `The following node IDs referenced in passToNodes are not present in the component tree: ${stringifiedNodeIds}.`,
469
+ });
470
+ }
471
+ };
346
472
 
347
473
  const ComponentDefinitionSchema = z
348
474
  .object({