octonom
Version:
Object Document Mapper for any database
50 lines • 2.01 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 lodash_1 = require("lodash");
const errors_1 = require("../errors");
const schema_1 = require("./schema");
class AnySchema {
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: sanitizedValue,
};
}
toObject(instance) {
return lodash_1.cloneDeep(instance.value);
}
validate(instance) {
return __awaiter(this, void 0, void 0, function* () {
if (instance.value === undefined && this.options.required) {
throw new errors_1.ValidationError('Required value is undefined.', 'required', instance.parent);
}
yield schema_1.validate(this.options, instance);
});
}
sanitize(value, sanitizeOptions) {
if (value === undefined && sanitizeOptions.defaults) {
value = typeof this.options.default === 'function'
? this.options.default()
: this.options.default;
}
return value;
}
}
exports.AnySchema = AnySchema;
//# sourceMappingURL=any.js.map