@martinmilo/verve
Version:
TypeScript domain modeling library with field-level authorization, business rule validation, and context-aware access control
32 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssociationRegistry = void 0;
function resolvePath(obj, path) {
return path.split('.').reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
}
class AssociationRegistry {
static register(association) {
this.associations.push(association);
const { sourceModel, fieldName, sourcePath, targetPath, } = association;
const key = `${sourceModel}.${fieldName}`;
const validator = (sourceModelInstance, targetModelInstance) => {
const sourceVal = resolvePath(sourceModelInstance, sourcePath);
const targetVal = resolvePath(targetModelInstance, targetPath);
return sourceVal === targetVal;
};
this.validators[key] = validator;
}
static getValidator(sourceModel, fieldName) {
return this.validators[`${sourceModel}.${fieldName}`];
}
static allValidators() {
return { ...this.validators };
}
static allAssociations() {
return [...this.associations];
}
}
exports.AssociationRegistry = AssociationRegistry;
AssociationRegistry.associations = [];
AssociationRegistry.validators = {};
//# sourceMappingURL=AssociationRegistry.js.map