UNPKG

@sprucelabs/schema

Version:

Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓

46 lines (45 loc) • 1.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const AbstractField_1 = __importDefault(require("./AbstractField")); const DateField_1 = require("./DateField"); class DateTimeField extends AbstractField_1.default { 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 (0, DateField_1.validateDateValue)({ value, isRequired: this.isRequired, name: this.name, }); } toValueType(value) { let normalized = value; if (normalized instanceof Date) { normalized = normalized.getTime(); } if (this.options?.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.'; exports.default = DateTimeField;