{"version":3,"sources":["../../../../src/fields/hooks/beforeChange/index.ts"],"sourcesContent":["import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'\nimport type { ValidationFieldError } from '../../../errors/index.js'\nimport type { SanitizedGlobalConfig } from '../../../globals/config/types.js'\nimport type { RequestContext } from '../../../index.js'\nimport type { JsonObject, Operation, PayloadRequest } from '../../../types/index.js'\n\nimport { ValidationError } from '../../../errors/index.js'\nimport { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'\nimport { traverseFields } from './traverseFields.js'\n\nexport type Args = {\n collection: null | SanitizedCollectionConfig\n context: RequestContext\n data: T\n doc: T\n docWithLocales: JsonObject\n global: null | SanitizedGlobalConfig\n id?: number | string\n operation: Operation\n overrideAccess?: boolean\n req: PayloadRequest\n skipValidation?: boolean\n}\n\n/**\n * This function is responsible for the following actions, in order:\n * - Run condition\n * - Execute field hooks\n * - Validate data\n * - Transform data for storage\n * - Unflatten locales. The input `data` is the normal document for one locale. The output result will become the document with locales.\n */\n\nexport const beforeChange = async ({\n id,\n collection,\n context,\n data: incomingData,\n doc,\n docWithLocales,\n global,\n operation,\n overrideAccess,\n req,\n skipValidation,\n}: Args): Promise => {\n const data = deepCopyObjectSimple(incomingData)\n const mergeLocaleActions: (() => Promise | void)[] = []\n const errors: ValidationFieldError[] = []\n\n await traverseFields({\n id,\n collection,\n context,\n data,\n doc,\n docWithLocales,\n errors,\n fieldLabelPath: '',\n fields: (collection?.fields || global?.fields)!,\n global,\n mergeLocaleActions,\n operation,\n overrideAccess: overrideAccess!,\n parentIndexPath: '',\n parentIsLocalized: false,\n parentPath: '',\n parentSchemaPath: '',\n req,\n siblingData: data,\n siblingDoc: doc,\n siblingDocWithLocales: docWithLocales,\n skipValidation,\n })\n\n if (errors.length > 0) {\n throw new ValidationError(\n {\n id,\n collection: collection?.slug,\n errors,\n global: global?.slug,\n req,\n },\n req.t,\n )\n }\n\n for (const action of mergeLocaleActions) {\n await action()\n }\n\n return data\n}\n"],"names":["ValidationError","deepCopyObjectSimple","traverseFields","beforeChange","id","collection","context","data","incomingData","doc","docWithLocales","global","operation","overrideAccess","req","skipValidation","mergeLocaleActions","errors","fieldLabelPath","fields","parentIndexPath","parentIsLocalized","parentPath","parentSchemaPath","siblingData","siblingDoc","siblingDocWithLocales","length","slug","t","action"],"mappings":"AAMA,SAASA,eAAe,QAAQ,2BAA0B;AAC1D,SAASC,oBAAoB,QAAQ,uCAAsC;AAC3E,SAASC,cAAc,QAAQ,sBAAqB;AAgBpD;;;;;;;CAOC,GAED,OAAO,MAAMC,eAAe,OAA6B,EACvDC,EAAE,EACFC,UAAU,EACVC,OAAO,EACPC,MAAMC,YAAY,EAClBC,GAAG,EACHC,cAAc,EACdC,MAAM,EACNC,SAAS,EACTC,cAAc,EACdC,GAAG,EACHC,cAAc,EACN;IACR,MAAMR,OAAON,qBAAqBO;IAClC,MAAMQ,qBAAqD,EAAE;IAC7D,MAAMC,SAAiC,EAAE;IAEzC,MAAMf,eAAe;QACnBE;QACAC;QACAC;QACAC;QACAE;QACAC;QACAO;QACAC,gBAAgB;QAChBC,QAASd,YAAYc,UAAUR,QAAQQ;QACvCR;QACAK;QACAJ;QACAC,gBAAgBA;QAChBO,iBAAiB;QACjBC,mBAAmB;QACnBC,YAAY;QACZC,kBAAkB;QAClBT;QACAU,aAAajB;QACbkB,YAAYhB;QACZiB,uBAAuBhB;QACvBK;IACF;IAEA,IAAIE,OAAOU,MAAM,GAAG,GAAG;QACrB,MAAM,IAAI3B,gBACR;YACEI;YACAC,YAAYA,YAAYuB;YACxBX;YACAN,QAAQA,QAAQiB;YAChBd;QACF,GACAA,IAAIe,CAAC;IAET;IAEA,KAAK,MAAMC,UAAUd,mBAAoB;QACvC,MAAMc;IACR;IAEA,OAAOvB;AACT,EAAC"}