@contentful/experiences-components-react 3.6.2-dev-20250912T1534-a44407f.0 → 3.7.0-prerelease-20250915T1724-8825648.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.js CHANGED
@@ -1393,6 +1393,25 @@ z.object({
1393
1393
  usedComponents: localeWrapper(UsedComponentsSchema).optional(),
1394
1394
  });
1395
1395
 
1396
+ function treeVisit$1(initialNode, onNode) {
1397
+ const _treeVisit = (currentNode) => {
1398
+ const children = [...currentNode.children];
1399
+ onNode(currentNode);
1400
+ for (const child of children) {
1401
+ _treeVisit(child);
1402
+ }
1403
+ };
1404
+ if (Array.isArray(initialNode)) {
1405
+ for (const node of initialNode) {
1406
+ _treeVisit(node);
1407
+ }
1408
+ }
1409
+ else {
1410
+ _treeVisit(initialNode);
1411
+ }
1412
+ }
1413
+
1414
+ const MAX_ALLOWED_PATHS = 200;
1396
1415
  const THUMBNAIL_IDS = [
1397
1416
  'columns',
1398
1417
  'columnsPlusRight',
@@ -1423,7 +1442,17 @@ const THUMBNAIL_IDS = [
1423
1442
  const VariableMappingSchema = z.object({
1424
1443
  parameterId: propertyKeySchema,
1425
1444
  type: z.literal('ContentTypeMapping'),
1426
- pathsByContentType: z.record(z.string(), z.object({ path: z.string() })),
1445
+ pathsByContentType: z
1446
+ .record(z.string(), z.object({ path: z.string() }))
1447
+ .superRefine((paths, ctx) => {
1448
+ const variableId = ctx.path[ctx.path.length - 2];
1449
+ if (Object.keys(paths).length > MAX_ALLOWED_PATHS) {
1450
+ ctx.addIssue({
1451
+ code: z.ZodIssueCode.custom,
1452
+ message: `Too many paths defined for variable mapping with id "${variableId}", maximum allowed is ${MAX_ALLOWED_PATHS}`,
1453
+ });
1454
+ }
1455
+ }),
1427
1456
  });
1428
1457
  const PassToNodeSchema = z
1429
1458
  .object({
@@ -1447,7 +1476,10 @@ const ParameterDefinitionSchema = z.object({
1447
1476
  })
1448
1477
  .optional(),
1449
1478
  contentTypes: z.array(z.string()).min(1),
1450
- passToNodes: z.array(PassToNodeSchema).optional(),
1479
+ passToNodes: z
1480
+ .array(PassToNodeSchema)
1481
+ .max(1, 'At most one "passToNodes" element is allowed per parameter definition.')
1482
+ .optional(), // we might change this to be empty array for native parameter definitions, that's why we don't use .length(1)
1451
1483
  });
1452
1484
  const ParameterDefinitionsSchema = z.record(propertyKeySchema, ParameterDefinitionSchema);
1453
1485
  const VariableMappingsSchema = z.record(propertyKeySchema, VariableMappingSchema);
@@ -1468,14 +1500,107 @@ const ComponentSettingsSchema = z
1468
1500
  category: z.string().max(50, 'Category must contain at most 50 characters').optional(),
1469
1501
  prebindingDefinitions: z.array(PrebindingDefinitionSchema).length(1).optional(),
1470
1502
  })
