@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
47 lines (46 loc) • 1.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
const AbstractField_1 = __importDefault(require("./AbstractField"));
class DirectoryField extends AbstractField_1.default {
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?.relativeTo;
if (stringValue) {
path = stringValue;
}
else if (typeof value === 'object') {
path = value.path;
}
if (!(typeof path === 'string') || path.length === 0) {
throw new SpruceError_1.default({
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!';
exports.default = DirectoryField;