UNPKG

@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
import SpruceError from '../errors/SpruceError.js'; import AbstractField from './AbstractField.js'; class DirectoryField extends AbstractField { static generateTemplateDetails(options) { const { definition } = options; return { valueType: `${options.importAs}.IDirectoryFieldValue${definition.isArray ? '[]' : ''}`, }; } toValueType(value, options) { const stringValue = typeof value === 'string' || value.toString ? value.toString() : undefined; let path; const relativeTo = options === null || options === void 0 ? void 0 : options.relativeTo; if (stringValue) { path = stringValue; } else if (typeof value === 'object') { path = value.path; } if (!(typeof path === 'string') || path.length === 0) { throw new SpruceError({ code: 'TRANSFORMATION_ERROR', fieldType: 'directory', incomingTypeof: typeof value, incomingValue: value, name: this.name, }); } if (path && relativeTo) { const pathUtil = require('path'); path = pathUtil.relative(relativeTo, path) || path; } return { path }; } } DirectoryField.description = 'A way to select entire directories once!'; export default DirectoryField;