@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 4.25 kB
Source Map (JSON)
{"version":3,"file":"custom-field.mjs","names":[],"sources":["../../../../../../src/libs/collection/custom-fields/fields/datetime/custom-field.ts"],"sourcesContent":["import { isValid } from \"date-fns\";\nimport z from \"zod\";\nimport type { ServiceResponse } from \"../../../../../types.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 { datetimeFieldConfig } from \"./config.js\";\n\nclass DatetimeCustomField extends CustomField<\"datetime\"> {\n\ttype = datetimeFieldConfig.type;\n\tconfig;\n\tkey;\n\tprops;\n\tconstructor(key: string, props?: CFProps<\"datetime\">) {\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\tlocalized: this.props?.localized ?? false,\n\t\t\ttime: this.props?.time ?? 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<\"datetime\">;\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(\"timestamp\"),\n\t\t\t\t\t\tnullable: true,\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthis.config.default === \"\" ? undefined : this.config.default,\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<\"datetime\">[\"value\"];\n\t}\n\toverride normalizeInputValue(value: unknown) {\n\t\treturn typeof value === \"string\" ? value.trim() : value;\n\t}\n\tuniqueValidation(value: unknown) {\n\t\tconst valueSchema = z.union([z.string(), z.number(), z.date()]);\n\n\t\tconst valueValidate = zodSafeParse(value, valueSchema);\n\t\tif (!valueValidate.valid) return valueValidate;\n\n\t\tconst date = new Date(value as string | number | Date);\n\t\tif (!isValid(date)) {\n\t\t\treturn {\n\t\t\t\tvalid: false,\n\t\t\t\tmessage: copy(\"server:core.fields.date.validation.invalid\"),\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tvalid: true,\n\t\t};\n\t}\n}\n\nexport default DatetimeCustomField;\n"],"mappings":"0RAgBA,IAAM,EAAN,cAAkC,CAAwB,CACzD,KAAO,EAAoB,KAC3B,OACA,IACA,MACA,YAAY,EAAa,EAA6B,CACrD,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,gBAAgB,KAAK,KAAK,GAAG,KAAK,IAAI,QAAS,CACnD,eAAgB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