@kiwi-lib/utils
Version:
46 lines • 1.49 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JoiSchemaBuilder = void 0;
const joi_1 = __importDefault(require("joi"));
class JoiSchemaBuilder {
constructor() {
this.schema = joi_1.default['string']();
this._allowEmpty = true;
this._customOptionsAppliers = [];
}
type(type) {
this.schema = joi_1.default[type]();
return this;
}
allowEmpty(allowEmpty) {
this._allowEmpty = allowEmpty;
return this;
}
valid(valid) {
this._valid = valid;
return this;
}
default(defaultValue) {
this.defaultValue = defaultValue;
return this;
}
customOptionsAppliers(customOptionsAppliers) {
this._customOptionsAppliers = customOptionsAppliers;
return this;
}
build() {
if (this._allowEmpty)
this.schema = this.schema.empty('');
if (Array.isArray(this._valid))
this.schema = this.schema.valid(...this._valid);
if (this.defaultValue)
this.schema = this.schema.default(this.defaultValue);
this.schema = this._customOptionsAppliers.reduce((prev, curr) => curr(prev), this.schema);
return this.schema;
}
}
exports.JoiSchemaBuilder = JoiSchemaBuilder;
//# sourceMappingURL=joi-schema.builder.js.map