UNPKG

@lucidcms/core

Version:

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

1 lines 4.44 kB
{"version":3,"file":"custom-field.mjs","names":[],"sources":["../../../../../../src/libs/collection/custom-fields/fields/select/custom-field.ts"],"sourcesContent":["import z from \"zod\";\nimport type { ServiceResponse } from \"../../../../../types.js\";\nimport deepMerge from \"../../../../../utils/helpers/deep-merge.js\";\nimport { copy } from \"../../../../i18n/index.js\";\nimport CustomField from \"../../custom-field.js\";\nimport type {\n\tCFConfig,\n\tCFProps,\n\tCFResponse,\n\tGetSchemaDefinitionProps,\n\tSchemaDefinition,\n} from \"../../types.js\";\nimport keyToTitle from \"../../utils/key-to-title.js\";\nimport zodSafeParse from \"../../utils/zod-safe-parse.js\";\nimport { selectFieldConfig } from \"./config.js\";\n\nclass SelectCustomField extends CustomField<\"select\"> {\n\ttype = selectFieldConfig.type;\n\tconfig;\n\tkey;\n\tprops;\n\tconstructor(key: string, props?: CFProps<\"select\">) {\n\t\tsuper();\n\t\tthis.key = key;\n\t\tthis.props = props;\n\t\tthis.config = {\n\t\t\tkey: this.key,\n\t\t\ttype: this.type,\n\t\t\tdetails: {\n\t\t\t\tlabel:\n\t\t\t\t\tthis.props?.details?.label ??\n\t\t\t\t\tcopy(`admin:fields.${this.type}.${this.key}.label`, {\n\t\t\t\t\t\tdefaultMessage: keyToTitle(this.key),\n\t\t\t\t\t}),\n\t\t\t\tsummary: this.props?.details?.summary,\n\t\t\t\tplaceholder: this.props?.details?.placeholder,\n\t\t\t},\n\t\t\toptions: this.props?.options ?? [],\n\t\t\tlocalized: this.props?.localized ?? false,\n\t\t\tdefault: this.props?.default ?? \"\",\n\t\t\tindex: this.props?.index,\n\t\t\tui: {\n\t\t\t\thidden: this.props?.ui?.hidden,\n\t\t\t\tdisabled: this.props?.ui?.disabled,\n\t\t\t\tcondition: this.props?.ui?.condition,\n\t\t\t\twidth: this.props?.ui?.width,\n\t\t\t},\n\t\t\tvalidation: this.props?.validation,\n\t\t} satisfies CFConfig<\"select\">;\n\t}\n\tget errors() {\n\t\treturn deepMerge(super.errors, {\n\t\t\trequired: {\n\t\t\t\tmessage: copy(\"server:core.fields.select.validation.required\"),\n\t\t\t},\n\t\t});\n\t}\n\toverride normalizeInputValue(value: unknown) {\n\t\treturn typeof value === \"string\" ? value.trim() : value;\n\t}\n\tgetSchemaDefinition(\n\t\tprops: GetSchemaDefinitionProps,\n\t): Awaited<ServiceResponse<SchemaDefinition>> {\n\t\treturn {\n\t\t\tdata: {\n\t\t\t\tcolumns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: this.key,\n\t\t\t\t\t\ttype: props.db.getDataType(\"text\"),\n\t\t\t\t\t\tnullable: true,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\terror: undefined,\n\t\t};\n\t}\n\tformatResponseValue(value?: string | null) {\n\t\treturn (value ??\n\t\t\tthis.config.default ??\n\t\t\tnull) satisfies CFResponse<\"select\">[\"value\"];\n\t}\n\tuniqueValidation(value: unknown) {\n\t\tconst valueSchema = z.string();\n\n\t\tconst valueValidate = zodSafeParse(value, valueSchema);\n\t\tif (!valueValidate.valid) return valueValidate;\n\n\t\tif (this.config.options) {\n\t\t\tconst optionValues = this.config.options.map((option) => option.value);\n\t\t\tif (!optionValues.includes(value as string)) {\n\t\t\t\treturn {\n\t\t\t\t\tvalid: false,\n\t\t\t\t\tmessage: copy(\"server:core.fields.select.validation.option.invalid\"),\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\treturn { valid: true };\n\t}\n}\n\nexport default SelectCustomField;\n"],"mappings":"gTAgBA,IAAM,EAAN,cAAgC,CAAsB,CACrD,KAAO,EAAkB,KACzB,OACA,IACA,MACA,YAAY,EAAa,EAA2B,CACnD,MAAM,EACN,KAAK,IAAM,EACX,KAAK,MAAQ,EACb,KAAK,OAAS,CACb,IAAK,KAAK,IACV,KAAM,KAAK,KACX,QAAS,CACR,MACC,KAAK,OAAO,SAAS,OACrB,EAAK,gB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