@avonjs/avonjs
Version:
A fluent Node.js API generator.
121 lines (120 loc) • 3.35 kB
JavaScript
"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 Field_1 = __importDefault(require("./Field"));
// TODO: Should be removed or renamed.
// biome-ignore lint/suspicious/noShadowRestrictedNames:
class Array extends Field_1.default {
/**
* The validation rules callback for creation and updates.
*/
rulesSchema = joi_1.default.array().items(joi_1.default.any());
/**
* The validation rules callback for creation.
*/
creationRulesSchema = joi_1.default.array().items(joi_1.default.any());
/**
* The validation rules callback for updates.
*/
updateRulesSchema = joi_1.default.array().items(joi_1.default.any());
/**
* Minimum length of the array
*/
minItems;
/**
* Maximum length of the array
*/
maxItems;
/**
* Indicates items schema.
*/
itemsSchema = {
type: 'string',
minItems: 1,
};
/**
* 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.array(requestAttribute);
model.setAttribute(attribute, this.isValidNullValue(value)
? this.nullValue()
: (0, collect_js_1.default)(value).values().all());
}
/**
* Specifies the exact number of items in the array.
*/
length(limit = 0) {
this.min(limit).max(limit).rules(joi_1.default.array().length(limit));
return this;
}
/**
* Specifies the minimum number of items in the array.
*/
min(minItems = 0) {
this.minItems = minItems;
this.rules(joi_1.default.array().min(minItems));
return this;
}
/**
* Specifies the maximum number of items in the array.
*/
max(maxItems = 0) {
this.maxItems = maxItems;
this.rules(joi_1.default.array().max(maxItems));
return this;
}
/**
* Mutate the field value for response.
*/
getMutatedValue(request, value) {
return (0, collect_js_1.default)(value).values().all();
}
/**
* Determine field is filterable or not.
*/
isFilterable() {
return false;
}
/**
* Determine field is orderable or not.
*/
isOrderable() {
return false;
}
/**
* Specify items schema.
*/
items(itemsSchema) {
this.itemsSchema = itemsSchema;
return this;
}
/**
* Get the value considered as null.
*/
nullValue() {
return [];
}
/**
* Get the base swagger-ui schema.
*/
baseSchema(request) {
return {
...super.baseSchema(request),
type: 'array',
items: this.itemsSchema,
uniqueItems: true,
minItems: this.minItems,
maxItems: this.maxItems,
};
}
}
exports.default = Array;