UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

113 lines (112 loc) 3.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const collect_js_1 = __importDefault(require("collect.js")); const joi_1 = __importDefault(require("joi")); const FieldCollection_1 = __importDefault(require("../Collections/FieldCollection")); const Field_1 = __importDefault(require("./Field")); class List extends Field_1.default { /** * The validation rules callback for creation and updates. */ rulesSchema = joi_1.default.array(); /** * The validation rules callback for creation. */ creationRulesSchema = joi_1.default.array(); /** * The validation rules callback for updates. */ updateRulesSchema = joi_1.default.array(); /** * The object possible keys. */ fields; constructor(attribute, fields = [], resolveCallback) { super(attribute, resolveCallback); this.fields = new FieldCollection_1.default(fields); this.default((request) => { return [ new FieldCollection_1.default(this.fields.each((field) => field.resolveDefaultValue(request))).fieldValues(request), ]; }); } /** * Get the creation rules for this field. */ getCreationRules(request) { let rules = {}; this.fields.each((field) => { rules = { ...rules, ...field.getCreationRules(request) }; }); return { [this.attribute]: super.getCreationRules(request)[this.attribute].items(rules), }; } /** * Get the update rules for this field. */ getUpdateRules(request) { let rules = {}; this.fields.each((field) => { rules = { ...rules, ...field.getUpdateRules(request) }; }); return { [this.attribute]: super.getUpdateRules(request)[this.attribute].items(rules), }; } /** * Hydrate the given attribute on the model based on the incoming request. */ fillAttributeFromRequest(request, requestAttribute, model, attribute) { if (!request.exists(requestAttribute)) { this.fillAttributeFromDefault(request, model, attribute); return; } const value = request.get(requestAttribute); model.setAttribute(attribute, this.isValidNullValue(value) ? this.nullValue() : JSON.stringify((0, collect_js_1.default)(value).values().all())); } /** * Mutate the field value for response. */ getMutatedValue(request, value) { return typeof value === 'string' ? JSON.parse(value) : value; } /** * Determine field is filterable or not. */ isFilterable() { return false; } /** * Determine field is orderable or not. */ isOrderable() { return false; } /** * Get the swagger-ui schema. */ responseSchema(request) { return { ...super.responseSchema(request), type: 'array', items: this.fields.responseSchemas(request), }; } /** * Get the swagger-ui schema. */ payloadSchema(request) { return { ...super.payloadSchema(request), type: 'array', items: this.fields.payloadSchemas(request), }; } } exports.default = List;