UNPKG

@sprucelabs/schema

Version:

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

78 lines (74 loc) • 2.15 kB
var _a; import SpruceError from '../errors/SpruceError.js'; import isUndefinedOrNull from '../utilities/isUndefinedOrNull.js'; class AbstractField { constructor(name, definition) { this.definition = definition; this.name = name; this.type = definition.type; return this; } /** For mapping schemas to types dynamically in schema values */ static generateTypeDetails() { return { valueTypeMapper: undefined, }; } /** Details needed for generating templates */ static generateTemplateDetails(_options) { throw new SpruceError({ code: 'NOT_IMPLEMENTED', instructions: `Copy and paste this into ${this.name}: public static generateTemplateDetails( options: FieldTemplateDetailOptions<{{YourFieldName}}Definition> ): IFieldTemplateDetails { const { definition } = options return { valueType: \`string\${definition.isArray ? '[]' : ''}\` } }`, }); } get options() { return this.definition.options; } get isRequired() { return !!this.definition.isRequired; } get isPrivate() { return !!this.definition.isPrivate; } get isArray() { return !!this.definition.isArray; } get label() { return this.definition.label; } get hint() { return this.definition.hint; } get minArrayLength() { var _b; return (_b = this.definition.minArrayLength) !== null && _b !== void 0 ? _b : 1; } validate(value, _) { const errors = []; if (isUndefinedOrNull(value) && this.isRequired) { errors.push({ code: 'MISSING_PARAMETER', name: this.name, label: this.label, }); } return errors; } /** Transform any value to the value type of this field */ toValueType(value, _) { return value; } } _a = AbstractField; AbstractField.description = `Please set the description for your field ${_a.name}: public static description = '*** describe your field here ***' `; export default AbstractField;