@avonjs/avonjs
Version:
A fluent Node.js API generator.
110 lines (109 loc) • 3.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const joi_1 = __importDefault(require("joi"));
const FieldCollection_1 = __importDefault(require("../Collections/FieldCollection"));
const Models_1 = require("../Models");
const Field_1 = __importDefault(require("./Field"));
class Json extends Field_1.default {
/**
* The validation rules callback for creation and updates.
*/
rulesSchema = joi_1.default.object();
/**
* The validation rules callback for creation.
*/
creationRulesSchema = joi_1.default.object();
/**
* The validation rules callback for updates.
*/
updateRulesSchema = joi_1.default.object();
/**
* 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].keys(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].keys(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(value));
}
/**
* Mutate the field value for response.
*/
getMutatedValue(request, value) {
const attributes = new Models_1.Fluent(typeof value === 'string' ? JSON.parse(value) : value);
return new FieldCollection_1.default(this.fields.each((field) => field.resolve(attributes))).mapWithKeys((field) => [field.attribute, field.getValue(request)]);
}
/**
* 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: 'object',
properties: this.fields.responseSchemas(request),
};
}
/**
* Get the swagger-ui schema.
*/
payloadSchema(request) {
return {
...super.payloadSchema(request),
type: 'object',
properties: this.fields.payloadSchemas(request),
};
}
}
exports.default = Json;