@ournet/domain
Version:
Ournet domain
36 lines (35 loc) • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JoiEntityValidator = void 0;
const joi_1 = require("joi");
class JoiEntityValidator {
constructor(options) {
this.options = options;
}
onCreate(data) {
const result = validateSchema(this.options.createSchema, data);
if (result.error) {
throw result.error;
}
return result.value;
}
onUpdate(data) {
const result = validateSchema(this.options.updateSchema, data);
if (result.error) {
throw result.error;
}
return result.value;
}
}
exports.JoiEntityValidator = JoiEntityValidator;
function validateSchema(schema, data) {
return joi_1.validate(data, schema, {
allowUnknown: false,
abortEarly: true,
convert: true,
noDefaults: false,
presence: "optional",
stripUnknown: false,
skipFunctions: false
});
}