@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
37 lines (36 loc) • 1.23 kB
JavaScript
import AbstractField from './AbstractField.js';
import { validateDateValue } from './DateField.js';
class DateTimeField extends AbstractField {
static generateTemplateDetails(options) {
return {
valueType: `${options.importAs}.DateTimeFieldValue${options.definition.isArray ? '[]' : ''}`,
};
}
validate(value, options) {
const errors = super.validate(value, options);
if (errors.length > 0) {
return errors;
}
return validateDateValue({
value,
isRequired: this.isRequired,
name: this.name,
});
}
toValueType(value) {
var _a;
let normalized = value;
if (normalized instanceof Date) {
normalized = normalized.getTime();
}
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.dateTimeFormat) === 'iso_8601') {
return new Date(value).toISOString();
}
if (typeof normalized === 'string') {
normalized = new Date(normalized).getTime();
}
return normalized ? +normalized : normalized;
}
}
DateTimeField.description = 'Date and time support.';
export default DateTimeField;