UNPKG

@vtaits/form-schema

Version:

Serialization and parsing form values by schema

74 lines (72 loc) 2.18 kB
import { defaultErrorMessages } from "./chunk-O5F4Y5EZ.js"; import { DATE_FORMAT_DATE, DATE_FORMAT_ISO, parseValueAndValidate, serializeDate } from "./chunk-4VOZ33ZJ.js"; // src/fields/datetime/constants.ts var DEFAULT_CLIENT_DATE_FORMAT = DATE_FORMAT_DATE; var DEFAULT_DISPLAY_DATE_FORMAT = "yyyy-MM-dd HH:mm"; var DEFAULT_SERVER_DATE_FORMAT = DATE_FORMAT_ISO; // src/fields/datetime/datetime.ts import { UTCDate } from "@date-fns/utc"; var datetime = { serializerSingle: ({ value, fieldSchema: { clientDateFormat: clientDateFormatParam, serverDateFormat: serverDateFormatParam, utc } }) => { const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT; const serverDateFormat = serverDateFormatParam || DEFAULT_SERVER_DATE_FORMAT; const date = parseValueAndValidate(value, clientDateFormat); if (date) { return serializeDate(utc ? new UTCDate(date) : date, serverDateFormat); } return null; }, parserSingle: ({ value, fieldSchema: { clientDateFormat: clientDateFormatParam, serverDateFormat: serverDateFormatParam } }) => { const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT; const serverDateFormat = serverDateFormatParam || DEFAULT_SERVER_DATE_FORMAT; const date = parseValueAndValidate(value, serverDateFormat); if (date) { return serializeDate(date, clientDateFormat); } return null; }, validatorBeforeSubmit: ({ value, setCurrentError, fieldSchema, fieldSchema: { clientDateFormat: clientDateFormatParam } }) => { const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT; const { errorMessages: errorMessagesParam, required } = fieldSchema; const errorMessages = { ...defaultErrorMessages, ...errorMessagesParam }; const date = parseValueAndValidate(value, clientDateFormat); if (required && !date) { setCurrentError(errorMessages.required); } } }; export { DEFAULT_CLIENT_DATE_FORMAT, DEFAULT_DISPLAY_DATE_FORMAT, DEFAULT_SERVER_DATE_FORMAT, datetime }; //# sourceMappingURL=fields_datetime.js.map