@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 4.45 kB
Source Map (JSON)
{"version":3,"file":"check-field-conditions.mjs","names":[],"sources":["../../../../src/libs/config/checks/check-field-conditions.ts"],"sourcesContent":["import LucidError from \"../../../utils/errors/lucid-error.js\";\nimport type FieldBuilder from \"../../collection/builders/field-builder/index.js\";\nimport type CustomField from \"../../collection/custom-fields/custom-field.js\";\nimport type {\n\tFieldConditionConfig,\n\tFieldTypes,\n\tFieldUIConfig,\n} from \"../../collection/custom-fields/types.js\";\nimport { translate } from \"../../i18n/index.js\";\n\nconst getFieldCondition = (\n\tfield: CustomField<FieldTypes>,\n): FieldConditionConfig | undefined => {\n\treturn (field.config as { ui?: FieldUIConfig }).ui?.condition;\n};\n\n/**\n * Returns the scope keys a field can resolve condition targets against:\n * its own tree scope followed by each ancestor repeater scope up to the root.\n */\nconst ancestorScopeKeys = (\n\tfields: Map<string, CustomField<FieldTypes>>,\n\tfield: CustomField<FieldTypes>,\n): Array<string | null> => {\n\tconst scopes: Array<string | null> = [];\n\tlet parent = field.treeParent;\n\n\twhile (parent !== null) {\n\t\tscopes.push(parent);\n\t\tparent = fields.get(parent)?.treeParent ?? null;\n\t}\n\n\tscopes.push(null);\n\treturn scopes;\n};\n\n/**\n * Validates that field conditions only reference resolvable targets: a field\n * in the same tree that is a direct sibling or lives in an ancestor scope.\n */\nconst checkFieldConditions = (\n\ttype: \"brick\" | \"collection\",\n\ttypeKey: string,\n\tbuilder: FieldBuilder,\n) => {\n\tfor (const [key, field] of builder.fields) {\n\t\tconst condition = getFieldCondition(field);\n\t\tif (!condition) continue;\n\n\t\tfor (const rules of condition.groups ?? []) {\n\t\t\tfor (const rule of rules) {\n\t\t\t\tconst errorData = {\n\t\t\t\t\tfield: key,\n\t\t\t\t\ttarget: rule.field,\n\t\t\t\t\ttype: type,\n\t\t\t\t\ttypeKey: typeKey,\n\t\t\t\t};\n\n\t\t\t\tif (rule.field === key) {\n\t\t\t\t\tthrow new LucidError({\n\t\t\t\t\t\tmessage: translate(\"server:core.fields.condition.target.self\", {\n\t\t\t\t\t\t\tdata: errorData,\n\t\t\t\t\t\t}),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst target = builder.fields.get(rule.field);\n\t\t\t\tif (!target) {\n\t\t\t\t\tthrow new LucidError({\n\t\t\t\t\t\tmessage: translate(\n\t\t\t\t\t\t\t\"server:core.fields.condition.target.not.found\",\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdata: errorData,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (\n\t\t\t\t\ttarget.type === \"repeater\" ||\n\t\t\t\t\ttarget.type === \"tab\" ||\n\t\t\t\t\ttarget.type === \"section\" ||\n\t\t\t\t\ttarget.type === \"collapsible\"\n\t\t\t\t) {\n\t\t\t\t\tthrow new LucidError({\n\t\t\t\t\t\tmessage: translate(\n\t\t\t\t\t\t\t\"server:core.fields.condition.target.invalid.type\",\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\t...errorData,\n\t\t\t\t\t\t\t\t\ttargetType: target.type,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst scopes = ancestorScopeKeys(builder.fields, field);\n\t\t\t\tif (!scopes.includes(target.treeParent)) {\n\t\t\t\t\tthrow new LucidError({\n\t\t\t\t\t\tmessage: translate(\n\t\t\t\t\t\t\t\"server:core.fields.condition.target.out.of.scope\",\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdata: errorData,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};\n\nexport default checkFieldConditions;\n"],"mappings":"0GAUA,MAAM,EACL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