@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.19 kB
JavaScript
import{copy as e}from"../../../../i18n/copy.mjs";import t from"../../../helpers/prefix-generated-column-name.mjs";import n from"../../utils/zod-safe-parse.mjs";import r from"../../custom-field.mjs";import i from"../../utils/key-to-title.mjs";import{rangeFieldConfig as a}from"./config.mjs";import{normalizeRangeInputValue as o,normalizeStoredRangeValues as s}from"./values.mjs";import c from"zod";var l=class extends r{type=a.type;config;key;props;constructor(t,n){super(),this.key=t,this.props=n;let r=this.props?.min??0,a=this.props?.max??100,o=this.props?.step??1,c=this.props?.thumbs,l=this.props?.default??(c===2?[r,a]:[r]);this.config={key:this.key,type:this.type,details:{label:this.props?.details?.label??e(`admin:fields.${this.type}.${this.key}.label`,{defaultMessage:i(this.key)}),summary:this.props?.details?.summary},min:r,max:a,step:o,localized:this.props?.localized??!1,default:s(l,c),index:this.props?.index,thumbs:c,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 o(e,this.config.thumbs)}get defaultValue(){return s(this.config.default,this.config.thumbs)}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:`value`,type:e.db.getDataType(`real`),nullable:!1}]},error:void 0}}formatResponseValue(e){let t=s(e,this.config.thumbs);return t.length>0?t:s(this.config.default,this.config.thumbs)}get relationValueColumn(){return`value`}serializeRelationFieldValue(e){return s(e,this.config.thumbs).map(e=>({[t(`value`)]:e}))}uniqueValidation(e){let t=this.config.thumbs===2?2:1;return n(e,c.array(c.number().min(this.config.min).max(this.config.max)).refine(e=>e.length===0||e.length===t,{message:`Expected ${t} range value${t===1?``:`s`}`}).refine(e=>e.every(e=>{let t=(e-this.config.min)/this.config.step;return Math.abs(t-Math.round(t))<1e-9}),{message:`Range values must align to a step of ${this.config.step}`}))}};export{l as default};
//# sourceMappingURL=custom-field.mjs.map