UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.23 kB
{"version":3,"file":"relation-field-values.mjs","names":[],"sources":["../../../../../../../src/libs/collection/custom-fields/fields/relation/utils/relation-field-values.ts"],"sourcesContent":["import type { RelationCustomFieldValue } from \"../types.js\";\n\n/**\n * Guards the relation field value shape used by relation-table storage.\n */\nexport const isRelationCustomFieldValue = (\n\tvalue: unknown,\n): value is RelationCustomFieldValue => {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tif (!(\"id\" in value) || !(\"collectionKey\" in value)) return false;\n\n\treturn (\n\t\ttypeof value.id === \"number\" &&\n\t\tNumber.isFinite(value.id) &&\n\t\ttypeof value.collectionKey === \"string\"\n\t);\n};\n\n/**\n * Normalizes raw relation field input into the stored array shape.\n */\nexport const normalizeStoredRelationCustomFieldValues = (\n\tvalue: unknown,\n\tmultiple?: boolean,\n): RelationCustomFieldValue[] => {\n\tif (!Array.isArray(value)) return [];\n\n\tconst normalizedValues = value.reduce<RelationCustomFieldValue[]>(\n\t\t(acc, item) => {\n\t\t\tif (!isRelationCustomFieldValue(item)) return acc;\n\n\t\t\tacc.push({\n\t\t\t\tid: item.id,\n\t\t\t\tcollectionKey: item.collectionKey,\n\t\t\t});\n\t\t\treturn acc;\n\t\t},\n\t\t[],\n\t);\n\n\tif (multiple === true) return normalizedValues;\n\treturn normalizedValues.slice(0, 1);\n};\n\n/**\n * Preserves invalid non-array input for validation while still enforcing the\n * configured cardinality on valid array payloads.\n */\nexport const clampRelationFieldInput = (\n\tvalue: unknown,\n\tmultiple?: boolean,\n): unknown => {\n\tif (!Array.isArray(value)) return value;\n\tif (multiple === true) return value;\n\treturn value.slice(0, 1);\n};\n"],"mappings":"AAKA,MAAa,EACZ,GAEI,OAAO,GAAU,WAAY,GAC7B,EAAE,OAAQ,IAAU,EAAE,kBAAmB,GAAe,GAG3D,OAAO,EAAM,IAAO,UACpB,OAAO,SAAS,EAAM,EAAE,GACxB,OAAO,EAAM,eAAkB,SAOpB,GACZ,EACA,IACgC,CAChC,GAAI,CAAC,MAAM,QAAQ,CAAK,EAAG,MAAO,CAAC,EAEnC,IAAM,EAAmB,EAAM,QAC7B,EAAK,KACA,EAA2B,CAAI,GAEpC,EAAI,KAAK,CACR,GAAI,EAAK,GACT,cAAe,EAAK,aACrB,CAAC,EAL6C,GAQ/C,CAAC,CACF,EAGA,OADI,IAAa,GAAa,EACvB,EAAiB,MAAM,EAAG,CAAC,CACnC,EAMa,GACZ,EACA,IAEI,CAAC,MAAM,QAAQ,CAAK,GACpB,IAAa,GAAa,EACvB,EAAM,MAAM,EAAG,CAAC"}