@strapi/content-manager 5.31.3 → 5.32.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/admin/pages/EditView/components/FormInputs/BlocksInput/BlocksEditor.js +16 -4
- package/dist/admin/pages/EditView/components/FormInputs/BlocksInput/BlocksEditor.js.map +1 -1
- package/dist/admin/pages/EditView/components/FormInputs/BlocksInput/BlocksEditor.mjs +17 -6
- package/dist/admin/pages/EditView/components/FormInputs/BlocksInput/BlocksEditor.mjs.map +1 -1
- package/dist/admin/pages/EditView/utils/data.js +3 -11
- package/dist/admin/pages/EditView/utils/data.js.map +1 -1
- package/dist/admin/pages/EditView/utils/data.mjs +3 -11
- package/dist/admin/pages/EditView/utils/data.mjs.map +1 -1
- package/dist/admin/src/pages/EditView/components/FormInputs/BlocksInput/BlocksEditor.d.ts +7 -2
- package/dist/admin/utils/validation.js +1 -1
- package/dist/admin/utils/validation.js.map +1 -1
- package/dist/admin/utils/validation.mjs +1 -1
- package/dist/admin/utils/validation.mjs.map +1 -1
- package/dist/server/services/document-metadata.js +17 -4
- package/dist/server/services/document-metadata.js.map +1 -1
- package/dist/server/services/document-metadata.mjs +17 -4
- package/dist/server/services/document-metadata.mjs.map +1 -1
- package/dist/server/src/services/document-metadata.d.ts.map +1 -1
- package/package.json +6 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.mjs","sources":["../../../../../admin/src/pages/EditView/utils/data.ts"],"sourcesContent":["import { createRulesEngine } from '@strapi/admin/strapi-admin';\nimport { generateNKeysBetween } from 'fractional-indexing';\nimport pipe from 'lodash/fp/pipe';\n\nimport { DOCUMENT_META_FIELDS } from '../../../constants/attributes';\n\nimport type { ComponentsDictionary, Document } from '../../../hooks/useDocument';\nimport type { Schema, UID } from '@strapi/types';\n\n/* -------------------------------------------------------------------------------------------------\n * traverseData\n * -----------------------------------------------------------------------------------------------*/\n\n// Make only attributes required since it's the only one Content History has\ntype PartialSchema = Partial<Schema.Schema> & Pick<Schema.Schema, 'attributes'>;\n\ntype Predicate = <TAttribute extends Schema.Attribute.AnyAttribute>(\n attribute: TAttribute,\n value: Schema.Attribute.Value<TAttribute>\n) => boolean;\ntype Transform = <TAttribute extends Schema.Attribute.AnyAttribute>(\n value: any,\n attribute: TAttribute\n) => any;\ntype AnyData = Omit<Document, 'id'>;\n\nconst BLOCK_LIST_ATTRIBUTE_KEYS = ['__component', '__temp_key__'];\n\n/**\n * @internal This function is used to traverse the data and transform the values.\n * Given a predicate function, it will transform the value (using the given transform function)\n * if the predicate returns true. If it finds that the attribute is a component or dynamiczone,\n * it will recursively traverse those data structures as well.\n *\n * It is possible to break the ContentManager by using this function incorrectly, for example,\n * if you transform a number into a string but the attribute type is a number, the ContentManager\n * will not be able to save the data and the Form will likely crash because the component it's\n * passing the data too won't succesfully be able to handle the value.\n */\nconst traverseData =\n (predicate: Predicate, transform: Transform) =>\n (schema: PartialSchema, components: ComponentsDictionary = {}) =>\n (data: AnyData = {}) => {\n const traverse = (datum: AnyData, attributes: Schema.Schema['attributes']) => {\n return Object.entries(datum).reduce<AnyData>((acc, [key, value]) => {\n const attribute = attributes[key];\n\n /**\n * If the attribute is a block list attribute, we don't want to transform it.\n * We also don't want to transform null or undefined values.\n */\n if (BLOCK_LIST_ATTRIBUTE_KEYS.includes(key) || value === null || value === undefined) {\n acc[key] = value;\n return acc;\n }\n\n if (attribute.type === 'component') {\n if (attribute.repeatable) {\n const componentValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, true>>;\n acc[key] = componentValue.map((componentData) =>\n traverse(componentData, components[attribute.component]?.attributes ?? {})\n );\n } else {\n const componentValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, false>>;\n\n acc[key] = traverse(componentValue, components[attribute.component]?.attributes ?? {});\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamicZoneValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n\n acc[key] = dynamicZoneValue.map((componentData) =>\n traverse(componentData, components[componentData.__component]?.attributes ?? {})\n );\n } else if (predicate(attribute, value)) {\n acc[key] = transform(value, attribute);\n } else {\n acc[key] = value;\n }\n\n return acc;\n }, {});\n };\n\n return traverse(data, schema.attributes);\n };\n\n/* -------------------------------------------------------------------------------------------------\n * removeProhibitedFields\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal Removes all the fields that are not allowed.\n */\nconst removeProhibitedFields = (prohibitedFields: Schema.Attribute.Kind[]) =>\n traverseData(\n (attribute) => prohibitedFields.includes(attribute.type),\n () => ''\n );\n\n/* -------------------------------------------------------------------------------------------------\n * prepareRelations\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Sets all relation values to an empty array.\n */\nconst prepareRelations = traverseData(\n (attribute) => attribute.type === 'relation',\n () => ({\n connect: [],\n disconnect: [],\n })\n);\n\n/* -------------------------------------------------------------------------------------------------\n * prepareTempKeys\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Adds a `__temp_key__` to each component and dynamiczone item. This gives us\n * a stable identifier regardless of its ids etc. that we can then use for drag and drop.\n */\nconst prepareTempKeys = traverseData(\n (attribute) =>\n (attribute.type === 'component' && attribute.repeatable) || attribute.type === 'dynamiczone',\n (data) => {\n if (Array.isArray(data) && data.length > 0) {\n const keys = generateNKeysBetween(undefined, undefined, data.length);\n\n return data.map((datum, index) => ({\n ...datum,\n __temp_key__: keys[index],\n }));\n }\n\n return data;\n }\n);\n\n/* -------------------------------------------------------------------------------------------------\n * removeFieldsThatDontExistOnSchema\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Fields that don't exist in the schema like createdAt etc. are only on the first level (not nested),\n * as such we don't need to traverse the components to remove them.\n */\nconst removeFieldsThatDontExistOnSchema = (schema: PartialSchema) => (data: AnyData) => {\n const schemaKeys = Object.keys(schema.attributes);\n const dataKeys = Object.keys(data);\n\n const keysToRemove = dataKeys.filter((key) => !schemaKeys.includes(key));\n\n const revisedData = [...keysToRemove, ...DOCUMENT_META_FIELDS].reduce((acc, key) => {\n delete acc[key];\n\n return acc;\n }, structuredClone(data));\n\n return revisedData;\n};\n\n/**\n * @internal\n * @description We need to remove null fields from the data-structure because it will pass it\n * to the specific inputs breaking them as most would prefer empty strings or `undefined` if\n * they're controlled / uncontrolled.\n */\nconst removeNullValues = (data: AnyData) => {\n return Object.entries(data).reduce<AnyData>((acc, [key, value]) => {\n if (value === null) {\n return acc;\n }\n\n acc[key] = value;\n\n return acc;\n }, {});\n};\n\n/* -------------------------------------------------------------------------------------------------\n * transformDocuments\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Takes a document data structure (this could be from the API or a default form structure)\n * and applies consistent data transformations to it. This is also used when we add new components to the\n * form to ensure the data is correctly prepared from their default state e.g. relations are set to an empty array.\n */\nconst transformDocument =\n (schema: PartialSchema, components: ComponentsDictionary = {}) =>\n (document: AnyData) => {\n const transformations = pipe(\n removeFieldsThatDontExistOnSchema(schema),\n removeProhibitedFields(['password'])(schema, components),\n removeNullValues,\n prepareRelations(schema, components),\n prepareTempKeys(schema, components)\n );\n\n return transformations(document);\n };\n\ntype HandleOptions = {\n schema?: Schema.ContentType | Schema.Component;\n initialValues?: AnyData;\n components?: Record<string, Schema.Component>;\n};\n\ntype RemovedFieldPath = string;\n\n/**\n * @internal\n * @description Finds the initial value for a component or dynamic zone item (based on its __temp_key__ and not its index).\n * @param initialValue - The initial values object.\n * @param item - The item to find the initial value for.\n * @returns The initial value for the item.\n */\nconst getItemInitialValue = (initialValue: AnyData, item: AnyData) => {\n if (initialValue && Array.isArray(initialValue)) {\n const matchingInitialItem = initialValue.find(\n (initialItem) => initialItem.__temp_key__ === item.__temp_key__\n );\n if (matchingInitialItem) {\n return matchingInitialItem;\n }\n }\n return {};\n};\n\n/**\n * @internal\n * @description Collects paths of attributes that should be removed based on visibility conditions.\n * This function only evaluates conditions.visible (JSON Logic), not the visible boolean property.\n *\n * @param data - The data object to evaluate\n * @param schema - The content type schema\n * @param components - Dictionary of component schemas\n * @param path - Current path in the data structure (for nested components/dynamiczones)\n * @returns Array of field paths that should be removed\n */\nconst collectInvisibleAttributes = (\n data: AnyData,\n schema: Schema.ContentType | Schema.Component | undefined,\n components: Record<string, Schema.Component>,\n path: string[] = []\n): RemovedFieldPath[] => {\n if (!schema?.attributes) return [];\n\n const rulesEngine = createRulesEngine();\n const removedPaths: RemovedFieldPath[] = [];\n const evaluatedData: AnyData = {};\n\n for (const [attrName, attrDef] of Object.entries(schema.attributes)) {\n const fullPath = [...path, attrName].join('.');\n\n // Skip fields with visible: false - they're managed by backend\n if ('visible' in attrDef && attrDef.visible === false) {\n continue;\n }\n\n const condition = attrDef?.conditions?.visible;\n const isVisible = condition\n ? rulesEngine.evaluate(condition, { ...data, ...evaluatedData })\n : true;\n\n if (!isVisible) {\n removedPaths.push(fullPath);\n continue;\n }\n\n // Track this field for future condition evaluations\n if (attrName in data) {\n evaluatedData[attrName] = data[attrName];\n }\n\n // Recursively process components\n if (attrDef.type === 'component') {\n const compSchema = components[attrDef.component];\n const value = data[attrName];\n\n if (attrDef.repeatable && Array.isArray(value)) {\n value.forEach((item) => {\n const nestedPaths = collectInvisibleAttributes(item, compSchema, components, [\n ...path,\n `${attrName}[${item.__temp_key__}]`,\n ]);\n removedPaths.push(...nestedPaths);\n });\n } else if (value && typeof value === 'object') {\n const nestedPaths = collectInvisibleAttributes(value, compSchema, components, [\n ...path,\n attrName,\n ]);\n removedPaths.push(...nestedPaths);\n }\n }\n\n // Recursively process dynamic zones\n if (attrDef.type === 'dynamiczone' && Array.isArray(data[attrName])) {\n data[attrName].forEach((dzItem: AnyData) => {\n const compUID = dzItem?.__component;\n const compSchema = components[compUID];\n const nestedPaths = collectInvisibleAttributes(dzItem, compSchema, components, [\n ...path,\n `${attrName}[${dzItem.__temp_key__}]`,\n ]);\n removedPaths.push(...nestedPaths);\n });\n }\n }\n\n return removedPaths;\n};\n\n/**\n * @internal\n * @description Removes attributes from data based on the list of paths to remove.\n * Preserves fields with visible: false from data or initialValues.\n *\n * @param data - The data object to filter\n * @param initialValues - Initial values to fall back to\n * @param schema - The content type schema\n * @param components - Dictionary of component schemas\n * @param removedPaths - Array of field paths to remove\n * @param currentPath - Current path in the data structure\n * @returns Filtered data object\n */\nconst filterDataByRemovedPaths = (\n data: AnyData,\n initialValues: AnyData,\n schema: Schema.ContentType | Schema.Component | undefined,\n components: Record<string, Schema.Component>,\n removedPaths: RemovedFieldPath[],\n currentPath: string[] = []\n): AnyData => {\n if (!schema?.attributes) return data;\n\n const result: AnyData = {};\n\n for (const [attrName, attrDef] of Object.entries(schema.attributes)) {\n const fullPath = [...currentPath, attrName].join('.');\n\n // Check if this field should be removed\n if (removedPaths.includes(fullPath)) {\n continue;\n }\n\n // Handle fields with visible: false - preserve from data or initialValues\n if ('visible' in attrDef && attrDef.visible === false) {\n const userProvided = Object.hasOwn(data, attrName);\n if (userProvided) {\n result[attrName] = data[attrName];\n } else if (attrName in initialValues) {\n result[attrName] = initialValues[attrName];\n }\n continue;\n }\n\n const userProvided = Object.hasOwn(data, attrName);\n const currentValue = userProvided ? data[attrName] : undefined;\n const initialValue = initialValues?.[attrName];\n\n // Handle components\n if (attrDef.type === 'component') {\n const compSchema = components[attrDef.component];\n const value = currentValue === undefined ? initialValue : currentValue;\n\n if (!value) {\n result[attrName] = attrDef.repeatable ? [] : null;\n continue;\n }\n\n if (attrDef.repeatable && Array.isArray(value)) {\n result[attrName] = value.map((item) => {\n const componentInitialValue = getItemInitialValue(initialValue, item);\n return filterDataByRemovedPaths(\n item,\n componentInitialValue,\n compSchema,\n components,\n removedPaths,\n [...currentPath, `${attrName}[${item.__temp_key__}]`]\n );\n });\n } else {\n result[attrName] = filterDataByRemovedPaths(\n value,\n initialValue ?? {},\n compSchema,\n components,\n removedPaths,\n [...currentPath, attrName]\n );\n }\n\n continue;\n }\n\n // Handle dynamic zones\n if (attrDef.type === 'dynamiczone') {\n if (!Array.isArray(currentValue)) {\n result[attrName] = [];\n continue;\n }\n\n result[attrName] = currentValue.map((dzItem) => {\n const compUID = dzItem?.__component;\n const compSchema = components[compUID];\n const componentInitialValue = getItemInitialValue(initialValue, dzItem);\n\n const cleaned = filterDataByRemovedPaths(\n dzItem,\n componentInitialValue,\n compSchema,\n components,\n removedPaths,\n [...currentPath, `${attrName}[${dzItem.__temp_key__}]`]\n );\n\n // For newly created components, ensure id is undefined (in case of reordering)\n const processedItem =\n dzItem.id === undefined || dzItem.id === null\n ? { __component: compUID, ...cleaned, id: undefined }\n : { __component: compUID, ...cleaned };\n\n return processedItem;\n });\n\n continue;\n }\n\n // Regular fields - preserve from data or initialValues\n if (currentValue !== undefined) {\n result[attrName] = currentValue;\n } else if (initialValue !== undefined) {\n result[attrName] = initialValue;\n }\n }\n\n // Pass through any fields from data that aren't in the schema\n for (const [key, value] of Object.entries(data)) {\n if (!(key in result) && !(key in (schema?.attributes || {}))) {\n result[key] = value;\n }\n }\n\n return result;\n};\n\n/**\n * Removes values from the data object if their corresponding attribute has a\n * visibility condition that evaluates to false.\n *\n * @param data - The data object to filter based on visibility\n * @param options - Schema, initialValues, and components\n * @returns Object with filtered data and list of removed attribute paths\n */\nconst handleInvisibleAttributes = (\n data: AnyData,\n { schema, initialValues = {}, components = {} }: HandleOptions\n): {\n data: AnyData;\n removedAttributes: RemovedFieldPath[];\n} => {\n if (!schema?.attributes) return { data, removedAttributes: [] };\n\n const removedAttributes = collectInvisibleAttributes(data, schema, components);\n\n const filteredData = filterDataByRemovedPaths(\n data,\n initialValues,\n schema,\n components,\n removedAttributes\n );\n\n return {\n data: filteredData,\n removedAttributes,\n };\n};\n\nexport {\n removeProhibitedFields,\n prepareRelations,\n prepareTempKeys,\n removeFieldsThatDontExistOnSchema,\n transformDocument,\n handleInvisibleAttributes,\n};\nexport type { AnyData };\n"],"names":["BLOCK_LIST_ATTRIBUTE_KEYS","traverseData","predicate","transform","schema","components","data","traverse","datum","attributes","Object","entries","reduce","acc","key","value","attribute","includes","undefined","type","repeatable","componentValue","map","componentData","component","dynamicZoneValue","__component","removeProhibitedFields","prohibitedFields","prepareRelations","connect","disconnect","prepareTempKeys","Array","isArray","length","keys","generateNKeysBetween","index","__temp_key__","removeFieldsThatDontExistOnSchema","schemaKeys","dataKeys","keysToRemove","filter","revisedData","DOCUMENT_META_FIELDS","structuredClone","removeNullValues","transformDocument","document","transformations","pipe","getItemInitialValue","initialValue","item","matchingInitialItem","find","initialItem","collectInvisibleAttributes","path","rulesEngine","createRulesEngine","removedPaths","evaluatedData","attrName","attrDef","fullPath","join","visible","condition","conditions","isVisible","evaluate","push","compSchema","forEach","nestedPaths","dzItem","compUID","filterDataByRemovedPaths","initialValues","currentPath","result","userProvided","hasOwn","currentValue","componentInitialValue","cleaned","processedItem","id","handleInvisibleAttributes","removedAttributes","filteredData"],"mappings":";;;;;AA0BA,MAAMA,yBAA4B,GAAA;AAAC,IAAA,aAAA;AAAe,IAAA;AAAe,CAAA;AAEjE;;;;;;;;;;AAUC,IACD,MAAMC,YAAAA,GACJ,CAACC,SAAAA,EAAsBC,YACvB,CAACC,MAAAA,EAAuBC,UAAmC,GAAA,EAAE,GAC7D,CAACC,IAAAA,GAAgB,EAAE,GAAA;YACjB,MAAMC,QAAAA,GAAW,CAACC,KAAgBC,EAAAA,UAAAA,GAAAA;gBAChC,OAAOC,MAAAA,CAAOC,OAAO,CAACH,KAAOI,CAAAA,CAAAA,MAAM,CAAU,CAACC,GAAAA,EAAK,CAACC,GAAAA,EAAKC,KAAM,CAAA,GAAA;oBAC7D,MAAMC,SAAAA,GAAYP,UAAU,CAACK,GAAI,CAAA;AAEjC;;;YAIA,IAAId,0BAA0BiB,QAAQ,CAACH,QAAQC,KAAU,KAAA,IAAA,IAAQA,UAAUG,SAAW,EAAA;wBACpFL,GAAG,CAACC,IAA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9D,MAAMA,iBAAAA,GAAoBjC,0BAA2BrD,CAAAA,IAAAA,EAAMF,MAAQC,EAAAA,UAAAA,CAAAA;AAEnE,IAAA,MAAMwF,YAAeb,GAAAA,wBAAAA,CACnB1E,IACA2E,EAAAA,aAAAA,EACA7E,QACAC,UACAuF,EAAAA,iBAAAA,CAAAA;IAGF,OAAO;QACLtF,IAAMuF,EAAAA,YAAAA;AACND,QAAAA;AACF,KAAA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"data.mjs","sources":["../../../../../admin/src/pages/EditView/utils/data.ts"],"sourcesContent":["import { createRulesEngine } from '@strapi/admin/strapi-admin';\nimport { generateNKeysBetween } from 'fractional-indexing';\nimport pipe from 'lodash/fp/pipe';\n\nimport { DOCUMENT_META_FIELDS } from '../../../constants/attributes';\n\nimport type { ComponentsDictionary, Document } from '../../../hooks/useDocument';\nimport type { Schema, UID } from '@strapi/types';\n\n/* -------------------------------------------------------------------------------------------------\n * traverseData\n * -----------------------------------------------------------------------------------------------*/\n\n// Make only attributes required since it's the only one Content History has\ntype PartialSchema = Partial<Schema.Schema> & Pick<Schema.Schema, 'attributes'>;\n\ntype Predicate = <TAttribute extends Schema.Attribute.AnyAttribute>(\n attribute: TAttribute,\n value: Schema.Attribute.Value<TAttribute>\n) => boolean;\ntype Transform = <TAttribute extends Schema.Attribute.AnyAttribute>(\n value: any,\n attribute: TAttribute\n) => any;\ntype AnyData = Omit<Document, 'id'>;\n\nconst BLOCK_LIST_ATTRIBUTE_KEYS = ['__component', '__temp_key__'];\n\n/**\n * @internal This function is used to traverse the data and transform the values.\n * Given a predicate function, it will transform the value (using the given transform function)\n * if the predicate returns true. If it finds that the attribute is a component or dynamiczone,\n * it will recursively traverse those data structures as well.\n *\n * It is possible to break the ContentManager by using this function incorrectly, for example,\n * if you transform a number into a string but the attribute type is a number, the ContentManager\n * will not be able to save the data and the Form will likely crash because the component it's\n * passing the data too won't succesfully be able to handle the value.\n */\nconst traverseData =\n (predicate: Predicate, transform: Transform) =>\n (schema: PartialSchema, components: ComponentsDictionary = {}) =>\n (data: AnyData = {}) => {\n const traverse = (datum: AnyData, attributes: Schema.Schema['attributes']) => {\n return Object.entries(datum).reduce<AnyData>((acc, [key, value]) => {\n const attribute = attributes[key];\n\n /**\n * If the attribute is a block list attribute, we don't want to transform it.\n * We also don't want to transform null or undefined values.\n */\n if (BLOCK_LIST_ATTRIBUTE_KEYS.includes(key) || value === null || value === undefined) {\n acc[key] = value;\n return acc;\n }\n\n if (attribute.type === 'component') {\n if (attribute.repeatable) {\n const componentValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, true>>;\n acc[key] = componentValue.map((componentData) =>\n traverse(componentData, components[attribute.component]?.attributes ?? {})\n );\n } else {\n const componentValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, false>>;\n\n acc[key] = traverse(componentValue, components[attribute.component]?.attributes ?? {});\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamicZoneValue = (\n predicate(attribute, value) ? transform(value, attribute) : value\n ) as Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n\n acc[key] = dynamicZoneValue.map((componentData) =>\n traverse(componentData, components[componentData.__component]?.attributes ?? {})\n );\n } else if (predicate(attribute, value)) {\n acc[key] = transform(value, attribute);\n } else {\n acc[key] = value;\n }\n\n return acc;\n }, {});\n };\n\n return traverse(data, schema.attributes);\n };\n\n/* -------------------------------------------------------------------------------------------------\n * removeProhibitedFields\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal Removes all the fields that are not allowed.\n */\nconst removeProhibitedFields = (prohibitedFields: Schema.Attribute.Kind[]) =>\n traverseData(\n (attribute) => prohibitedFields.includes(attribute.type),\n () => ''\n );\n\n/* -------------------------------------------------------------------------------------------------\n * prepareRelations\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Sets all relation values to an empty array.\n */\nconst prepareRelations = traverseData(\n (attribute) => attribute.type === 'relation',\n () => ({\n connect: [],\n disconnect: [],\n })\n);\n\n/* -------------------------------------------------------------------------------------------------\n * prepareTempKeys\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Adds a `__temp_key__` to each component and dynamiczone item. This gives us\n * a stable identifier regardless of its ids etc. that we can then use for drag and drop.\n */\nconst prepareTempKeys = traverseData(\n (attribute) =>\n (attribute.type === 'component' && attribute.repeatable) || attribute.type === 'dynamiczone',\n (data) => {\n if (Array.isArray(data) && data.length > 0) {\n const keys = generateNKeysBetween(undefined, undefined, data.length);\n\n return data.map((datum, index) => ({\n ...datum,\n __temp_key__: keys[index],\n }));\n }\n\n return data;\n }\n);\n\n/* -------------------------------------------------------------------------------------------------\n * removeFieldsThatDontExistOnSchema\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Fields that don't exist in the schema like createdAt etc. are only on the first level (not nested),\n * as such we don't need to traverse the components to remove them.\n */\nconst removeFieldsThatDontExistOnSchema = (schema: PartialSchema) => (data: AnyData) => {\n const schemaKeys = Object.keys(schema.attributes);\n const dataKeys = Object.keys(data);\n\n const keysToRemove = dataKeys.filter((key) => !schemaKeys.includes(key));\n\n const revisedData = [...keysToRemove, ...DOCUMENT_META_FIELDS].reduce((acc, key) => {\n delete acc[key];\n\n return acc;\n }, structuredClone(data));\n\n return revisedData;\n};\n\n/**\n * @internal\n * @description We need to remove null fields from the data-structure because it will pass it\n * to the specific inputs breaking them as most would prefer empty strings or `undefined` if\n * they're controlled / uncontrolled. However, Boolean fields should preserve null values.\n */\nconst removeNullValues = (schema: PartialSchema, components: ComponentsDictionary = {}) =>\n traverseData(\n (attribute, value) => value === null && attribute.type !== 'boolean',\n () => undefined\n )(schema, components);\n\n/* -------------------------------------------------------------------------------------------------\n * transformDocuments\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * @internal\n * @description Takes a document data structure (this could be from the API or a default form structure)\n * and applies consistent data transformations to it. This is also used when we add new components to the\n * form to ensure the data is correctly prepared from their default state e.g. relations are set to an empty array.\n */\nconst transformDocument =\n (schema: PartialSchema, components: ComponentsDictionary = {}) =>\n (document: AnyData) => {\n const transformations = pipe(\n removeFieldsThatDontExistOnSchema(schema),\n removeProhibitedFields(['password'])(schema, components),\n removeNullValues(schema, components),\n prepareRelations(schema, components),\n prepareTempKeys(schema, components)\n );\n\n return transformations(document);\n };\n\ntype HandleOptions = {\n schema?: Schema.ContentType | Schema.Component;\n initialValues?: AnyData;\n components?: Record<string, Schema.Component>;\n};\n\ntype RemovedFieldPath = string;\n\n/**\n * @internal\n * @description Finds the initial value for a component or dynamic zone item (based on its __temp_key__ and not its index).\n * @param initialValue - The initial values object.\n * @param item - The item to find the initial value for.\n * @returns The initial value for the item.\n */\nconst getItemInitialValue = (initialValue: AnyData, item: AnyData) => {\n if (initialValue && Array.isArray(initialValue)) {\n const matchingInitialItem = initialValue.find(\n (initialItem) => initialItem.__temp_key__ === item.__temp_key__\n );\n if (matchingInitialItem) {\n return matchingInitialItem;\n }\n }\n return {};\n};\n\n/**\n * @internal\n * @description Collects paths of attributes that should be removed based on visibility conditions.\n * This function only evaluates conditions.visible (JSON Logic), not the visible boolean property.\n *\n * @param data - The data object to evaluate\n * @param schema - The content type schema\n * @param components - Dictionary of component schemas\n * @param path - Current path in the data structure (for nested components/dynamiczones)\n * @returns Array of field paths that should be removed\n */\nconst collectInvisibleAttributes = (\n data: AnyData,\n schema: Schema.ContentType | Schema.Component | undefined,\n components: Record<string, Schema.Component>,\n path: string[] = []\n): RemovedFieldPath[] => {\n if (!schema?.attributes) return [];\n\n const rulesEngine = createRulesEngine();\n const removedPaths: RemovedFieldPath[] = [];\n const evaluatedData: AnyData = {};\n\n for (const [attrName, attrDef] of Object.entries(schema.attributes)) {\n const fullPath = [...path, attrName].join('.');\n\n // Skip fields with visible: false - they're managed by backend\n if ('visible' in attrDef && attrDef.visible === false) {\n continue;\n }\n\n const condition = attrDef?.conditions?.visible;\n const isVisible = condition\n ? rulesEngine.evaluate(condition, { ...data, ...evaluatedData })\n : true;\n\n if (!isVisible) {\n removedPaths.push(fullPath);\n continue;\n }\n\n // Track this field for future condition evaluations\n if (attrName in data) {\n evaluatedData[attrName] = data[attrName];\n }\n\n // Recursively process components\n if (attrDef.type === 'component') {\n const compSchema = components[attrDef.component];\n const value = data[attrName];\n\n if (attrDef.repeatable && Array.isArray(value)) {\n value.forEach((item) => {\n const nestedPaths = collectInvisibleAttributes(item, compSchema, components, [\n ...path,\n `${attrName}[${item.__temp_key__}]`,\n ]);\n removedPaths.push(...nestedPaths);\n });\n } else if (value && typeof value === 'object') {\n const nestedPaths = collectInvisibleAttributes(value, compSchema, components, [\n ...path,\n attrName,\n ]);\n removedPaths.push(...nestedPaths);\n }\n }\n\n // Recursively process dynamic zones\n if (attrDef.type === 'dynamiczone' && Array.isArray(data[attrName])) {\n data[attrName].forEach((dzItem: AnyData) => {\n const compUID = dzItem?.__component;\n const compSchema = components[compUID];\n const nestedPaths = collectInvisibleAttributes(dzItem, compSchema, components, [\n ...path,\n `${attrName}[${dzItem.__temp_key__}]`,\n ]);\n removedPaths.push(...nestedPaths);\n });\n }\n }\n\n return removedPaths;\n};\n\n/**\n * @internal\n * @description Removes attributes from data based on the list of paths to remove.\n * Preserves fields with visible: false from data or initialValues.\n *\n * @param data - The data object to filter\n * @param initialValues - Initial values to fall back to\n * @param schema - The content type schema\n * @param components - Dictionary of component schemas\n * @param removedPaths - Array of field paths to remove\n * @param currentPath - Current path in the data structure\n * @returns Filtered data object\n */\nconst filterDataByRemovedPaths = (\n data: AnyData,\n initialValues: AnyData,\n schema: Schema.ContentType | Schema.Component | undefined,\n components: Record<string, Schema.Component>,\n removedPaths: RemovedFieldPath[],\n currentPath: string[] = []\n): AnyData => {\n if (!schema?.attributes) return data;\n\n const result: AnyData = {};\n\n for (const [attrName, attrDef] of Object.entries(schema.attributes)) {\n const fullPath = [...currentPath, attrName].join('.');\n\n // Check if this field should be removed\n if (removedPaths.includes(fullPath)) {\n continue;\n }\n\n // Handle fields with visible: false - preserve from data or initialValues\n if ('visible' in attrDef && attrDef.visible === false) {\n const userProvided = Object.hasOwn(data, attrName);\n if (userProvided) {\n result[attrName] = data[attrName];\n } else if (attrName in initialValues) {\n result[attrName] = initialValues[attrName];\n }\n continue;\n }\n\n const userProvided = Object.hasOwn(data, attrName);\n const currentValue = userProvided ? data[attrName] : undefined;\n const initialValue = initialValues?.[attrName];\n\n // Handle components\n if (attrDef.type === 'component') {\n const compSchema = components[attrDef.component];\n const value = currentValue === undefined ? initialValue : currentValue;\n\n if (!value) {\n result[attrName] = attrDef.repeatable ? [] : null;\n continue;\n }\n\n if (attrDef.repeatable && Array.isArray(value)) {\n result[attrName] = value.map((item) => {\n const componentInitialValue = getItemInitialValue(initialValue, item);\n return filterDataByRemovedPaths(\n item,\n componentInitialValue,\n compSchema,\n components,\n removedPaths,\n [...currentPath, `${attrName}[${item.__temp_key__}]`]\n );\n });\n } else {\n result[attrName] = filterDataByRemovedPaths(\n value,\n initialValue ?? {},\n compSchema,\n components,\n removedPaths,\n [...currentPath, attrName]\n );\n }\n\n continue;\n }\n\n // Handle dynamic zones\n if (attrDef.type === 'dynamiczone') {\n if (!Array.isArray(currentValue)) {\n result[attrName] = [];\n continue;\n }\n\n result[attrName] = currentValue.map((dzItem) => {\n const compUID = dzItem?.__component;\n const compSchema = components[compUID];\n const componentInitialValue = getItemInitialValue(initialValue, dzItem);\n\n const cleaned = filterDataByRemovedPaths(\n dzItem,\n componentInitialValue,\n compSchema,\n components,\n removedPaths,\n [...currentPath, `${attrName}[${dzItem.__temp_key__}]`]\n );\n\n // For newly created components, ensure id is undefined (in case of reordering)\n const processedItem =\n dzItem.id === undefined || dzItem.id === null\n ? { __component: compUID, ...cleaned, id: undefined }\n : { __component: compUID, ...cleaned };\n\n return processedItem;\n });\n\n continue;\n }\n\n // Regular fields - preserve from data or initialValues\n if (currentValue !== undefined) {\n result[attrName] = currentValue;\n } else if (initialValue !== undefined) {\n result[attrName] = initialValue;\n }\n }\n\n // Pass through any fields from data that aren't in the schema\n for (const [key, value] of Object.entries(data)) {\n if (!(key in result) && !(key in (schema?.attributes || {}))) {\n result[key] = value;\n }\n }\n\n return result;\n};\n\n/**\n * Removes values from the data object if their corresponding attribute has a\n * visibility condition that evaluates to false.\n *\n * @param data - The data object to filter based on visibility\n * @param options - Schema, initialValues, and components\n * @returns Object with filtered data and list of removed attribute paths\n */\nconst handleInvisibleAttributes = (\n data: AnyData,\n { schema, initialValues = {}, components = {} }: HandleOptions\n): {\n data: AnyData;\n removedAttributes: RemovedFieldPath[];\n} => {\n if (!schema?.attributes) return { data, removedAttributes: [] };\n\n const removedAttributes = collectInvisibleAttributes(data, schema, components);\n\n const filteredData = filterDataByRemovedPaths(\n data,\n initialValues,\n schema,\n components,\n removedAttributes\n );\n\n return {\n data: filteredData,\n removedAttributes,\n };\n};\n\nexport {\n removeProhibitedFields,\n prepareRelations,\n prepareTempKeys,\n removeFieldsThatDontExistOnSchema,\n transformDocument,\n handleInvisibleAttributes,\n};\nexport type { AnyData };\n"],"names":["BLOCK_LIST_ATTRIBUTE_KEYS","traverseData","predicate","transform","schema","components","data","traverse","datum","attributes","Object","entries","reduce","acc","key","value","attribute","includes","undefined","type","repeatable","componentValue","map","componentData","component","dynamicZoneValue","__component","removeProhibitedFields","prohibitedFields","prepareRelations","connect","disconnect","prepareTempKeys","Array","isArray","length","keys","generateNKeysBetween","index","__temp_key__","removeFieldsThatDontExistOnSchema","schemaKeys","dataKeys","keysToRemove","filter","revisedData","DOCUMENT_META_FIELDS","structuredClone","removeNullValues","transformDocument","document","transformations","pipe","getItemInitialValue","initialValue","item","matchingInitialItem","find","initialItem","collectInvisibleAttributes","path","rulesEngine","createRulesEngine","removedPaths","evaluatedData","attrName","attrDef","fullPath","join","visible","condition","conditions","isVisible","evaluate","push","compSchema","forEach","nestedPaths","dzItem","compUID","filterDataByRemovedPaths","initialValues","currentPath","result","userProvided","hasOwn","currentValue","componentInitialValue","cleaned","processedItem","id","handleInvisibleAttributes","removedAttributes","filteredData"],"mappings":";;;;;AA0BA,MAAMA,yBAA4B,GAAA;AAAC,IAAA,aAAA;AAAe,IAAA;AAAe,CAAA;AAEjE;;;;;;;;;;AAUC,IACD,MAAMC,YAAAA,GACJ,CAACC,SAAAA,EAAsBC,YACvB,CAACC,MAAAA,EAAuBC,UAAmC,GAAA,EAAE,GAC7D,CAACC,IAAAA,GAAgB,EAAE,GAAA;YACjB,MAAMC,QAAAA,GAAW,CAACC,KAAgBC,EAAAA,UAAAA,GAAAA;gBAChC,OAAOC,MAAAA,CAAOC,OAAO,CAACH,KAAOI,CAAAA,CAAAA,MAAM,CAAU,CAACC,GAAAA,EAAK,CAACC,GAAAA,EAAKC,KAAM,CAAA,GAAA;oBAC7D,MAAMC,SAAAA,GAAYP,UAAU,CAACK,GAAI,CAAA;AAEjC;;;YAIA,IAAId,0BAA0BiB,QAAQ,CAACH,QAAQC,KAAU,KAAA,IAAA,IAAQA,UAAUG,SAAW,EAAA;wBACpFL,GAAG,CAACC,IAAI,GAAGC,KAAAA;wBACX,OAAOF,GAAAA;AACT;oBAEA,IAAIG,SAAAA,CAAUG,IAAI,KAAK,WAAa,EAAA;wBAClC,IAAIH,SAAAA,CAAUI,UAAU,EAAE;AACxB,4BAAA,MAAMC,iBACJnB,SAAUc,CAAAA,SAAAA,EAAWD,KAASZ,CAAAA,GAAAA,SAAAA,CAAUY,OAAOC,SAAaD,CAAAA,GAAAA,KAAAA;AAE9DF,4BAAAA,GAAG,CAACC,GAAI,CAAA,GAAGO,cAAeC,CAAAA,GAAG,CAAC,CAACC,aAAAA,GAC7BhB,QAASgB,CAAAA,aAAAA,EAAelB,UAAU,CAACW,SAAAA,CAAUQ,SAAS,CAAC,EAAEf,cAAc,EAAC,CAAA,CAAA;yBAErE,MAAA;AACL,4BAAA,MAAMY,iBACJnB,SAAUc,CAAAA,SAAAA,EAAWD,KAASZ,CAAAA,GAAAA,SAA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xC,GAAKC,EAAAA,KAAAA,CAAM,IAAIL,MAAOC,CAAAA,OAAO,CAACL,IAAO,CAAA,CAAA;AAC/C,QAAA,IAAI,EAAEQ,GAAOqE,IAAAA,MAAK,KAAM,EAAErE,GAAQV,KAAAA,MAAQK,EAAAA,UAAAA,IAAc,EAAC,CAAC,CAAI,EAAA;YAC5D0E,MAAM,CAACrE,IAAI,GAAGC,KAAAA;AAChB;AACF;IAEA,OAAOoE,MAAAA;AACT,CAAA;AAEA;;;;;;;AAOC,IACKQ,MAAAA,yBAAAA,GAA4B,CAChCrF,IAAAA,EACA,EAAEF,MAAM,EAAE6E,aAAgB,GAAA,EAAE,EAAE5E,UAAa,GAAA,EAAE,EAAiB,GAAA;IAK9D,IAAI,CAACD,MAAQK,EAAAA,UAAAA,EAAY,OAAO;AAAEH,QAAAA,IAAAA;AAAMsF,QAAAA,iBAAAA,EAAmB;AAAG,KAAA;IAE9D,MAAMA,iBAAAA,GAAoBjC,0BAA2BrD,CAAAA,IAAAA,EAAMF,MAAQC,EAAAA,UAAAA,CAAAA;AAEnE,IAAA,MAAMwF,YAAeb,GAAAA,wBAAAA,CACnB1E,IACA2E,EAAAA,aAAAA,EACA7E,QACAC,UACAuF,EAAAA,iBAAAA,CAAAA;IAGF,OAAO;QACLtF,IAAMuF,EAAAA,YAAAA;AACND,QAAAA;AACF,KAAA;AACF;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { type FieldValue } from '@strapi/admin/strapi-admin';
|
|
3
3
|
import { MessageDescriptor } from 'react-intl';
|
|
4
|
-
import { Editor } from 'slate';
|
|
4
|
+
import { Editor, type Descendant } from 'slate';
|
|
5
5
|
import { type RenderElementProps } from 'slate-react';
|
|
6
6
|
import { type CSSProperties } from 'styled-components';
|
|
7
7
|
import { type BlocksContentProps } from './BlocksContent';
|
|
@@ -51,6 +51,11 @@ declare const BlocksEditorProvider: {
|
|
|
51
51
|
declare function useBlocksEditorContext(consumerName: string): BlocksEditorContextValue & {
|
|
52
52
|
editor: Editor;
|
|
53
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Normalize the blocks state to null if the editor state is considered empty,
|
|
56
|
+
* otherwise return the state
|
|
57
|
+
*/
|
|
58
|
+
declare const normalizeBlocksState: (editor: Editor, value: Schema.Attribute.BlocksValue | Descendant[]) => Schema.Attribute.BlocksValue | Descendant[] | null;
|
|
54
59
|
interface BlocksEditorProps extends Pick<FieldValue<Schema.Attribute.BlocksValue>, 'onChange' | 'value' | 'error'>, BlocksContentProps {
|
|
55
60
|
disabled?: boolean;
|
|
56
61
|
name: string;
|
|
@@ -58,4 +63,4 @@ interface BlocksEditorProps extends Pick<FieldValue<Schema.Attribute.BlocksValue
|
|
|
58
63
|
declare const BlocksEditor: React.ForwardRefExoticComponent<BlocksEditorProps & React.RefAttributes<{
|
|
59
64
|
focus: () => void;
|
|
60
65
|
}>>;
|
|
61
|
-
export { type BlocksStore, type SelectorBlockKey, BlocksEditor, BlocksEditorProvider, useBlocksEditorContext, isSelectorBlockKey, };
|
|
66
|
+
export { type BlocksStore, type SelectorBlockKey, BlocksEditor, BlocksEditorProvider, useBlocksEditorContext, isSelectorBlockKey, normalizeBlocksState, };
|
|
@@ -144,7 +144,7 @@ const createAttributeSchema = (attribute)=>{
|
|
|
144
144
|
case 'biginteger':
|
|
145
145
|
return yup__namespace.string().matches(/^-?\d*$/);
|
|
146
146
|
case 'boolean':
|
|
147
|
-
return yup__namespace.boolean();
|
|
147
|
+
return yup__namespace.boolean().nullable();
|
|
148
148
|
case 'blocks':
|
|
149
149
|
return yup__namespace.mixed().test('isBlocks', strapiAdmin.translatedErrors.json, (value)=>{
|
|
150
150
|
if (!value || Array.isArray(value)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sources":["../../../admin/src/utils/validation.ts"],"sourcesContent":["import { translatedErrors } from '@strapi/admin/strapi-admin';\nimport pipe from 'lodash/fp/pipe';\nimport * as yup from 'yup';\n\nimport { DOCUMENT_META_FIELDS } from '../constants/attributes';\n\nimport type { ComponentsDictionary, Schema } from '../hooks/useDocument';\nimport type { Schema as SchemaUtils } from '@strapi/types';\nimport type { ObjectShape } from 'yup/lib/object';\n\ntype AnySchema =\n | yup.StringSchema\n | yup.NumberSchema\n | yup.BooleanSchema\n | yup.DateSchema\n | yup.ArraySchema<any>\n | yup.ObjectSchema<any>;\n\n/* -------------------------------------------------------------------------------------------------\n * createYupSchema\n * -----------------------------------------------------------------------------------------------*/\n\ninterface ValidationOptions {\n status: 'draft' | 'published' | null;\n removedAttributes?: string[];\n}\n\nconst arrayValidator = (attribute: Schema['attributes'][string], options: ValidationOptions) => ({\n message: translatedErrors.required,\n test(value: unknown) {\n if (options.status === 'draft') {\n return true;\n }\n\n if (!attribute.required) {\n return true;\n }\n\n if (!value) {\n return false;\n }\n\n if (Array.isArray(value) && value.length === 0) {\n return false;\n }\n\n return true;\n },\n});\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n/**\n * TODO: should we create a Map to store these based on the hash of the schema?\n */\nconst createYupSchema = (\n attributes: Schema['attributes'] = {},\n components: ComponentsDictionary = {},\n options: ValidationOptions = { status: null }\n): yup.ObjectSchema<any> => {\n const createModelSchema = (\n attributes: Schema['attributes'],\n removedAttributes: string[] = []\n ): yup.ObjectSchema<any> =>\n yup\n .object()\n .shape(\n Object.entries(attributes).reduce<ObjectShape>((acc, [name, attribute]) => {\n const getNestedPathsForAttribute = (removed: string[], attrName: string): string[] => {\n const prefix = `${attrName}.`;\n const bracketRegex = new RegExp(`^${escapeRegex(attrName)}\\\\[\\\\d+\\\\]\\\\.`);\n\n return removed\n .filter((p) => p.startsWith(prefix) || bracketRegex.test(p))\n .map((p) =>\n p.startsWith(prefix) ? p.slice(prefix.length) : p.replace(bracketRegex, '')\n );\n };\n\n if (DOCUMENT_META_FIELDS.includes(name)) {\n return acc;\n }\n\n if (removedAttributes?.includes(name)) {\n // If the attribute is not visible, we don't want to validate it\n return acc;\n }\n\n const nestedRemoved = getNestedPathsForAttribute(removedAttributes, name);\n\n /**\n * These validations won't apply to every attribute\n * and that's okay, in that case we just return the\n * schema as it was passed.\n */\n const validations = [\n addNullableValidation,\n addRequiredValidation,\n addMinLengthValidation,\n addMaxLengthValidation,\n addMinValidation,\n addMaxValidation,\n addRegexValidation,\n ].map((fn) => fn(attribute, options));\n\n const transformSchema = pipe(...validations);\n\n switch (attribute.type) {\n case 'component': {\n const { attributes } = components[attribute.component];\n\n if (attribute.repeatable) {\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(createModelSchema(attributes, nestedRemoved).nullable(false))\n ).test(arrayValidator(attribute, options)),\n };\n } else {\n return {\n ...acc,\n [name]: transformSchema(createModelSchema(attributes, nestedRemoved).nullable()),\n };\n }\n }\n case 'dynamiczone':\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(\n yup.lazy(\n (\n data: SchemaUtils.Attribute.Value<SchemaUtils.Attribute.DynamicZone>[number]\n ) => {\n const attributes = components?.[data?.__component]?.attributes;\n\n const validation = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(components)),\n })\n .nullable(false);\n if (!attributes) {\n return validation;\n }\n\n return validation.concat(createModelSchema(attributes, nestedRemoved));\n }\n ) as unknown as yup.ObjectSchema<any>\n )\n ).test(arrayValidator(attribute, options)),\n };\n case 'relation':\n return {\n ...acc,\n [name]: transformSchema(\n yup.lazy((value) => {\n if (!value) {\n return yup.mixed().nullable(true);\n } else if (Array.isArray(value)) {\n // If a relation value is an array, we expect\n // an array of objects with {id} properties, representing the related entities.\n return yup.array().of(\n yup.object().shape({\n id: yup.number().required(),\n })\n );\n } else if (typeof value === 'object') {\n // A realtion value can also be an object. Some API\n // repsonses return the number of entities in the relation\n // as { count: x }\n return yup.object();\n } else {\n return yup\n .mixed()\n .test(\n 'type-error',\n 'Relation values must be either null, an array of objects with {id} or an object.',\n () => false\n );\n }\n })\n ),\n };\n default:\n return {\n ...acc,\n [name]: transformSchema(createAttributeSchema(attribute)),\n };\n }\n }, {})\n )\n /**\n * TODO: investigate why an undefined object fails a check of `nullable`.\n */\n .default(null);\n\n return createModelSchema(attributes, options.removedAttributes);\n};\n\nconst createAttributeSchema = (\n attribute: Exclude<\n SchemaUtils.Attribute.AnyAttribute,\n { type: 'dynamiczone' } | { type: 'component' } | { type: 'relation' }\n >\n) => {\n switch (attribute.type) {\n case 'biginteger':\n return yup.string().matches(/^-?\\d*$/);\n case 'boolean':\n return yup.boolean();\n case 'blocks':\n return yup.mixed().test('isBlocks', translatedErrors.json, (value) => {\n if (!value || Array.isArray(value)) {\n return true;\n } else {\n return false;\n }\n });\n case 'decimal':\n case 'float':\n case 'integer':\n return yup.number();\n case 'email':\n return yup.string().email(translatedErrors.email);\n case 'enumeration':\n return yup.string().oneOf([...attribute.enum, null]);\n case 'json':\n return yup.mixed().test('isJSON', translatedErrors.json, (value) => {\n /**\n * We don't want to validate the JSON field if it's empty.\n */\n if (!value || (typeof value === 'string' && value.length === 0)) {\n return true;\n }\n\n // If the value was created via content API and wasn't changed, then it's still an object\n if (typeof value === 'object') {\n try {\n JSON.stringify(value);\n return true;\n } catch (err) {\n return false;\n }\n }\n\n try {\n JSON.parse(value);\n\n return true;\n } catch (err) {\n return false;\n }\n });\n case 'password':\n return yup.string().nullable();\n case 'richtext':\n case 'string':\n case 'text':\n return yup.string();\n case 'uid':\n return yup\n .string()\n .matches(attribute.regex ? new RegExp(attribute.regex) : /^[A-Za-z0-9-_.~]*$/);\n default:\n /**\n * This allows any value.\n */\n return yup.mixed();\n }\n};\n\n// Helper function to return schema.nullable() if it exists, otherwise return schema\nconst nullableSchema = <TSchema extends AnySchema>(schema: TSchema) => {\n return schema?.nullable\n ? schema.nullable()\n : // In some cases '.nullable' will not be available on the schema.\n // e.g. when the schema has been built using yup.lazy (e.g. for relations).\n // In these cases we should just return the schema as it is.\n schema;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Validators\n * -----------------------------------------------------------------------------------------------*/\n/**\n * Our validator functions can be preped with the\n * attribute and then have the schema piped through them.\n */\ntype ValidationFn = (\n attribute: Schema['attributes'][string],\n options: ValidationOptions\n) => <TSchema extends AnySchema>(schema: TSchema) => TSchema;\n\nconst addNullableValidation: ValidationFn = () => (schema) => {\n return nullableSchema(schema);\n};\n\nconst addRequiredValidation: ValidationFn = (attribute, options) => (schema) => {\n if (options.status === 'draft' || !attribute.required || attribute.type === 'password') {\n return schema;\n }\n\n if (attribute.required && 'required' in schema) {\n return schema.required(translatedErrors.required);\n }\n\n return schema;\n};\n\nconst addMinLengthValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // Skip minLength validation for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if (\n 'minLength' in attribute &&\n attribute.minLength &&\n Number.isInteger(attribute.minLength) &&\n 'min' in schema\n ) {\n return schema.min(attribute.minLength, {\n ...translatedErrors.minLength,\n values: {\n min: attribute.minLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMaxLengthValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if (\n 'maxLength' in attribute &&\n attribute.maxLength &&\n Number.isInteger(attribute.maxLength) &&\n 'max' in schema\n ) {\n return schema.max(attribute.maxLength, {\n ...translatedErrors.maxLength,\n values: {\n max: attribute.maxLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMinValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // do not validate min for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if ('min' in attribute && 'min' in schema) {\n const min = toInteger(attribute.min);\n\n if (min) {\n return schema.min(min, {\n ...translatedErrors.min,\n values: {\n min,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst addMaxValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('max' in attribute) {\n const max = toInteger(attribute.max);\n\n if ('max' in schema && max) {\n return schema.max(max, {\n ...translatedErrors.max,\n values: {\n max,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst toInteger = (val?: string | number): number | undefined => {\n if (typeof val === 'number' || val === undefined) {\n return val;\n } else {\n const num = Number(val);\n return isNaN(num) ? undefined : num;\n }\n};\n\nconst addRegexValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('regex' in attribute && attribute.regex && 'matches' in schema) {\n return schema.matches(new RegExp(attribute.regex), {\n message: {\n id: translatedErrors.regex.id,\n defaultMessage: 'The value does not match the defined pattern.',\n },\n\n excludeEmptyString: !attribute.required,\n }) as TSchema;\n }\n\n return schema;\n };\n\nexport { createYupSchema };\n"],"names":["arrayValidator","attribute","options","message","translatedErrors","required","test","value","status","Array","isArray","length","escapeRegex","str","replace","createYupSchema","attributes","components","createModelSchema","removedAttributes","yup","object","shape","Object","entries","reduce","acc","name","getNestedPathsForAttribute","removed","attrName","prefix","bracketRegex","RegExp","filter","p","startsWith","map","slice","DOCUMENT_META_FIELDS","includes","nestedRemoved","validations","addNullableValidation","addRequiredValidation","addMinLengthValidation","addMaxLengthValidation","addMinValidation","addMaxValidation","addRegexValidation","fn","transformSchema","pipe","type","component","repeatable","array","of","nullable","lazy","data","__component","validation","string","oneOf","keys","concat","mixed","id","number","createAttributeSchema","default","matches","boolean","json","email","enum","JSON","stringify","err","parse","regex","nullableSchema","schema","minLength","Number","isInteger","min","values","maxLength","max","toInteger","val","undefined","num","isNaN","defaultMessage","excludeEmptyString"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,MAAMA,cAAiB,GAAA,CAACC,SAAyCC,EAAAA,OAAAA,IAAgC;AAC/FC,QAAAA,OAAAA,EAASC,6BAAiBC,QAAQ;AAClCC,QAAAA,IAAAA,CAAAA,CAAKC,KAAc,EAAA;YACjB,IAAIL,OAAAA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BAAA;gBAEAC,kBAAoB,EAAA,CAAChG,UAAUI;AACjC,aAAA,CAAA;AACF;QAEA,OAAO8E,MAAAA;AACT,KAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"validation.js","sources":["../../../admin/src/utils/validation.ts"],"sourcesContent":["import { translatedErrors } from '@strapi/admin/strapi-admin';\nimport pipe from 'lodash/fp/pipe';\nimport * as yup from 'yup';\n\nimport { DOCUMENT_META_FIELDS } from '../constants/attributes';\n\nimport type { ComponentsDictionary, Schema } from '../hooks/useDocument';\nimport type { Schema as SchemaUtils } from '@strapi/types';\nimport type { ObjectShape } from 'yup/lib/object';\n\ntype AnySchema =\n | yup.StringSchema\n | yup.NumberSchema\n | yup.BooleanSchema\n | yup.DateSchema\n | yup.ArraySchema<any>\n | yup.ObjectSchema<any>;\n\n/* -------------------------------------------------------------------------------------------------\n * createYupSchema\n * -----------------------------------------------------------------------------------------------*/\n\ninterface ValidationOptions {\n status: 'draft' | 'published' | null;\n removedAttributes?: string[];\n}\n\nconst arrayValidator = (attribute: Schema['attributes'][string], options: ValidationOptions) => ({\n message: translatedErrors.required,\n test(value: unknown) {\n if (options.status === 'draft') {\n return true;\n }\n\n if (!attribute.required) {\n return true;\n }\n\n if (!value) {\n return false;\n }\n\n if (Array.isArray(value) && value.length === 0) {\n return false;\n }\n\n return true;\n },\n});\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n/**\n * TODO: should we create a Map to store these based on the hash of the schema?\n */\nconst createYupSchema = (\n attributes: Schema['attributes'] = {},\n components: ComponentsDictionary = {},\n options: ValidationOptions = { status: null }\n): yup.ObjectSchema<any> => {\n const createModelSchema = (\n attributes: Schema['attributes'],\n removedAttributes: string[] = []\n ): yup.ObjectSchema<any> =>\n yup\n .object()\n .shape(\n Object.entries(attributes).reduce<ObjectShape>((acc, [name, attribute]) => {\n const getNestedPathsForAttribute = (removed: string[], attrName: string): string[] => {\n const prefix = `${attrName}.`;\n const bracketRegex = new RegExp(`^${escapeRegex(attrName)}\\\\[\\\\d+\\\\]\\\\.`);\n\n return removed\n .filter((p) => p.startsWith(prefix) || bracketRegex.test(p))\n .map((p) =>\n p.startsWith(prefix) ? p.slice(prefix.length) : p.replace(bracketRegex, '')\n );\n };\n\n if (DOCUMENT_META_FIELDS.includes(name)) {\n return acc;\n }\n\n if (removedAttributes?.includes(name)) {\n // If the attribute is not visible, we don't want to validate it\n return acc;\n }\n\n const nestedRemoved = getNestedPathsForAttribute(removedAttributes, name);\n\n /**\n * These validations won't apply to every attribute\n * and that's okay, in that case we just return the\n * schema as it was passed.\n */\n const validations = [\n addNullableValidation,\n addRequiredValidation,\n addMinLengthValidation,\n addMaxLengthValidation,\n addMinValidation,\n addMaxValidation,\n addRegexValidation,\n ].map((fn) => fn(attribute, options));\n\n const transformSchema = pipe(...validations);\n\n switch (attribute.type) {\n case 'component': {\n const { attributes } = components[attribute.component];\n\n if (attribute.repeatable) {\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(createModelSchema(attributes, nestedRemoved).nullable(false))\n ).test(arrayValidator(attribute, options)),\n };\n } else {\n return {\n ...acc,\n [name]: transformSchema(createModelSchema(attributes, nestedRemoved).nullable()),\n };\n }\n }\n case 'dynamiczone':\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(\n yup.lazy(\n (\n data: SchemaUtils.Attribute.Value<SchemaUtils.Attribute.DynamicZone>[number]\n ) => {\n const attributes = components?.[data?.__component]?.attributes;\n\n const validation = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(components)),\n })\n .nullable(false);\n if (!attributes) {\n return validation;\n }\n\n return validation.concat(createModelSchema(attributes, nestedRemoved));\n }\n ) as unknown as yup.ObjectSchema<any>\n )\n ).test(arrayValidator(attribute, options)),\n };\n case 'relation':\n return {\n ...acc,\n [name]: transformSchema(\n yup.lazy((value) => {\n if (!value) {\n return yup.mixed().nullable(true);\n } else if (Array.isArray(value)) {\n // If a relation value is an array, we expect\n // an array of objects with {id} properties, representing the related entities.\n return yup.array().of(\n yup.object().shape({\n id: yup.number().required(),\n })\n );\n } else if (typeof value === 'object') {\n // A realtion value can also be an object. Some API\n // repsonses return the number of entities in the relation\n // as { count: x }\n return yup.object();\n } else {\n return yup\n .mixed()\n .test(\n 'type-error',\n 'Relation values must be either null, an array of objects with {id} or an object.',\n () => false\n );\n }\n })\n ),\n };\n default:\n return {\n ...acc,\n [name]: transformSchema(createAttributeSchema(attribute)),\n };\n }\n }, {})\n )\n /**\n * TODO: investigate why an undefined object fails a check of `nullable`.\n */\n .default(null);\n\n return createModelSchema(attributes, options.removedAttributes);\n};\n\nconst createAttributeSchema = (\n attribute: Exclude<\n SchemaUtils.Attribute.AnyAttribute,\n { type: 'dynamiczone' } | { type: 'component' } | { type: 'relation' }\n >\n) => {\n switch (attribute.type) {\n case 'biginteger':\n return yup.string().matches(/^-?\\d*$/);\n case 'boolean':\n return yup.boolean().nullable();\n case 'blocks':\n return yup.mixed().test('isBlocks', translatedErrors.json, (value) => {\n if (!value || Array.isArray(value)) {\n return true;\n } else {\n return false;\n }\n });\n case 'decimal':\n case 'float':\n case 'integer':\n return yup.number();\n case 'email':\n return yup.string().email(translatedErrors.email);\n case 'enumeration':\n return yup.string().oneOf([...attribute.enum, null]);\n case 'json':\n return yup.mixed().test('isJSON', translatedErrors.json, (value) => {\n /**\n * We don't want to validate the JSON field if it's empty.\n */\n if (!value || (typeof value === 'string' && value.length === 0)) {\n return true;\n }\n\n // If the value was created via content API and wasn't changed, then it's still an object\n if (typeof value === 'object') {\n try {\n JSON.stringify(value);\n return true;\n } catch (err) {\n return false;\n }\n }\n\n try {\n JSON.parse(value);\n\n return true;\n } catch (err) {\n return false;\n }\n });\n case 'password':\n return yup.string().nullable();\n case 'richtext':\n case 'string':\n case 'text':\n return yup.string();\n case 'uid':\n return yup\n .string()\n .matches(attribute.regex ? new RegExp(attribute.regex) : /^[A-Za-z0-9-_.~]*$/);\n default:\n /**\n * This allows any value.\n */\n return yup.mixed();\n }\n};\n\n// Helper function to return schema.nullable() if it exists, otherwise return schema\nconst nullableSchema = <TSchema extends AnySchema>(schema: TSchema) => {\n return schema?.nullable\n ? schema.nullable()\n : // In some cases '.nullable' will not be available on the schema.\n // e.g. when the schema has been built using yup.lazy (e.g. for relations).\n // In these cases we should just return the schema as it is.\n schema;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Validators\n * -----------------------------------------------------------------------------------------------*/\n/**\n * Our validator functions can be preped with the\n * attribute and then have the schema piped through them.\n */\ntype ValidationFn = (\n attribute: Schema['attributes'][string],\n options: ValidationOptions\n) => <TSchema extends AnySchema>(schema: TSchema) => TSchema;\n\nconst addNullableValidation: ValidationFn = () => (schema) => {\n return nullableSchema(schema);\n};\n\nconst addRequiredValidation: ValidationFn = (attribute, options) => (schema) => {\n if (options.status === 'draft' || !attribute.required || attribute.type === 'password') {\n return schema;\n }\n\n if (attribute.required && 'required' in schema) {\n return schema.required(translatedErrors.required);\n }\n\n return schema;\n};\n\nconst addMinLengthValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // Skip minLength validation for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if (\n 'minLength' in attribute &&\n attribute.minLength &&\n Number.isInteger(attribute.minLength) &&\n 'min' in schema\n ) {\n return schema.min(attribute.minLength, {\n ...translatedErrors.minLength,\n values: {\n min: attribute.minLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMaxLengthValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if (\n 'maxLength' in attribute &&\n attribute.maxLength &&\n Number.isInteger(attribute.maxLength) &&\n 'max' in schema\n ) {\n return schema.max(attribute.maxLength, {\n ...translatedErrors.maxLength,\n values: {\n max: attribute.maxLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMinValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // do not validate min for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if ('min' in attribute && 'min' in schema) {\n const min = toInteger(attribute.min);\n\n if (min) {\n return schema.min(min, {\n ...translatedErrors.min,\n values: {\n min,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst addMaxValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('max' in attribute) {\n const max = toInteger(attribute.max);\n\n if ('max' in schema && max) {\n return schema.max(max, {\n ...translatedErrors.max,\n values: {\n max,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst toInteger = (val?: string | number): number | undefined => {\n if (typeof val === 'number' || val === undefined) {\n return val;\n } else {\n const num = Number(val);\n return isNaN(num) ? undefined : num;\n }\n};\n\nconst addRegexValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('regex' in attribute && attribute.regex && 'matches' in schema) {\n return schema.matches(new RegExp(attribute.regex), {\n message: {\n id: translatedErrors.regex.id,\n defaultMessage: 'The value does not match the defined pattern.',\n },\n\n excludeEmptyString: !attribute.required,\n }) as TSchema;\n }\n\n return schema;\n };\n\nexport { createYupSchema };\n"],"names":["arrayValidator","attribute","options","message","translatedErrors","required","test","value","status","Array","isArray","length","escapeRegex","str","replace","createYupSchema","attributes","components","createModelSchema","removedAttributes","yup","object","shape","Object","entries","reduce","acc","name","getNestedPathsForAttribute","removed","attrName","prefix","bracketRegex","RegExp","filter","p","startsWith","map","slice","DOCUMENT_META_FIELDS","includes","nestedRemoved","validations","addNullableValidation","addRequiredValidation","addMinLengthValidation","addMaxLengthValidation","addMinValidation","addMaxValidation","addRegexValidation","fn","transformSchema","pipe","type","component","repeatable","array","of","nullable","lazy","data","__component","validation","string","oneOf","keys","concat","mixed","id","number","createAttributeSchema","default","matches","boolean","json","email","enum","JSON","stringify","err","parse","regex","nullableSchema","schema","minLength","Number","isInteger","min","values","maxLength","max","toInteger","val","undefined","num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IAAAA,SAAAA,CAAUgF,KAAK,IAAI,aAAaE,MAAQ,EAAA;AAClE,YAAA,OAAOA,OAAOX,OAAO,CAAC,IAAIvC,MAAOhC,CAAAA,SAAAA,CAAUgF,KAAK,CAAG,EAAA;gBACjD9E,OAAS,EAAA;oBACPiE,EAAIhE,EAAAA,4BAAAA,CAAiB6E,KAAK,CAACb,EAAE;oBAC7B4B,cAAgB,EAAA;AAClB,iBAAA;gBAEAC,kBAAoB,EAAA,CAAChG,UAAUI;AACjC,aAAA,CAAA;AACF;QAEA,OAAO8E,MAAAA;AACT,KAAA;;;;"}
|
|
@@ -123,7 +123,7 @@ const createAttributeSchema = (attribute)=>{
|
|
|
123
123
|
case 'biginteger':
|
|
124
124
|
return yup.string().matches(/^-?\d*$/);
|
|
125
125
|
case 'boolean':
|
|
126
|
-
return yup.boolean();
|
|
126
|
+
return yup.boolean().nullable();
|
|
127
127
|
case 'blocks':
|
|
128
128
|
return yup.mixed().test('isBlocks', translatedErrors.json, (value)=>{
|
|
129
129
|
if (!value || Array.isArray(value)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.mjs","sources":["../../../admin/src/utils/validation.ts"],"sourcesContent":["import { translatedErrors } from '@strapi/admin/strapi-admin';\nimport pipe from 'lodash/fp/pipe';\nimport * as yup from 'yup';\n\nimport { DOCUMENT_META_FIELDS } from '../constants/attributes';\n\nimport type { ComponentsDictionary, Schema } from '../hooks/useDocument';\nimport type { Schema as SchemaUtils } from '@strapi/types';\nimport type { ObjectShape } from 'yup/lib/object';\n\ntype AnySchema =\n | yup.StringSchema\n | yup.NumberSchema\n | yup.BooleanSchema\n | yup.DateSchema\n | yup.ArraySchema<any>\n | yup.ObjectSchema<any>;\n\n/* -------------------------------------------------------------------------------------------------\n * createYupSchema\n * -----------------------------------------------------------------------------------------------*/\n\ninterface ValidationOptions {\n status: 'draft' | 'published' | null;\n removedAttributes?: string[];\n}\n\nconst arrayValidator = (attribute: Schema['attributes'][string], options: ValidationOptions) => ({\n message: translatedErrors.required,\n test(value: unknown) {\n if (options.status === 'draft') {\n return true;\n }\n\n if (!attribute.required) {\n return true;\n }\n\n if (!value) {\n return false;\n }\n\n if (Array.isArray(value) && value.length === 0) {\n return false;\n }\n\n return true;\n },\n});\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n/**\n * TODO: should we create a Map to store these based on the hash of the schema?\n */\nconst createYupSchema = (\n attributes: Schema['attributes'] = {},\n components: ComponentsDictionary = {},\n options: ValidationOptions = { status: null }\n): yup.ObjectSchema<any> => {\n const createModelSchema = (\n attributes: Schema['attributes'],\n removedAttributes: string[] = []\n ): yup.ObjectSchema<any> =>\n yup\n .object()\n .shape(\n Object.entries(attributes).reduce<ObjectShape>((acc, [name, attribute]) => {\n const getNestedPathsForAttribute = (removed: string[], attrName: string): string[] => {\n const prefix = `${attrName}.`;\n const bracketRegex = new RegExp(`^${escapeRegex(attrName)}\\\\[\\\\d+\\\\]\\\\.`);\n\n return removed\n .filter((p) => p.startsWith(prefix) || bracketRegex.test(p))\n .map((p) =>\n p.startsWith(prefix) ? p.slice(prefix.length) : p.replace(bracketRegex, '')\n );\n };\n\n if (DOCUMENT_META_FIELDS.includes(name)) {\n return acc;\n }\n\n if (removedAttributes?.includes(name)) {\n // If the attribute is not visible, we don't want to validate it\n return acc;\n }\n\n const nestedRemoved = getNestedPathsForAttribute(removedAttributes, name);\n\n /**\n * These validations won't apply to every attribute\n * and that's okay, in that case we just return the\n * schema as it was passed.\n */\n const validations = [\n addNullableValidation,\n addRequiredValidation,\n addMinLengthValidation,\n addMaxLengthValidation,\n addMinValidation,\n addMaxValidation,\n addRegexValidation,\n ].map((fn) => fn(attribute, options));\n\n const transformSchema = pipe(...validations);\n\n switch (attribute.type) {\n case 'component': {\n const { attributes } = components[attribute.component];\n\n if (attribute.repeatable) {\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(createModelSchema(attributes, nestedRemoved).nullable(false))\n ).test(arrayValidator(attribute, options)),\n };\n } else {\n return {\n ...acc,\n [name]: transformSchema(createModelSchema(attributes, nestedRemoved).nullable()),\n };\n }\n }\n case 'dynamiczone':\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(\n yup.lazy(\n (\n data: SchemaUtils.Attribute.Value<SchemaUtils.Attribute.DynamicZone>[number]\n ) => {\n const attributes = components?.[data?.__component]?.attributes;\n\n const validation = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(components)),\n })\n .nullable(false);\n if (!attributes) {\n return validation;\n }\n\n return validation.concat(createModelSchema(attributes, nestedRemoved));\n }\n ) as unknown as yup.ObjectSchema<any>\n )\n ).test(arrayValidator(attribute, options)),\n };\n case 'relation':\n return {\n ...acc,\n [name]: transformSchema(\n yup.lazy((value) => {\n if (!value) {\n return yup.mixed().nullable(true);\n } else if (Array.isArray(value)) {\n // If a relation value is an array, we expect\n // an array of objects with {id} properties, representing the related entities.\n return yup.array().of(\n yup.object().shape({\n id: yup.number().required(),\n })\n );\n } else if (typeof value === 'object') {\n // A realtion value can also be an object. Some API\n // repsonses return the number of entities in the relation\n // as { count: x }\n return yup.object();\n } else {\n return yup\n .mixed()\n .test(\n 'type-error',\n 'Relation values must be either null, an array of objects with {id} or an object.',\n () => false\n );\n }\n })\n ),\n };\n default:\n return {\n ...acc,\n [name]: transformSchema(createAttributeSchema(attribute)),\n };\n }\n }, {})\n )\n /**\n * TODO: investigate why an undefined object fails a check of `nullable`.\n */\n .default(null);\n\n return createModelSchema(attributes, options.removedAttributes);\n};\n\nconst createAttributeSchema = (\n attribute: Exclude<\n SchemaUtils.Attribute.AnyAttribute,\n { type: 'dynamiczone' } | { type: 'component' } | { type: 'relation' }\n >\n) => {\n switch (attribute.type) {\n case 'biginteger':\n return yup.string().matches(/^-?\\d*$/);\n case 'boolean':\n return yup.boolean();\n case 'blocks':\n return yup.mixed().test('isBlocks', translatedErrors.json, (value) => {\n if (!value || Array.isArray(value)) {\n return true;\n } else {\n return false;\n }\n });\n case 'decimal':\n case 'float':\n case 'integer':\n return yup.number();\n case 'email':\n return yup.string().email(translatedErrors.email);\n case 'enumeration':\n return yup.string().oneOf([...attribute.enum, null]);\n case 'json':\n return yup.mixed().test('isJSON', translatedErrors.json, (value) => {\n /**\n * We don't want to validate the JSON field if it's empty.\n */\n if (!value || (typeof value === 'string' && value.length === 0)) {\n return true;\n }\n\n // If the value was created via content API and wasn't changed, then it's still an object\n if (typeof value === 'object') {\n try {\n JSON.stringify(value);\n return true;\n } catch (err) {\n return false;\n }\n }\n\n try {\n JSON.parse(value);\n\n return true;\n } catch (err) {\n return false;\n }\n });\n case 'password':\n return yup.string().nullable();\n case 'richtext':\n case 'string':\n case 'text':\n return yup.string();\n case 'uid':\n return yup\n .string()\n .matches(attribute.regex ? new RegExp(attribute.regex) : /^[A-Za-z0-9-_.~]*$/);\n default:\n /**\n * This allows any value.\n */\n return yup.mixed();\n }\n};\n\n// Helper function to return schema.nullable() if it exists, otherwise return schema\nconst nullableSchema = <TSchema extends AnySchema>(schema: TSchema) => {\n return schema?.nullable\n ? schema.nullable()\n : // In some cases '.nullable' will not be available on the schema.\n // e.g. when the schema has been built using yup.lazy (e.g. for relations).\n // In these cases we should just return the schema as it is.\n schema;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Validators\n * -----------------------------------------------------------------------------------------------*/\n/**\n * Our validator functions can be preped with the\n * attribute and then have the schema piped through them.\n */\ntype ValidationFn = (\n attribute: Schema['attributes'][string],\n options: ValidationOptions\n) => <TSchema extends AnySchema>(schema: TSchema) => TSchema;\n\nconst addNullableValidation: ValidationFn = () => (schema) => {\n return nullableSchema(schema);\n};\n\nconst addRequiredValidation: ValidationFn = (attribute, options) => (schema) => {\n if (options.status === 'draft' || !attribute.required || attribute.type === 'password') {\n return schema;\n }\n\n if (attribute.required && 'required' in schema) {\n return schema.required(translatedErrors.required);\n }\n\n return schema;\n};\n\nconst addMinLengthValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // Skip minLength validation for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if (\n 'minLength' in attribute &&\n attribute.minLength &&\n Number.isInteger(attribute.minLength) &&\n 'min' in schema\n ) {\n return schema.min(attribute.minLength, {\n ...translatedErrors.minLength,\n values: {\n min: attribute.minLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMaxLengthValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if (\n 'maxLength' in attribute &&\n attribute.maxLength &&\n Number.isInteger(attribute.maxLength) &&\n 'max' in schema\n ) {\n return schema.max(attribute.maxLength, {\n ...translatedErrors.maxLength,\n values: {\n max: attribute.maxLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMinValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // do not validate min for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if ('min' in attribute && 'min' in schema) {\n const min = toInteger(attribute.min);\n\n if (min) {\n return schema.min(min, {\n ...translatedErrors.min,\n values: {\n min,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst addMaxValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('max' in attribute) {\n const max = toInteger(attribute.max);\n\n if ('max' in schema && max) {\n return schema.max(max, {\n ...translatedErrors.max,\n values: {\n max,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst toInteger = (val?: string | number): number | undefined => {\n if (typeof val === 'number' || val === undefined) {\n return val;\n } else {\n const num = Number(val);\n return isNaN(num) ? undefined : num;\n }\n};\n\nconst addRegexValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('regex' in attribute && attribute.regex && 'matches' in schema) {\n return schema.matches(new RegExp(attribute.regex), {\n message: {\n id: translatedErrors.regex.id,\n defaultMessage: 'The value does not match the defined pattern.',\n },\n\n excludeEmptyString: !attribute.required,\n }) as TSchema;\n }\n\n return schema;\n };\n\nexport { createYupSchema };\n"],"names":["arrayValidator","attribute","options","message","translatedErrors","required","test","value","status","Array","isArray","length","escapeRegex","str","replace","createYupSchema","attributes","components","createModelSchema","removedAttributes","yup","object","shape","Object","entries","reduce","acc","name","getNestedPathsForAttribute","removed","attrName","prefix","bracketRegex","RegExp","filter","p","startsWith","map","slice","DOCUMENT_META_FIELDS","includes","nestedRemoved","validations","addNullableValidation","addRequiredValidation","addMinLengthValidation","addMaxLengthValidation","addMinValidation","addMaxValidation","addRegexValidation","fn","transformSchema","pipe","type","component","repeatable","array","of","nullable","lazy","data","__component","validation","string","oneOf","keys","concat","mixed","id","number","createAttributeSchema","default","matches","boolean","json","email","enum","JSON","stringify","err","parse","regex","nullableSchema","schema","minLength","Number","isInteger","min","values","maxLength","max","toInteger","val","undefined","num","isNaN","defaultMessage","excludeEmptyString"],"mappings":";;;;;AA2BA,MAAMA,cAAiB,GAAA,CAACC,SAAyCC,EAAAA,OAAAA,IAAgC;AAC/FC,QAAAA,OAAAA,EAASC,iBAAiBC,QAAQ;AAClCC,QAAAA,IAAAA,CAAAA,CAAKC,KAAc,EAAA;YACjB,IAAIL,OAAAA,CAAQM,MAAM,KAAK,OAAS,EAAA;gBAC9B,OAAO,IAAA;AACT;YAEA,IAAI,CAACP,SAAUI,CAAAA,QAAQ,EAAE;gBACvB,OAAO,IAAA;AACT;AAEA,YAAA,IAAI,CAACE,KAAO,EAAA;gBACV,OAAO,KAAA;AACT;AAEA,YAAA,IAAIE,MAAMC,OAAO,CAACH,UAAUA,KAAMI,CAAAA,MAAM,KAAK,CAAG,EAAA;gBAC9C,OAAO,KAAA;AACT;YAEA,OAAO,IAAA;AACT;KACF,CAAA;AACA,MAAMC,cAAc,CAACC,GAAAA,GAAgBA,GAAIC,CAAAA,OAAO,CAAC,qBAAuB,EAAA,MAAA,CAAA;AACxE;;IAGA,MAAMC,eAA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B,oBAAA,GAAGtF,iBAAiBsF,GAAG;oBACvBF,MAAQ,EAAA;AACNE,wBAAAA;AACF;AACF,iBAAA,CAAA;AACF;AACF;QAEA,OAAOP,MAAAA;AACT,KAAA;AAEF,MAAMQ,YAAY,CAACC,GAAAA,GAAAA;AACjB,IAAA,IAAI,OAAOA,GAAAA,KAAQ,QAAYA,IAAAA,GAAAA,KAAQC,SAAW,EAAA;QAChD,OAAOD,GAAAA;KACF,MAAA;AACL,QAAA,MAAME,MAAMT,MAAOO,CAAAA,GAAAA,CAAAA;QACnB,OAAOG,KAAAA,CAAMD,OAAOD,SAAYC,GAAAA,GAAAA;AAClC;AACF,CAAA;AAEA,MAAM7C,kBAAAA,GACJ,CAAChD,SAAAA,GACD,CAA4BkF,MAAAA,GAAAA;AAC1B,QAAA,IAAI,WAAWlF,SAAaA,IAAAA,SAAAA,CAAUgF,KAAK,IAAI,aAAaE,MAAQ,EAAA;AAClE,YAAA,OAAOA,OAAOX,OAAO,CAAC,IAAIvC,MAAOhC,CAAAA,SAAAA,CAAUgF,KAAK,CAAG,EAAA;gBACjD9E,OAAS,EAAA;oBACPiE,EAAIhE,EAAAA,gBAAAA,CAAiB6E,KAAK,CAACb,EAAE;oBAC7B4B,cAAgB,EAAA;AAClB,iBAAA;gBAEAC,kBAAoB,EAAA,CAAChG,UAAUI;AACjC,aAAA,CAAA;AACF;QAEA,OAAO8E,MAAAA;AACT,KAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"validation.mjs","sources":["../../../admin/src/utils/validation.ts"],"sourcesContent":["import { translatedErrors } from '@strapi/admin/strapi-admin';\nimport pipe from 'lodash/fp/pipe';\nimport * as yup from 'yup';\n\nimport { DOCUMENT_META_FIELDS } from '../constants/attributes';\n\nimport type { ComponentsDictionary, Schema } from '../hooks/useDocument';\nimport type { Schema as SchemaUtils } from '@strapi/types';\nimport type { ObjectShape } from 'yup/lib/object';\n\ntype AnySchema =\n | yup.StringSchema\n | yup.NumberSchema\n | yup.BooleanSchema\n | yup.DateSchema\n | yup.ArraySchema<any>\n | yup.ObjectSchema<any>;\n\n/* -------------------------------------------------------------------------------------------------\n * createYupSchema\n * -----------------------------------------------------------------------------------------------*/\n\ninterface ValidationOptions {\n status: 'draft' | 'published' | null;\n removedAttributes?: string[];\n}\n\nconst arrayValidator = (attribute: Schema['attributes'][string], options: ValidationOptions) => ({\n message: translatedErrors.required,\n test(value: unknown) {\n if (options.status === 'draft') {\n return true;\n }\n\n if (!attribute.required) {\n return true;\n }\n\n if (!value) {\n return false;\n }\n\n if (Array.isArray(value) && value.length === 0) {\n return false;\n }\n\n return true;\n },\n});\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n/**\n * TODO: should we create a Map to store these based on the hash of the schema?\n */\nconst createYupSchema = (\n attributes: Schema['attributes'] = {},\n components: ComponentsDictionary = {},\n options: ValidationOptions = { status: null }\n): yup.ObjectSchema<any> => {\n const createModelSchema = (\n attributes: Schema['attributes'],\n removedAttributes: string[] = []\n ): yup.ObjectSchema<any> =>\n yup\n .object()\n .shape(\n Object.entries(attributes).reduce<ObjectShape>((acc, [name, attribute]) => {\n const getNestedPathsForAttribute = (removed: string[], attrName: string): string[] => {\n const prefix = `${attrName}.`;\n const bracketRegex = new RegExp(`^${escapeRegex(attrName)}\\\\[\\\\d+\\\\]\\\\.`);\n\n return removed\n .filter((p) => p.startsWith(prefix) || bracketRegex.test(p))\n .map((p) =>\n p.startsWith(prefix) ? p.slice(prefix.length) : p.replace(bracketRegex, '')\n );\n };\n\n if (DOCUMENT_META_FIELDS.includes(name)) {\n return acc;\n }\n\n if (removedAttributes?.includes(name)) {\n // If the attribute is not visible, we don't want to validate it\n return acc;\n }\n\n const nestedRemoved = getNestedPathsForAttribute(removedAttributes, name);\n\n /**\n * These validations won't apply to every attribute\n * and that's okay, in that case we just return the\n * schema as it was passed.\n */\n const validations = [\n addNullableValidation,\n addRequiredValidation,\n addMinLengthValidation,\n addMaxLengthValidation,\n addMinValidation,\n addMaxValidation,\n addRegexValidation,\n ].map((fn) => fn(attribute, options));\n\n const transformSchema = pipe(...validations);\n\n switch (attribute.type) {\n case 'component': {\n const { attributes } = components[attribute.component];\n\n if (attribute.repeatable) {\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(createModelSchema(attributes, nestedRemoved).nullable(false))\n ).test(arrayValidator(attribute, options)),\n };\n } else {\n return {\n ...acc,\n [name]: transformSchema(createModelSchema(attributes, nestedRemoved).nullable()),\n };\n }\n }\n case 'dynamiczone':\n return {\n ...acc,\n [name]: transformSchema(\n yup.array().of(\n yup.lazy(\n (\n data: SchemaUtils.Attribute.Value<SchemaUtils.Attribute.DynamicZone>[number]\n ) => {\n const attributes = components?.[data?.__component]?.attributes;\n\n const validation = yup\n .object()\n .shape({\n __component: yup.string().required().oneOf(Object.keys(components)),\n })\n .nullable(false);\n if (!attributes) {\n return validation;\n }\n\n return validation.concat(createModelSchema(attributes, nestedRemoved));\n }\n ) as unknown as yup.ObjectSchema<any>\n )\n ).test(arrayValidator(attribute, options)),\n };\n case 'relation':\n return {\n ...acc,\n [name]: transformSchema(\n yup.lazy((value) => {\n if (!value) {\n return yup.mixed().nullable(true);\n } else if (Array.isArray(value)) {\n // If a relation value is an array, we expect\n // an array of objects with {id} properties, representing the related entities.\n return yup.array().of(\n yup.object().shape({\n id: yup.number().required(),\n })\n );\n } else if (typeof value === 'object') {\n // A realtion value can also be an object. Some API\n // repsonses return the number of entities in the relation\n // as { count: x }\n return yup.object();\n } else {\n return yup\n .mixed()\n .test(\n 'type-error',\n 'Relation values must be either null, an array of objects with {id} or an object.',\n () => false\n );\n }\n })\n ),\n };\n default:\n return {\n ...acc,\n [name]: transformSchema(createAttributeSchema(attribute)),\n };\n }\n }, {})\n )\n /**\n * TODO: investigate why an undefined object fails a check of `nullable`.\n */\n .default(null);\n\n return createModelSchema(attributes, options.removedAttributes);\n};\n\nconst createAttributeSchema = (\n attribute: Exclude<\n SchemaUtils.Attribute.AnyAttribute,\n { type: 'dynamiczone' } | { type: 'component' } | { type: 'relation' }\n >\n) => {\n switch (attribute.type) {\n case 'biginteger':\n return yup.string().matches(/^-?\\d*$/);\n case 'boolean':\n return yup.boolean().nullable();\n case 'blocks':\n return yup.mixed().test('isBlocks', translatedErrors.json, (value) => {\n if (!value || Array.isArray(value)) {\n return true;\n } else {\n return false;\n }\n });\n case 'decimal':\n case 'float':\n case 'integer':\n return yup.number();\n case 'email':\n return yup.string().email(translatedErrors.email);\n case 'enumeration':\n return yup.string().oneOf([...attribute.enum, null]);\n case 'json':\n return yup.mixed().test('isJSON', translatedErrors.json, (value) => {\n /**\n * We don't want to validate the JSON field if it's empty.\n */\n if (!value || (typeof value === 'string' && value.length === 0)) {\n return true;\n }\n\n // If the value was created via content API and wasn't changed, then it's still an object\n if (typeof value === 'object') {\n try {\n JSON.stringify(value);\n return true;\n } catch (err) {\n return false;\n }\n }\n\n try {\n JSON.parse(value);\n\n return true;\n } catch (err) {\n return false;\n }\n });\n case 'password':\n return yup.string().nullable();\n case 'richtext':\n case 'string':\n case 'text':\n return yup.string();\n case 'uid':\n return yup\n .string()\n .matches(attribute.regex ? new RegExp(attribute.regex) : /^[A-Za-z0-9-_.~]*$/);\n default:\n /**\n * This allows any value.\n */\n return yup.mixed();\n }\n};\n\n// Helper function to return schema.nullable() if it exists, otherwise return schema\nconst nullableSchema = <TSchema extends AnySchema>(schema: TSchema) => {\n return schema?.nullable\n ? schema.nullable()\n : // In some cases '.nullable' will not be available on the schema.\n // e.g. when the schema has been built using yup.lazy (e.g. for relations).\n // In these cases we should just return the schema as it is.\n schema;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Validators\n * -----------------------------------------------------------------------------------------------*/\n/**\n * Our validator functions can be preped with the\n * attribute and then have the schema piped through them.\n */\ntype ValidationFn = (\n attribute: Schema['attributes'][string],\n options: ValidationOptions\n) => <TSchema extends AnySchema>(schema: TSchema) => TSchema;\n\nconst addNullableValidation: ValidationFn = () => (schema) => {\n return nullableSchema(schema);\n};\n\nconst addRequiredValidation: ValidationFn = (attribute, options) => (schema) => {\n if (options.status === 'draft' || !attribute.required || attribute.type === 'password') {\n return schema;\n }\n\n if (attribute.required && 'required' in schema) {\n return schema.required(translatedErrors.required);\n }\n\n return schema;\n};\n\nconst addMinLengthValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // Skip minLength validation for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if (\n 'minLength' in attribute &&\n attribute.minLength &&\n Number.isInteger(attribute.minLength) &&\n 'min' in schema\n ) {\n return schema.min(attribute.minLength, {\n ...translatedErrors.minLength,\n values: {\n min: attribute.minLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMaxLengthValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if (\n 'maxLength' in attribute &&\n attribute.maxLength &&\n Number.isInteger(attribute.maxLength) &&\n 'max' in schema\n ) {\n return schema.max(attribute.maxLength, {\n ...translatedErrors.maxLength,\n values: {\n max: attribute.maxLength,\n },\n }) as TSchema;\n }\n\n return schema;\n };\n\nconst addMinValidation: ValidationFn =\n (attribute, options) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n // do not validate min for draft\n if (options.status === 'draft') {\n return schema;\n }\n\n if ('min' in attribute && 'min' in schema) {\n const min = toInteger(attribute.min);\n\n if (min) {\n return schema.min(min, {\n ...translatedErrors.min,\n values: {\n min,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst addMaxValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('max' in attribute) {\n const max = toInteger(attribute.max);\n\n if ('max' in schema && max) {\n return schema.max(max, {\n ...translatedErrors.max,\n values: {\n max,\n },\n }) as TSchema;\n }\n }\n\n return schema;\n };\n\nconst toInteger = (val?: string | number): number | undefined => {\n if (typeof val === 'number' || val === undefined) {\n return val;\n } else {\n const num = Number(val);\n return isNaN(num) ? undefined : num;\n }\n};\n\nconst addRegexValidation: ValidationFn =\n (attribute) =>\n <TSchema extends AnySchema>(schema: TSchema): TSchema => {\n if ('regex' in attribute && attribute.regex && 'matches' in schema) {\n return schema.matches(new RegExp(attribute.regex), {\n message: {\n id: translatedErrors.regex.id,\n defaultMessage: 'The value does not match the defined pattern.',\n },\n\n excludeEmptyString: !attribute.required,\n }) as TSchema;\n }\n\n return schema;\n };\n\nexport { createYupSchema };\n"],"names":["arrayValidator","attribute","options","message","translatedErrors","required","test","value","status","Array","isArray","length","escapeRegex","str","replace","createYupSchema","attributes","components","createModelSchema","removedAttributes","yup","object","shape","Object","entries","reduce","acc","name","getNestedPathsForAttribute","removed","attrName","prefix","bracketRegex","RegExp","filter","p","startsWith","map","slice","DOCUMENT_META_FIELDS","includes","nestedRemoved","validations","addNullableValidation","addRequiredValidation","addMinLengthValidation","addMaxLengthValidation","addMinValidation","addMaxValidation","addRegexValidation","fn","transformSchema","pipe","type","component","repeatable","array","of","nullable","lazy","data","__component","validation","string","oneOf","keys","concat","mixed","id","number","createAttributeSchema","default","matches","boolean","json","email","enum","JSON","stringify","err","parse","regex","nullableSchema","schema","minLength","Number","isInteger","min","values","maxLength","max","toInteger","val","undefined","num","isNaN","defaultMessage","excludeEmptyString"],"mappings":";;;;;AA2BA,MAAMA,cAAiB,GAAA,CAACC,SAAyCC,EAAAA,OAAAA,IAAgC;AAC/FC,QAAAA,OAAAA,EAASC,iBAAiBC,QAAQ;AAClCC,QAAAA,IAAAA,CAAAA,CAAKC,KAAc,EAAA;YACjB,IAAIL,OAAAA,CAAQM,MAAM,KAAK,OAAS,EAAA;gBAC9B,OAAO,IAAA;AACT;YAEA,IAAI,CAACP,SAAUI,CAAAA,QAAQ,EAAE;gBACvB,OAAO,IAAA;AACT;AAEA,YAAA,IAAI,CAACE,KAAO,EAAA;gBACV,OAAO,KAAA;AACT;AAEA,YAAA,IAAIE,MAAMC,OAAO,CAACH,UA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utils/populate.js');
|
|
6
6
|
|
|
7
|
-
const { getScalarAttributes } = strapiUtils.contentTypes;
|
|
7
|
+
const { getScalarAttributes, getMediaAttributes } = strapiUtils.contentTypes;
|
|
8
8
|
const AVAILABLE_STATUS_FIELDS = [
|
|
9
9
|
'id',
|
|
10
10
|
'documentId',
|
|
@@ -151,8 +151,9 @@ var documentMetadata = (({ strapi })=>({
|
|
|
151
151
|
// TODO: Ignore publishedAt if availableStatus=false, and ignore locale if
|
|
152
152
|
// i18n is disabled
|
|
153
153
|
const { populate: populate$1 = {}, fields = [] } = populate.getPopulateForValidation(uid);
|
|
154
|
-
// Include non-translatable scalar fields in availableLocales for i18n prefilling
|
|
154
|
+
// Include non-translatable scalar and media fields in availableLocales for i18n prefilling
|
|
155
155
|
let nonLocalizedFields = [];
|
|
156
|
+
let nonLocalizedMediaFields = [];
|
|
156
157
|
try {
|
|
157
158
|
const i18nPlugin = strapi.plugin('i18n');
|
|
158
159
|
if (i18nPlugin) {
|
|
@@ -161,19 +162,31 @@ var documentMetadata = (({ strapi })=>({
|
|
|
161
162
|
const model = strapi.getModel(uid);
|
|
162
163
|
if (model?.attributes) {
|
|
163
164
|
const allNonLocalized = i18nService.getNonLocalizedAttributes(model);
|
|
164
|
-
// Get
|
|
165
|
+
// Get scalar and media attributes separately
|
|
165
166
|
const scalarAttrs = getScalarAttributes(model);
|
|
166
|
-
|
|
167
|
+
const mediaAttrs = getMediaAttributes(model);
|
|
168
|
+
// Separate scalar fields (can be in fields array) from media fields (need to be populated)
|
|
167
169
|
nonLocalizedFields = allNonLocalized.filter((field)=>field in model.attributes && scalarAttrs.includes(field));
|
|
170
|
+
nonLocalizedMediaFields = allNonLocalized.filter((field)=>field in model.attributes && mediaAttrs.includes(field));
|
|
168
171
|
}
|
|
169
172
|
}
|
|
170
173
|
}
|
|
171
174
|
} catch (error) {
|
|
172
175
|
// i18n plugin might not be enabled or might error, ignore silently
|
|
173
176
|
}
|
|
177
|
+
// Build populate object for non-localized media fields
|
|
178
|
+
const mediaPopulate = nonLocalizedMediaFields.reduce((acc, field)=>{
|
|
179
|
+
acc[field] = {
|
|
180
|
+
populate: {
|
|
181
|
+
folder: true
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
return acc;
|
|
185
|
+
}, {});
|
|
174
186
|
const params = {
|
|
175
187
|
populate: {
|
|
176
188
|
...populate$1,
|
|
189
|
+
...mediaPopulate,
|
|
177
190
|
// NOTE: creator fields are selected in this way to avoid exposing sensitive data
|
|
178
191
|
createdBy: {
|
|
179
192
|
select: [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document-metadata.js","sources":["../../../server/src/services/document-metadata.ts"],"sourcesContent":["import { groupBy, pick, uniq } from 'lodash/fp';\n\nimport { async, contentTypes } from '@strapi/utils';\nimport type { Core, UID, Modules } from '@strapi/types';\n\nimport type { DocumentMetadata } from '../../../shared/contracts/collection-types';\nimport { getPopulateForValidation } from './utils/populate';\n\nconst { getScalarAttributes } = contentTypes;\n\nexport interface DocumentVersion {\n id: string | number;\n documentId: Modules.Documents.ID;\n locale?: string;\n localizations?: DocumentVersion[];\n updatedAt?: string | null | Date;\n publishedAt?: string | null | Date;\n}\n\nconst AVAILABLE_STATUS_FIELDS = [\n 'id',\n 'documentId',\n 'locale',\n 'updatedAt',\n 'createdAt',\n 'publishedAt',\n 'createdBy',\n 'updatedBy',\n 'status',\n];\nconst AVAILABLE_LOCALES_FIELDS = [\n 'id',\n 'documentId',\n 'locale',\n 'updatedAt',\n 'createdAt',\n 'publishedAt',\n 'documentId',\n];\n\nconst CONTENT_MANAGER_STATUS = {\n PUBLISHED: 'published',\n DRAFT: 'draft',\n MODIFIED: 'modified',\n};\n\n/**\n * Controls the metadata properties to be returned\n *\n * If `availableLocales` is set to `true` (default), the returned metadata will include\n * the available locales of the document for its current status.\n *\n * If `availableStatus` is set to `true` (default), the returned metadata will include\n * the available status of the document for its current locale.\n */\nexport interface GetMetadataOptions {\n availableLocales?: boolean;\n availableStatus?: boolean;\n}\n\n/**\n * Checks if the provided document version has been modified after all other versions.\n */\nconst getIsVersionLatestModification = (\n version?: DocumentVersion,\n otherVersion?: DocumentVersion\n): boolean => {\n if (!version || !version.updatedAt) {\n return false;\n }\n\n const versionUpdatedAt = version?.updatedAt ? new Date(version.updatedAt).getTime() : 0;\n\n const otherUpdatedAt = otherVersion?.updatedAt ? new Date(otherVersion.updatedAt).getTime() : 0;\n\n return versionUpdatedAt > otherUpdatedAt;\n};\n\nexport default ({ strapi }: { strapi: Core.Strapi }) => ({\n /**\n * Returns available locales of a document for the current status\n */\n async getAvailableLocales(\n uid: UID.ContentType,\n version: DocumentVersion,\n allVersions: DocumentVersion[]\n ) {\n // Group all versions by locale\n const versionsByLocale = groupBy('locale', allVersions);\n\n // Delete the current locale\n if (version.locale) {\n delete versionsByLocale[version.locale];\n }\n\n // For each locale, get the ones with the same status\n // There will not be a draft and a version counterpart if the content\n // type does not have draft and publish\n const model = strapi.getModel(uid);\n\n const mappingResult = await async.map(\n Object.values(versionsByLocale),\n async (localeVersions: DocumentVersion[]) => {\n if (!contentTypes.hasDraftAndPublish(model)) {\n return localeVersions[0];\n }\n\n const draftVersion = localeVersions.find((v) => v.publishedAt === null);\n const otherVersions = localeVersions.filter((v) => v.id !== draftVersion?.id);\n\n if (!draftVersion) {\n return;\n }\n\n return {\n ...draftVersion,\n status: this.getStatus(draftVersion, otherVersions as any),\n };\n }\n );\n\n return (\n mappingResult\n // Filter just in case there is a document with no drafts\n .filter(Boolean) as unknown as DocumentMetadata['availableLocales']\n );\n },\n\n /**\n * Returns available status of a document for the current locale\n */\n getAvailableStatus(version: DocumentVersion, allVersions: DocumentVersion[]) {\n // Find the other status of the document\n const status =\n version.publishedAt !== null\n ? CONTENT_MANAGER_STATUS.DRAFT\n : CONTENT_MANAGER_STATUS.PUBLISHED;\n\n // Get version that match the current locale and not match the current status\n const availableStatus = allVersions.find((v) => {\n const matchLocale = v.locale === version.locale;\n const matchStatus = status === 'published' ? v.publishedAt !== null : v.publishedAt === null;\n return matchLocale && matchStatus;\n });\n\n if (!availableStatus) return availableStatus;\n\n // Pick status fields (at fields, status, by fields), use lodash fp\n return pick(AVAILABLE_STATUS_FIELDS, availableStatus);\n },\n\n /**\n * Get the available status of many documents, useful for batch operations\n * @param uid\n * @param documents\n * @returns\n */\n async getManyAvailableStatus(uid: UID.ContentType, documents: DocumentVersion[]) {\n if (!documents.length) return [];\n\n // The status and locale of all documents should be the same\n const status = documents[0].publishedAt !== null ? 'published' : 'draft';\n const locales = documents.map((d) => d.locale).filter(Boolean);\n\n const where: Record<string, any> = {\n documentId: { $in: documents.map((d) => d.documentId).filter(Boolean) },\n publishedAt: { $null: status === 'published' },\n };\n\n // If there is any locale to filter (if i18n is enabled)\n if (locales.length) {\n where.locale = { $in: locales };\n }\n\n return strapi.query(uid).findMany({\n where,\n select: ['id', 'documentId', 'locale', 'updatedAt', 'createdAt', 'publishedAt'],\n });\n },\n\n getStatus(version: DocumentVersion, otherDocumentStatuses?: DocumentMetadata['availableStatus']) {\n let draftVersion: DocumentVersion | undefined;\n let publishedVersion: DocumentVersion | undefined;\n\n if (version.publishedAt) {\n publishedVersion = version;\n } else {\n draftVersion = version;\n }\n\n const otherVersion = otherDocumentStatuses?.at(0);\n if (otherVersion?.publishedAt) {\n publishedVersion = otherVersion;\n } else if (otherVersion) {\n draftVersion = otherVersion;\n }\n\n if (!draftVersion) return CONTENT_MANAGER_STATUS.PUBLISHED;\n if (!publishedVersion) return CONTENT_MANAGER_STATUS.DRAFT;\n\n /*\n * The document is modified if the draft version has been updated more\n * recently than the published version.\n */\n const isDraftModified = getIsVersionLatestModification(draftVersion, publishedVersion);\n return isDraftModified ? CONTENT_MANAGER_STATUS.MODIFIED : CONTENT_MANAGER_STATUS.PUBLISHED;\n },\n\n // TODO is it necessary to return metadata on every page of the CM\n // We could refactor this so the locales are only loaded when they're\n // needed. e.g. in the bulk locale action modal.\n async getMetadata(\n uid: UID.ContentType,\n version: DocumentVersion,\n { availableLocales = true, availableStatus = true }: GetMetadataOptions = {}\n ) {\n // TODO: Ignore publishedAt if availableStatus=false, and ignore locale if\n // i18n is disabled\n const { populate = {}, fields = [] } = getPopulateForValidation(uid);\n\n // Include non-translatable scalar fields in availableLocales for i18n prefilling\n let nonLocalizedFields: string[] = [];\n try {\n const i18nPlugin = strapi.plugin('i18n');\n if (i18nPlugin) {\n const i18nService = i18nPlugin.service('content-types');\n if (i18nService?.getNonLocalizedAttributes) {\n const model = strapi.getModel(uid);\n if (model?.attributes) {\n const allNonLocalized = i18nService.getNonLocalizedAttributes(model);\n // Get only scalar attributes (components, relations, etc. can't be in fields array)\n const scalarAttrs = getScalarAttributes(model);\n // Only include scalar, non-localized fields that actually exist in the model\n nonLocalizedFields = allNonLocalized.filter(\n (field: string) => field in model.attributes && scalarAttrs.includes(field)\n );\n }\n }\n }\n } catch (error) {\n // i18n plugin might not be enabled or might error, ignore silently\n }\n\n const params = {\n populate: {\n ...populate,\n // NOTE: creator fields are selected in this way to avoid exposing sensitive data\n createdBy: {\n select: ['id', 'firstname', 'lastname', 'email'],\n },\n updatedBy: {\n select: ['id', 'firstname', 'lastname', 'email'],\n },\n },\n fields: uniq([...AVAILABLE_LOCALES_FIELDS, ...fields, ...nonLocalizedFields]),\n filters: {\n documentId: version.documentId,\n },\n };\n\n const dbParams = strapi.get('query-params').transform(uid, params);\n const versions = await strapi.db.query(uid).findMany(dbParams);\n\n // TODO: Remove use of available locales and use localizations instead\n const availableLocalesResult = availableLocales\n ? await this.getAvailableLocales(uid, version, versions)\n : [];\n\n const availableStatusResult = availableStatus\n ? this.getAvailableStatus(version, versions)\n : null;\n\n return {\n availableLocales: availableLocalesResult,\n availableStatus: availableStatusResult ? [availableStatusResult] : [],\n };\n },\n\n /**\n * Returns associated metadata of a document:\n * - Available locales of the document for the current status\n * - Available status of the document for the current locale\n */\n async formatDocumentWithMetadata(\n uid: UID.ContentType,\n document: DocumentVersion,\n opts: GetMetadataOptions = {}\n ) {\n if (!document) {\n return {\n data: document,\n meta: {\n availableLocales: [],\n availableStatus: [],\n },\n };\n }\n\n const hasDraftAndPublish = contentTypes.hasDraftAndPublish(strapi.getModel(uid));\n\n // Ignore available status if the content type does not have draft and publish\n if (!hasDraftAndPublish) {\n opts.availableStatus = false;\n }\n\n const meta = await this.getMetadata(uid, document, opts);\n\n // Populate localization statuses\n if (document.localizations) {\n const otherStatus = await this.getManyAvailableStatus(uid, document.localizations);\n\n document.localizations = document.localizations.map((d) => {\n const status = otherStatus.find(\n (s) => s.documentId === d.documentId && s.locale === d.locale\n );\n return {\n ...d,\n status: this.getStatus(d, status ? [status] : []),\n };\n });\n }\n\n return {\n data: {\n ...document,\n // Add status to the document only if draft and publish is enabled\n status: hasDraftAndPublish\n ? this.getStatus(document, meta.availableStatus as any)\n : undefined,\n },\n meta,\n };\n },\n});\n"],"names":["getScalarAttributes","contentTypes","AVAILABLE_STATUS_FIELDS","AVAILABLE_LOCALES_FIELDS","CONTENT_MANAGER_STATUS","PUBLISHED","DRAFT","MODIFIED","getIsVersionLatestModification","version","otherVersion","updatedAt","versionUpdatedAt","Date","getTime","otherUpdatedAt","strapi","getAvailableLocales","uid","allVersions","versionsByLocale","groupBy","locale","model","getModel","mappingResult","async","map","Object","values","localeVersions","hasDraftAndPublish","draftVersion","find","v","publishedAt","otherVersions","filter","id","status","getStatus","Boolean","getAvailableStatus","availableStatus","matchLocale","matchStatus","pick","getManyAvailableStatus","documents","length","locales","d","where","documentId","$in","$null","query","findMany","select","otherDocumentStatuses","publishedVersion","at","isDraftModified","getMetadata","availableLocales","populate","fields","getPopulateForValidation","nonLocalizedFields","i18nPlugin","plugin","i18nService","service","getNonLocalizedAttributes","attributes","allNonLocalized","scalarAttrs","field","includes","error","params","createdBy","updatedBy","uniq","filters","dbParams","get","transform","versions","db","availableLocalesResult","availableStatusResult","formatDocumentWithMetadata","document","opts","data","meta","localizations","otherStatus","s","undefined"],"mappings":";;;;;;AAQA,MAAM,EAAEA,mBAAmB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A;;YAGnD,IAAID,QAAAA,CAASI,aAAa,EAAE;gBAC1B,MAAMC,WAAAA,GAAc,MAAM,IAAI,CAACnD,sBAAsB,CAAC7B,GAAAA,EAAK2E,SAASI,aAAa,CAAA;AAEjFJ,gBAAAA,QAAAA,CAASI,aAAa,GAAGJ,QAAAA,CAASI,aAAa,CAACtE,GAAG,CAAC,CAACwB,CAAAA,GAAAA;AACnD,oBAAA,MAAMZ,SAAS2D,WAAYjE,CAAAA,IAAI,CAC7B,CAACkE,IAAMA,CAAE9C,CAAAA,UAAU,KAAKF,CAAAA,CAAEE,UAAU,IAAI8C,CAAAA,CAAE7E,MAAM,KAAK6B,EAAE7B,MAAM,CAAA;oBAE/D,OAAO;AACL,wBAAA,GAAG6B,CAAC;AACJZ,wBAAAA,MAAAA,EAAQ,IAAI,CAACC,SAAS,CAACW,GAAGZ,MAAS,GAAA;AAACA,4BAAAA;AAAO,yBAAA,GAAG,EAAE;AAClD,qBAAA;AACF,iBAAA,CAAA;AACF;YAEA,OAAO;gBACLwD,IAAM,EAAA;AACJ,oBAAA,GAAGF,QAAQ;;oBAEXtD,MAAQR,EAAAA,kBAAAA,GACJ,IAAI,CAACS,SAAS,CAACqD,QAAUG,EAAAA,IAAAA,CAAKrD,eAAe,CAC7CyD,GAAAA;AACN,iBAAA;AACAJ,gBAAAA;AACF,aAAA;AACF;AACF,KAAA,CAAC;;;;"}
|
|
1
|
+
{"version":3,"file":"document-metadata.js","sources":["../../../server/src/services/document-metadata.ts"],"sourcesContent":["import { groupBy, pick, uniq } from 'lodash/fp';\n\nimport { async, contentTypes } from '@strapi/utils';\nimport type { Core, UID, Modules } from '@strapi/types';\n\nimport type { DocumentMetadata } from '../../../shared/contracts/collection-types';\nimport { getPopulateForValidation } from './utils/populate';\n\nconst { getScalarAttributes, getMediaAttributes } = contentTypes;\n\nexport interface DocumentVersion {\n id: string | number;\n documentId: Modules.Documents.ID;\n locale?: string;\n localizations?: DocumentVersion[];\n updatedAt?: string | null | Date;\n publishedAt?: string | null | Date;\n}\n\nconst AVAILABLE_STATUS_FIELDS = [\n 'id',\n 'documentId',\n 'locale',\n 'updatedAt',\n 'createdAt',\n 'publishedAt',\n 'createdBy',\n 'updatedBy',\n 'status',\n];\nconst AVAILABLE_LOCALES_FIELDS = [\n 'id',\n 'documentId',\n 'locale',\n 'updatedAt',\n 'createdAt',\n 'publishedAt',\n 'documentId',\n];\n\nconst CONTENT_MANAGER_STATUS = {\n PUBLISHED: 'published',\n DRAFT: 'draft',\n MODIFIED: 'modified',\n};\n\n/**\n * Controls the metadata properties to be returned\n *\n * If `availableLocales` is set to `true` (default), the returned metadata will include\n * the available locales of the document for its current status.\n *\n * If `availableStatus` is set to `true` (default), the returned metadata will include\n * the available status of the document for its current locale.\n */\nexport interface GetMetadataOptions {\n availableLocales?: boolean;\n availableStatus?: boolean;\n}\n\n/**\n * Checks if the provided document version has been modified after all other versions.\n */\nconst getIsVersionLatestModification = (\n version?: DocumentVersion,\n otherVersion?: DocumentVersion\n): boolean => {\n if (!version || !version.updatedAt) {\n return false;\n }\n\n const versionUpdatedAt = version?.updatedAt ? new Date(version.updatedAt).getTime() : 0;\n\n const otherUpdatedAt = otherVersion?.updatedAt ? new Date(otherVersion.updatedAt).getTime() : 0;\n\n return versionUpdatedAt > otherUpdatedAt;\n};\n\nexport default ({ strapi }: { strapi: Core.Strapi }) => ({\n /**\n * Returns available locales of a document for the current status\n */\n async getAvailableLocales(\n uid: UID.ContentType,\n version: DocumentVersion,\n allVersions: DocumentVersion[]\n ) {\n // Group all versions by locale\n const versionsByLocale = groupBy('locale', allVersions);\n\n // Delete the current locale\n if (version.locale) {\n delete versionsByLocale[version.locale];\n }\n\n // For each locale, get the ones with the same status\n // There will not be a draft and a version counterpart if the content\n // type does not have draft and publish\n const model = strapi.getModel(uid);\n\n const mappingResult = await async.map(\n Object.values(versionsByLocale),\n async (localeVersions: DocumentVersion[]) => {\n if (!contentTypes.hasDraftAndPublish(model)) {\n return localeVersions[0];\n }\n\n const draftVersion = localeVersions.find((v) => v.publishedAt === null);\n const otherVersions = localeVersions.filter((v) => v.id !== draftVersion?.id);\n\n if (!draftVersion) {\n return;\n }\n\n return {\n ...draftVersion,\n status: this.getStatus(draftVersion, otherVersions as any),\n };\n }\n );\n\n return (\n mappingResult\n // Filter just in case there is a document with no drafts\n .filter(Boolean) as unknown as DocumentMetadata['availableLocales']\n );\n },\n\n /**\n * Returns available status of a document for the current locale\n */\n getAvailableStatus(version: DocumentVersion, allVersions: DocumentVersion[]) {\n // Find the other status of the document\n const status =\n version.publishedAt !== null\n ? CONTENT_MANAGER_STATUS.DRAFT\n : CONTENT_MANAGER_STATUS.PUBLISHED;\n\n // Get version that match the current locale and not match the current status\n const availableStatus = allVersions.find((v) => {\n const matchLocale = v.locale === version.locale;\n const matchStatus = status === 'published' ? v.publishedAt !== null : v.publishedAt === null;\n return matchLocale && matchStatus;\n });\n\n if (!availableStatus) return availableStatus;\n\n // Pick status fields (at fields, status, by fields), use lodash fp\n return pick(AVAILABLE_STATUS_FIELDS, availableStatus);\n },\n\n /**\n * Get the available status of many documents, useful for batch operations\n * @param uid\n * @param documents\n * @returns\n */\n async getManyAvailableStatus(uid: UID.ContentType, documents: DocumentVersion[]) {\n if (!documents.length) return [];\n\n // The status and locale of all documents should be the same\n const status = documents[0].publishedAt !== null ? 'published' : 'draft';\n const locales = documents.map((d) => d.locale).filter(Boolean);\n\n const where: Record<string, any> = {\n documentId: { $in: documents.map((d) => d.documentId).filter(Boolean) },\n publishedAt: { $null: status === 'published' },\n };\n\n // If there is any locale to filter (if i18n is enabled)\n if (locales.length) {\n where.locale = { $in: locales };\n }\n\n return strapi.query(uid).findMany({\n where,\n select: ['id', 'documentId', 'locale', 'updatedAt', 'createdAt', 'publishedAt'],\n });\n },\n\n getStatus(version: DocumentVersion, otherDocumentStatuses?: DocumentMetadata['availableStatus']) {\n let draftVersion: DocumentVersion | undefined;\n let publishedVersion: DocumentVersion | undefined;\n\n if (version.publishedAt) {\n publishedVersion = version;\n } else {\n draftVersion = version;\n }\n\n const otherVersion = otherDocumentStatuses?.at(0);\n if (otherVersion?.publishedAt) {\n publishedVersion = otherVersion;\n } else if (otherVersion) {\n draftVersion = otherVersion;\n }\n\n if (!draftVersion) return CONTENT_MANAGER_STATUS.PUBLISHED;\n if (!publishedVersion) return CONTENT_MANAGER_STATUS.DRAFT;\n\n /*\n * The document is modified if the draft version has been updated more\n * recently than the published version.\n */\n const isDraftModified = getIsVersionLatestModification(draftVersion, publishedVersion);\n return isDraftModified ? CONTENT_MANAGER_STATUS.MODIFIED : CONTENT_MANAGER_STATUS.PUBLISHED;\n },\n\n // TODO is it necessary to return metadata on every page of the CM\n // We could refactor this so the locales are only loaded when they're\n // needed. e.g. in the bulk locale action modal.\n async getMetadata(\n uid: UID.ContentType,\n version: DocumentVersion,\n { availableLocales = true, availableStatus = true }: GetMetadataOptions = {}\n ) {\n // TODO: Ignore publishedAt if availableStatus=false, and ignore locale if\n // i18n is disabled\n const { populate = {}, fields = [] } = getPopulateForValidation(uid);\n\n // Include non-translatable scalar and media fields in availableLocales for i18n prefilling\n let nonLocalizedFields: string[] = [];\n let nonLocalizedMediaFields: string[] = [];\n try {\n const i18nPlugin = strapi.plugin('i18n');\n if (i18nPlugin) {\n const i18nService = i18nPlugin.service('content-types');\n if (i18nService?.getNonLocalizedAttributes) {\n const model = strapi.getModel(uid);\n if (model?.attributes) {\n const allNonLocalized = i18nService.getNonLocalizedAttributes(model);\n // Get scalar and media attributes separately\n const scalarAttrs = getScalarAttributes(model);\n const mediaAttrs = getMediaAttributes(model);\n\n // Separate scalar fields (can be in fields array) from media fields (need to be populated)\n nonLocalizedFields = allNonLocalized.filter(\n (field: string) => field in model.attributes && scalarAttrs.includes(field)\n );\n nonLocalizedMediaFields = allNonLocalized.filter(\n (field: string) => field in model.attributes && mediaAttrs.includes(field)\n );\n }\n }\n }\n } catch (error) {\n // i18n plugin might not be enabled or might error, ignore silently\n }\n\n // Build populate object for non-localized media fields\n const mediaPopulate = nonLocalizedMediaFields.reduce(\n (acc, field) => {\n acc[field] = {\n populate: {\n folder: true,\n },\n };\n return acc;\n },\n {} as Record<string, { populate: { folder: boolean } }>\n );\n\n const params = {\n populate: {\n ...populate,\n ...mediaPopulate,\n // NOTE: creator fields are selected in this way to avoid exposing sensitive data\n createdBy: {\n select: ['id', 'firstname', 'lastname', 'email'],\n },\n updatedBy: {\n select: ['id', 'firstname', 'lastname', 'email'],\n },\n },\n fields: uniq([...AVAILABLE_LOCALES_FIELDS, ...fields, ...nonLocalizedFields]),\n filters: {\n documentId: version.documentId,\n },\n };\n\n const dbParams = strapi.get('query-params').transform(uid, params);\n const versions = await strapi.db.query(uid).findMany(dbParams);\n\n // TODO: Remove use of available locales and use localizations instead\n const availableLocalesResult = availableLocales\n ? await this.getAvailableLocales(uid, version, versions)\n : [];\n\n const availableStatusResult = availableStatus\n ? this.getAvailableStatus(version, versions)\n : null;\n\n return {\n availableLocales: availableLocalesResult,\n availableStatus: availableStatusResult ? [availableStatusResult] : [],\n };\n },\n\n /**\n * Returns associated metadata of a document:\n * - Available locales of the document for the current status\n * - Available status of the document for the current locale\n */\n async formatDocumentWithMetadata(\n uid: UID.ContentType,\n document: DocumentVersion,\n opts: GetMetadataOptions = {}\n ) {\n if (!document) {\n return {\n data: document,\n meta: {\n availableLocales: [],\n availableStatus: [],\n },\n };\n }\n\n const hasDraftAndPublish = contentTypes.hasDraftAndPublish(strapi.getModel(uid));\n\n // Ignore available status if the content type does not have draft and publish\n if (!hasDraftAndPublish) {\n opts.availableStatus = false;\n }\n\n const meta = await this.getMetadata(uid, document, opts);\n\n // Populate localization statuses\n if (document.localizations) {\n const otherStatus = await this.getManyAvailableStatus(uid, document.localizations);\n\n document.localizations = document.localizations.map((d) => {\n const status = otherStatus.find(\n (s) => s.documentId === d.documentId && s.locale === d.locale\n );\n return {\n ...d,\n status: this.getStatus(d, status ? [status] : []),\n };\n });\n }\n\n return {\n data: {\n ...document,\n // Add status to the document only if draft and publish is enabled\n status: hasDraftAndPublish\n ? this.getStatus(document, meta.availableStatus as any)\n : undefined,\n },\n meta,\n };\n },\n});\n"],"names":["getScalarAttributes","getMediaAttributes","contentTypes","AVAILABLE_STATUS_FIELDS","AVAILABLE_LOCALES_FIELDS","CONTENT_MANAGER_STATUS","PUBLISHED","DRAFT","MODIFIED","getIsVersionLatestModification","version","otherVersion","updatedAt","versionUpdatedAt","Date","getTime","otherUpdatedAt","strapi","getAvailableLocales","uid","allVersions","versionsByLocale","groupBy","locale","model","getModel","mappingResult","async","map","Object","values","localeVersions","hasDraftAndPublish","draftVersion","find","v","publishedAt","otherVersions","filter","id","status","getStatus","Boolean","getAvailableStatus","availableStatus","matchLocale","matchStatus","pick","getManyAvailableStatus","documents","length","locales","d","where","documentId","$in","$null","query","findMany","select","otherDocumentStatuses","publishedVersion","at","isDraftModified","getMetadata","availableLocales","populate","fields","getPopulateForValidation","nonLocalizedFields","nonLocalizedMediaFields","i18nPlugin","plugin","i18nService","service","getNonLocalizedAttributes","attributes","allNonLocalized","scalarAttrs","mediaAttrs","field","includes","error","mediaPopulate","reduce","acc","folder","params","createdBy","updatedBy","uniq","filters","dbParams","get","transform","versions","db","availableLocalesResult","availableStatusResult","formatDocumentWithMetadata","document","opts","data","meta","localizations","otherStatus","s","undefined"],"mappings":";;;;;;AAQA,MAAM,EAAEA,mBAAmB,EAAEC,kBAAkB,EAAE,GAAGC,wBAAAA;AAWpD,MAAMC,uBAA0B,GAAA;AAC9B,IAAA,IAAA;AACA,IAAA,YAAA;AACA,IAAA,QAAA;AACA,IAAA,WAAA;AACA,IAAA,WAAA;AACA,IAAA,aAAA;AACA,IAAA,WAAA;AACA,IAAA,WAAA;AACA,IAAA;AACD,CAAA;AACD,MAAMC,wBAA2B,GAAA;AAC/B,IAAA,IAAA;AACA,IAAA,YAAA;AACA,IAAA,QAAA;AACA,IAAA,WAAA;AACA,IAAA,WAAA;AACA,IAAA,aAAA;AACA,IAAA;AACD,CAAA;AAED,MA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