@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
42 lines (41 loc) • 1.45 kB
JavaScript
import AbstractField from './AbstractField.js';
import { validateDateValue } from './DateField.js';
class DateTimeField extends AbstractField {
static generateTemplateDetails(options) {
const { definition, importAs, language } = options;
const { isArray } = definition;
const arrayNotation = isArray ? '[]' : '';
return {
valueType: language === 'go'
? `${arrayNotation}${importAs}.DateTimeFieldValue`
: `${importAs}.DateTimeFieldValue${arrayNotation}`,
};
}
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;