@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
30 lines (29 loc) • 1.19 kB
JavaScript
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 &&
acceptableTypes[0] !== '*' &&
acceptableTypes.indexOf(value.type) === -1) {
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;
}
toValueType(value, _options) {
return value;
}
}
FileField.description = 'A way to handle files. Supports mime-type lookups.';
export default FileField;