@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 3.41 kB
JavaScript
import{copy as e}from"../../../../i18n/copy.mjs";import t from"../../../helpers/build-schema-index.mjs";import n from"../../../helpers/prefix-generated-column-name.mjs";import r from"../../utils/zod-safe-parse.mjs";import i from"../../custom-field.mjs";import a from"../../utils/key-to-title.mjs";import{validateRelationItemCount as o}from"../../utils/relation-item-count-validation.mjs";import{relationFieldConfig as s}from"./config.mjs";import c from"../../../helpers/build-table-name.mjs";import{normalizeRelationCollections as l}from"./utils/normalize-relation-collections.mjs";import{clampRelationFieldInput as u,normalizeStoredRelationCustomFieldValues as d}from"./utils/relation-field-values.mjs";import f from"zod";var p=class extends i{type=s.type;config;key;props;constructor(t,n){super(),this.key=t,this.props=n,this.config={key:this.key,type:this.type,collection:l(this.props.collection),details:{label:this.props?.details?.label??e(`admin:fields.${this.type}.${this.key}.label`,{defaultMessage:a(this.key)}),summary:this.props?.details?.summary},localized:this.props?.localized??!1,default:this.props?.default??[],index:this.props?.index,multiple:this.props?.multiple,ui:{hidden:this.props?.ui?.hidden,disabled:this.props?.ui?.disabled,condition:this.props?.ui?.condition,width:this.props?.ui?.width},validation:this.props?.validation}}normalizeInputValue(e){return u(e,this.config.multiple)}get defaultValue(){return d(this.config.default,this.config.multiple)}get errors(){return{...super.errors,required:{condition:e=>e==null||Array.isArray(e)&&e.length===0,message:e(`server:core.fields.validation.required`)}}}getSchemaDefinition(e){return{data:{columns:[{name:`collection_key`,type:e.db.getDataType(`text`),nullable:!1},{name:`document_id`,type:e.db.getDataType(`integer`),nullable:!1}]},error:void 0}}getIndexDefinitions(e){let r=[n(`collection_key`),n(`document_id`)];return[t({db:e.db,tableName:e.table.name,columns:r,source:`field`})]}formatResponseValue(e){return d(e,this.config.multiple)}serializeRelationFieldValue(e){return d(e,this.config.multiple).map(e=>({[n(`collection_key`)]:e.collectionKey,[n(`document_id`)]:e.id}))}extractRelationFieldValue(e){let t=e[n(`document_id`)],r=e[n(`collection_key`)];return typeof t!=`number`||typeof r!=`string`?null:{id:t,collectionKey:r}}getRelationFieldValidationInput(e){return d(e,this.config.multiple).reduce((e,t)=>(e[t.collectionKey]||(e[t.collectionKey]=[]),e[t.collectionKey]?.push(t.id),e),{})}getRelationFieldRefTargets(e){let t=this.extractRelationFieldValue(e);if(!t)return[];let n=c(`document`,{collection:t.collectionKey},null);return n.error?[]:[{table:n.data.name,value:t.id}]}uniqueValidation(t,n){let i=f.array(f.object({id:f.number(),collectionKey:f.string()})),a=u(t,this.config.multiple),s=r(a,i);if(!s.valid)return s;let c=d(a,this.config.multiple),p=o({multiple:this.config.multiple,length:c.length,validation:this.config.validation});if(!p.valid)return p;let m=l(this.config.collection),h=[];for(let[t,r]of c.entries()){if(!m.includes(r.collectionKey)){h.push({itemIndex:t,message:e(`server:core.fields.relation.validation.not.found`)});continue}n?.find(e=>e.id===r.id&&e.collection_key===r.collectionKey)===void 0&&h.push({itemIndex:t,message:e(`server:core.fields.relation.validation.not.found`)})}return h.length>0?{valid:!1,errors:h}:{valid:!0}}};export{p as default};
//# sourceMappingURL=custom-field.mjs.map