UNPKG

@sprucelabs/schema

Version:

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

39 lines (38 loc) • 1.58 kB
import AbstractField from './AbstractField.js'; class FileField extends AbstractField { static generateTemplateDetails(options) { return { valueType: `${options.importAs}.FileFieldValue${options.definition.isArray ? '[]' : ''}`, }; } validate(value, _) { var _a, _b, _c; const errors = super.validate(value); const acceptableTypes = (_b = (_a = this.definition.options) === null || _a === void 0 ? void 0 : _a.acceptableTypes) !== null && _b !== void 0 ? _b : []; if (value && !value.base64 && value.type && !this.isValidType(value.type)) { errors.push({ code: 'INVALID_PARAMETER', name: this.name, friendlyMessage: `You sent a '${value.type}' to '${(_c = this.label) !== null && _c !== void 0 ? _c : this.name}' and it only accepts '${acceptableTypes.join("', '")}'.`, }); } return errors; } isValidType(type) { var _a, _b; const types = (_b = (_a = this.definition.options) === null || _a === void 0 ? void 0 : _a.acceptableTypes) !== null && _b !== void 0 ? _b : []; const typeParts = type.split('/'); const typeStarred = `${typeParts[0]}/*`; return (types[0] === '*' || types.indexOf(type) !== -1 || types.indexOf(typeStarred) !== -1); } toValueType(value, _options) { return value; } } FileField.description = 'A way to handle files. Supports mime-type lookups.'; export default FileField;