@ivandt/json-rules
Version:
Rule parsing engine for JSON rules
112 lines (111 loc) • 5.17 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TemplateParser_instances, _TemplateParser_objectDiscovery, _TemplateParser_resolveFieldValue;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateParser = void 0;
const object_discovery_1 = require("./object-discovery");
class TemplateParser {
constructor() {
_TemplateParser_instances.add(this);
_TemplateParser_objectDiscovery.set(this, new object_discovery_1.ObjectDiscovery());
this.TEMPLATE_REGEX = /(?<!\{)\{([^{}]+)\}(?!\})/g;
}
/**
* Checks if a value contains template syntax
* @param value The value to check
* @returns True if value contains template variables
*/
hasTemplateVariables(value) {
if (typeof value !== "string")
return false;
// Reset regex lastIndex to avoid interference
this.TEMPLATE_REGEX.lastIndex = 0;
return this.TEMPLATE_REGEX.test(value);
}
/**
* Extracts all template variables from a value
* @param value The value to parse
* @returns Array of template variables found
*/
extractTemplateVariables(value) {
if (typeof value !== "string")
return [];
const variables = [];
let match;
// Reset regex lastIndex
this.TEMPLATE_REGEX.lastIndex = 0;
while ((match = this.TEMPLATE_REGEX.exec(value)) !== null) {
variables.push({
name: match[1],
fullMatch: match[0],
startIndex: match.index,
endIndex: match.index + match[0].length,
});
}
return variables;
}
/**
* Resolves template variables in a value using provided criteria
* @param value The value containing templates
* @param criteria The criteria object to resolve field references from
* @returns The resolved value with templates replaced
*/
resolveTemplateValue(value, criteria) {
if (!this.hasTemplateVariables(value)) {
return value;
}
const variables = this.extractTemplateVariables(value);
// If the entire value is a single template variable, return the field value directly
if (variables.length === 1 && variables[0].fullMatch === value) {
const fieldValue = __classPrivateFieldGet(this, _TemplateParser_instances, "m", _TemplateParser_resolveFieldValue).call(this, variables[0].name, criteria);
return fieldValue !== undefined ? fieldValue : value;
}
// For multiple templates or templates within strings, convert to string
let resolvedValue = value;
// Replace templates from end to start to maintain correct indices
for (let i = variables.length - 1; i >= 0; i--) {
const variable = variables[i];
const fieldValue = __classPrivateFieldGet(this, _TemplateParser_instances, "m", _TemplateParser_resolveFieldValue).call(this, variable.name, criteria);
// If field value is undefined, keep the template as-is (will be handled by validation)
if (fieldValue === undefined) {
continue;
}
resolvedValue =
resolvedValue.substring(0, variable.startIndex) +
fieldValue +
resolvedValue.substring(variable.endIndex);
}
return resolvedValue;
}
/**
* Validates that all template variables in a value exist in the criteria
* @param value The value containing templates
* @param criteria The criteria object to validate against
* @returns Object with validation result and missing fields
*/
validateTemplateVariables(value, criteria) {
const variables = this.extractTemplateVariables(value);
const missingFields = [];
for (const variable of variables) {
const fieldValue = __classPrivateFieldGet(this, _TemplateParser_instances, "m", _TemplateParser_resolveFieldValue).call(this, variable.name, criteria);
if (fieldValue === undefined) {
missingFields.push(variable.name);
}
}
return {
isValid: missingFields.length === 0,
missingFields,
};
}
}
exports.TemplateParser = TemplateParser;
_TemplateParser_objectDiscovery = new WeakMap(), _TemplateParser_instances = new WeakSet(), _TemplateParser_resolveFieldValue = function _TemplateParser_resolveFieldValue(fieldName, criteria) {
if (fieldName.includes(".")) {
return __classPrivateFieldGet(this, _TemplateParser_objectDiscovery, "f").resolveNestedProperty(fieldName, criteria);
}
return criteria[fieldName];
};