axiodb
Version:
A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.
51 lines • 2.63 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = schemaValidate;
/* eslint-disable @typescript-eslint/no-explicit-any */
const joi_1 = __importDefault(require("joi")); // Ensure to import Joi correctly
/**
* Validates the provided data against the given Joi schema.
*
* @param dataSchema - The Joi schema to validate against.
* @param data - The data to be validated.
* @param isUpdate - A boolean flag to indicate if the data is being validated for an update operation.
* @returns A promise that resolves with the validated data if validation is successful, or rejects with a validation error.
*/
function schemaValidate(dataSchema_1, data_1) {
return __awaiter(this, arguments, void 0, function* (dataSchema, data, isUpdate = false) {
if (isUpdate == true) {
// For update operations, we allow partial data
try {
// Use Joi.object() correctly to wrap the schema and validate data.
const joiSchema = joi_1.default.object(dataSchema).unknown(true); // Converts the provided schema to a Joi object schema
return yield joiSchema.validateAsync(data); // Validate the actual data against the schema
}
catch (error) {
return error;
}
}
else {
// For create operations, we require all data fields
try {
const joiSchema = joi_1.default.object(dataSchema); // Wrap the schema with Joi.object() for proper validation
return yield joiSchema.validateAsync(data); // Validate the actual data against the schema
}
catch (error) {
return error;
}
}
});
}
//# sourceMappingURL=validator.models.js.map