octonom
Version:
Object Document Mapper for any database
52 lines • 2.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("../errors");
const schema_1 = require("./schema");
class BooleanSchema {
constructor(options = {}) {
this.options = options;
}
create(value, sanitizeOptions = {}) {
const sanitizedValue = this.sanitize(value, sanitizeOptions);
if (sanitizedValue === undefined) {
return undefined;
}
return {
parent: sanitizeOptions.parent,
schema: this,
value: this.sanitize(value, sanitizeOptions),
};
}
toObject(instance) {
return instance.value;
}
validate(instance) {
return __awaiter(this, void 0, void 0, function* () {
yield schema_1.validate(this.options, instance);
});
}
sanitize(value, sanitizeOptions = {}) {
if (value === undefined && sanitizeOptions.defaults) {
return typeof this.options.default === 'function'
? this.options.default()
: this.options.default;
}
if (value === undefined) {
return undefined;
}
if (value !== true && value !== false) {
throw new errors_1.SanitizationError('Value is not a boolean.', 'no-boolean', sanitizeOptions.parent);
}
return value;
}
}
exports.BooleanSchema = BooleanSchema;
//# sourceMappingURL=boolean.js.map