@vtaits/form-schema
Version:
Serialization and parsing form values by schema
73 lines (71 loc) • 2.14 kB
JavaScript
import {
defaultErrorMessages
} from "./chunk-O5F4Y5EZ.js";
import {
DATE_FORMAT_DATE,
parseValueAndValidate,
serializeDate
} from "./chunk-4VOZ33ZJ.js";
// src/fields/date/constants.ts
var DEFAULT_CLIENT_DATE_FORMAT = DATE_FORMAT_DATE;
var DEFAULT_DISPLAY_DATE_FORMAT = "yyyy-MM-dd";
var DEFAULT_SERVER_DATE_FORMAT = "yyyy-MM-dd";
// src/fields/date/date.ts
import { UTCDate } from "@date-fns/utc";
var date = {
serializerSingle: ({
value,
fieldSchema: {
clientDateFormat: clientDateFormatParam,
serverDateFormat: serverDateFormatParam,
utc
}
}) => {
const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT;
const serverDateFormat = serverDateFormatParam || DEFAULT_SERVER_DATE_FORMAT;
const date2 = parseValueAndValidate(value, clientDateFormat);
if (date2) {
return serializeDate(utc ? new UTCDate(date2) : date2, 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 date2 = parseValueAndValidate(value, serverDateFormat);
if (date2) {
return serializeDate(date2, clientDateFormat);
}
return null;
},
validatorBeforeSubmit: ({
setCurrentError,
value,
fieldSchema,
fieldSchema: { clientDateFormat: clientDateFormatParam }
}) => {
const clientDateFormat = clientDateFormatParam || DEFAULT_CLIENT_DATE_FORMAT;
const { errorMessages: errorMessagesParam, required } = fieldSchema;
const errorMessages = {
...defaultErrorMessages,
...errorMessagesParam
};
const date2 = parseValueAndValidate(value, clientDateFormat);
if (required && !date2) {
setCurrentError(errorMessages.required);
}
}
};
export {
DEFAULT_CLIENT_DATE_FORMAT,
DEFAULT_DISPLAY_DATE_FORMAT,
DEFAULT_SERVER_DATE_FORMAT,
date
};
//# sourceMappingURL=fields_date.js.map