UNPKG

@sprucelabs/schema

Version:

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

82 lines (78 loc) • 2.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); const SpruceError_1 = __importDefault(require("../errors/SpruceError")); const isUndefinedOrNull_1 = __importDefault(require("../utilities/isUndefinedOrNull")); 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_1.default({ 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() { return this.definition.minArrayLength ?? 1; } validate(value, _) { const errors = []; if ((0, isUndefinedOrNull_1.default)(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 ***' `; exports.default = AbstractField;