1471
- .strict();
1472
- z.object({
1503
+ .strict()
1504
+ .superRefine((componentSettings, ctx) => {
1505
+ const { variableDefinitions, prebindingDefinitions } = componentSettings;
1506
+ if (!prebindingDefinitions || prebindingDefinitions.length === 0) {
1507
+ return;
1508
+ }
1509
+ const { parameterDefinitions, variableMappings, allowedVariableOverrides } = prebindingDefinitions[0];
1510
+ validateAtMostOneNativeParameterDefinition(parameterDefinitions, ctx);
1511
+ validateNoOverlapBetweenMappingAndOverrides(variableMappings, allowedVariableOverrides, ctx);
1512
+ validateMappingsAgainstVariableDefinitions(variableMappings, allowedVariableOverrides, variableDefinitions, ctx);
1513
+ validateMappingsAgainstParameterDefinitions(variableMappings, parameterDefinitions, ctx);
1514
+ });
1515
+ z
1516
+ .object({
1473
1517
  componentTree: localeWrapper(ComponentTreeSchema),
1474
1518
  dataSource: localeWrapper(DataSourceSchema),
1475
1519
  unboundValues: localeWrapper(UnboundValuesSchema),
1476
1520
  usedComponents: localeWrapper(UsedComponentsSchema).optional(),
1477
1521
  componentSettings: localeWrapper(ComponentSettingsSchema),
1522
+ })
1523
+ .superRefine((patternFields, ctx) => {
1524
+ const { componentTree, componentSettings } = patternFields;
1525
+ // values at this point are wrapped under locale code
1526
+ const nonLocalisedComponentTree = Object.values(componentTree)[0];
1527
+ const nonLocalisedComponentSettings = Object.values(componentSettings)[0];
1528
+ if (!nonLocalisedComponentSettings || !nonLocalisedComponentTree) {
1529
+ return;
1530
+ }
1531
+ validatePassToNodes(nonLocalisedComponentTree.children || [], nonLocalisedComponentSettings || {}, ctx);
1478
1532
  });
1533
+ const validateAtMostOneNativeParameterDefinition = (parameterDefinitions, ctx) => {
1534
+ const nativeParamDefinitions = Object.values(parameterDefinitions).filter((paramDefinition) => !(paramDefinition.passToNodes && paramDefinition.passToNodes.length > 0));
1535
+ if (nativeParamDefinitions.length > 1) {
1536
+ ctx.addIssue({
1537
+ code: z.ZodIssueCode.custom,
1538
+ message: `Only one native parameter definition (parameter definition without passToNodes) is allowed per prebinding definition.`,
1539
+ });
1540
+ }
1541
+ };
1542
+ const validateNoOverlapBetweenMappingAndOverrides = (variableMappings, allowedVariableOverrides, ctx) => {
1543
+ const variableMappingKeys = Object.keys(variableMappings || {});
1544
+ const overlap = variableMappingKeys.filter((key) => allowedVariableOverrides?.includes(key));
1545
+ if (overlap.length > 0) {
1546
+ ctx.addIssue({
1547
+ code: z.ZodIssueCode.custom,
1548
+ message: `Found both variable mapping and allowed override for the following keys: ${overlap.map((key) => `"${key}"`).join(', ')}.`,
1549
+ });
1550
+ }
1551
+ };
1552
+ const validateMappingsAgainstVariableDefinitions = (variableMappings, allowedVariableOverrides, variableDefinitions, ctx) => {
1553
+ const nonDesignVariableDefinitionKeys = Object.entries(variableDefinitions)
1554
+ .filter(([_, def]) => def.group !== 'style')
1555
+ .map(([key]) => key);
1556
+ const variableMappingKeys = Object.keys(variableMappings || {});
1557
+ const allKeys = [...variableMappingKeys, ...(allowedVariableOverrides || [])];
1558
+ const invalidMappings = allKeys.filter((key) => !nonDesignVariableDefinitionKeys.includes(key));
1559
+ if (invalidMappings.length > 0) {
1560
+ ctx.addIssue({
1561
+ code: z.ZodIssueCode.custom,
1562
+ message: `The following variable mappings or overrides are missing from the variable definitions: ${invalidMappings.map((key) => `"${key}"`).join(', ')}.`,
1563
+ });
1564
+ }
1565
+ };
1566
+ const validateMappingsAgainstParameterDefinitions = (variableMappings, parameterDefinitions, ctx) => {
1567
+ const parameterDefinitionKeys = Object.keys(parameterDefinitions || {});
1568
+ for (const [mappingKey, mappingValue] of Object.entries(variableMappings || {})) {
1569
+ if (!parameterDefinitionKeys.includes(mappingValue.parameterId)) {
1570
+ ctx.addIssue({
1571
+ code: z.ZodIssueCode.custom,
1572
+ message: `The variable mapping with id "${mappingKey}" references a non-existing parameterId "${mappingValue.parameterId}".`,
1573
+ });
1574
+ }
1575
+ }
1576
+ };
1577
+ const validatePassToNodes = (rootChildren, componentSettings, ctx) => {
1578
+ if (!componentSettings.prebindingDefinitions ||
1579
+ componentSettings.prebindingDefinitions.length === 0) {
1580
+ return;
1581
+ }
1582
+ const { parameterDefinitions } = componentSettings.prebindingDefinitions[0];
1583
+ let nodeIds = new Set();
1584
+ for (const paramDef of Object.values(parameterDefinitions || {})) {
1585
+ paramDef.passToNodes?.forEach((n) => nodeIds.add(n.nodeId));
1586
+ }
1587
+ treeVisit$1(rootChildren, (node) => {
1588
+ if (!node.id)
1589
+ return;
1590
+ if (nodeIds.has(node.id)) {
1591
+ nodeIds.delete(node.id);
1592
+ }
1593
+ });
1594
+ if (nodeIds.size > 0) {
1595
+ const stringifiedNodeIds = Array.from(nodeIds)
1596
+ .map((id) => `"${id}"`)
1597
+ .join(', ');
1598
+ ctx.addIssue({
1599
+ code: z.ZodIssueCode.custom,
1600
+ message: `The following node IDs referenced in passToNodes are not present in the component tree: ${stringifiedNodeIds}.`,
1601
+ });
1602
+ }
1603
+ };
1479
1604
 
1480
1605
  z
1481
1606
  .object({