@avonjs/avonjs
Version:
A fluent Node.js API generator.
80 lines (79 loc) • 2.2 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 Field_1 = __importDefault(require("./Field"));
const EnumFilter_1 = __importDefault(require("./Filters/EnumFilter"));
class Enum extends Field_1.default {
/**
* Indicates enum values.
*/
values;
/**
* The callback to be used for the field's default value.
*/
defaultCallback = () => {
return this.isNullable() ? this.nullValue() : this.values[0];
};
/**
* The validation rules callback for creation and updates.
*/
rulesSchema = joi_1.default.string();
/**
* The validation rules callback for creation.
*/
creationRulesSchema = joi_1.default.string();
/**
* The validation rules callback for updates.
*/
updateRulesSchema = joi_1.default.string();
constructor(attribute, values, resolveCallback) {
super(attribute, resolveCallback);
this.values = values;
this.rulesSchema = joi_1.default.string().valid(...values);
this.creationRulesSchema = joi_1.default.string().valid(...values);
this.updateRulesSchema = joi_1.default.string().valid(...values);
}
/**
* Mutate the field value for response.
*/
getMutatedValue(request, value) {
return this.values.includes(value) ? value : this.nullValue();
}
/**
* Determine field is filterable or not.
*/
isFilterable() {
return true;
}
/**
* Determine field is orderable or not.
*/
isOrderable() {
return true;
}
/**
* Get the enum values.
*/
getValues() {
return this.values;
}
/**
* Make the field filter.
*/
makeFilter(request) {
return new EnumFilter_1.default(this);
}
/**
* Get the base swagger-ui schema.
*/
baseSchema(request) {
return {
...super.baseSchema(request),
enum: this.values,
};
}
}
exports.default = Enum;