
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
xml-rule-builder
Advanced tools
A TypeScript library for building and managing XML-based rule systems with React components
A TypeScript library for building and managing XML-based rule systems, with optional React components for UI integration.
npm install xml-rule-builder
import {
xmlToRules,
rulesToXml,
validateRuleBlock,
createDefaultGroup,
type RuleBlock
} from 'xml-rule-builder';
// Convert XML to rule structure
const xmlString = `<rules><group logic="AND"><age comparator="greater_than">18</age></group></rules>`;
const rules = xmlToRules(xmlString);
// Convert rules back to XML
const xml = rulesToXml(rules);
// Validate rules
const validation = validateRuleBlock(rules);
if (!validation.isValid) {
console.log('Validation errors:', validation.errors);
}
// Create default rule structure
const defaultRules = createDefaultGroup();
The library allows you to define custom rule types and comparators:
import { setXmlTypes, type XmlRuleType } from 'xml-rule-builder';
const customTypes: XmlRuleType[] = [
{
label: 'GPA',
value: 'gpa',
comparators: [
{ label: 'Greater Than', value: 'greater_than' },
{ label: 'Less Than', value: 'less_than' },
{ label: 'Equals', value: 'equals' }
]
},
{
label: 'Course Level',
value: 'course_level',
comparators: [
{ label: 'Equals', value: 'equals' },
{ label: 'Not Equals', value: 'not_equals' }
]
}
];
setXmlTypes(customTypes);
import { loadXmlTypesFromConfig, type XmlTypesConfig } from 'xml-rule-builder';
const config: XmlTypesConfig = {
ruleTypes: [
{
label: 'Age',
value: 'age',
comparators: [
{ label: 'Equals', value: 'equals' },
{ label: 'Less Than', value: 'less_than' },
{ label: 'Greater Than', value: 'greater_than' }
]
}
]
};
loadXmlTypesFromConfig(config);
import { loadXmlTypesFromFile } from 'xml-rule-builder';
// Load from a JSON file
await loadXmlTypesFromFile('./config/xml-types.json');
Configuration file format (xml-types.json
):
{
"ruleTypes": [
{
"label": "Age",
"value": "age",
"comparators": [
{ "label": "Equals", "value": "equals" },
{ "label": "Less Than", "value": "less_than" },
{ "label": "Greater Than", "value": "greater_than" }
]
},
{
"label": "GPA",
"value": "gpa",
"comparators": [
{ "label": "Greater Than", "value": "greater_than" },
{ "label": "Less Than", "value": "less_than" }
]
}
]
}
import { addXmlTypes } from 'xml-rule-builder';
const additionalTypes = [
{
label: 'Test Score',
value: 'test_score',
comparators: [
{ label: 'Greater Than', value: 'greater_than' },
{ label: 'Less Than', value: 'less_than' }
]
}
];
addXmlTypes(additionalTypes);
import { resetToDefaultTypes } from 'xml-rule-builder';
resetToDefaultTypes();
import { XmlRuleBuilder } from 'xml-rule-builder';
function MyComponent() {
const handleXmlChange = (xml: string) => {
console.log('Generated XML:', xml);
};
const handleValidationChange = (isValid: boolean, errors: string[]) => {
if (!isValid) {
console.log('Validation errors:', errors);
}
};
// Use with custom types
const customConfig = {
xmlTypes: [
{
label: 'Custom Field',
value: 'custom_field',
comparators: [
{ label: 'Equals', value: 'equals' },
{ label: 'Not Equals', value: 'not_equals' }
]
}
]
};
return (
<XmlRuleBuilder
onXmlChange={handleXmlChange}
onValidationChange={handleValidationChange}
config={customConfig}
/>
);
}
xmlToRules(xmlString: string): RuleBlock
Parse XML string to rule structure.
rulesToXml(rules: RuleBlock, options?: XmlBuilderOptions): string
Convert rules to XML string.
Options:
indent?: boolean
- Enable pretty printing (default: true)prettyPrint?: boolean
- Enable indentation (default: true)validateRuleBlock(block: RuleBlock): ValidationResult
Validate rule structure and return validation result.
createDefaultRule(): RuleBlock
Create a default rule with the first available rule type.
createDefaultGroup(): RuleBlock
Create a default group containing one default rule.
createRuleWithType(ruleTypeValue: string): RuleBlock
Create a rule with a specific rule type.
setXmlTypes(customTypes: XmlRuleType[]): void
Replace all XML types with custom types.
loadXmlTypesFromConfig(config: XmlTypesConfig): void
Load XML types from a configuration object.
loadXmlTypesFromFile(filePath: string): Promise<void>
Load XML types from a JSON configuration file.
addXmlTypes(additionalTypes: XmlRuleType[]): void
Add additional types to existing ones.
resetToDefaultTypes(): void
Reset to the default XML types.
updateBlockByPath(root: RuleBlock, path: number[], updater: (block: RuleBlock) => RuleBlock): RuleBlock
Update a block at a specific path using an updater function.
addRuleToGroup(group: RuleBlock, index: number): RuleBlock
Add a new rule to a group at the specified index.
addGroupToGroup(group: RuleBlock, index: number): RuleBlock
Add a new group to a group at the specified index.
removeBlockFromGroup(group: RuleBlock, index: number): RuleBlock
Remove a block from a group at the specified index.
RuleBlock
Union type representing either a rule or a group:
type RuleBlock =
| {
type: 'rule';
ruleType: XmlRuleType;
comparator: XmlComparator;
value: string;
}
| {
type: 'group';
logic: 'AND' | 'OR';
children: RuleBlock[];
};
XmlRuleType
Represents a rule type with its comparators:
interface XmlRuleType {
label: string;
value: string;
comparators: XmlComparator[];
}
XmlComparator
Represents a comparator for a rule type:
interface XmlComparator {
label: string;
value: string;
}
XmlTypesConfig
Configuration object for XML types:
interface XmlTypesConfig {
ruleTypes: XmlRuleType[];
}
XmlRuleBuilderConfig
Configuration for the React component:
interface XmlRuleBuilderConfig {
xmlTypes?: XmlRuleType[];
configFile?: string;
}
ValidationResult
Result of rule validation:
interface ValidationResult {
isValid: boolean;
errors: string[];
}
XmlRuleBuilder
A React component for building XML rules with a visual interface.
Props:
onXmlChange?: (xml: string) => void
- Callback when XML changesinitialRules?: RuleBlock | null
- Initial rule structureonValidationChange?: (isValid: boolean, errors: string[]) => void
- Callback when validation changesconfig?: XmlRuleBuilderConfig
- Configuration for custom typesThe library includes default rule types that can be replaced or extended:
<rules>
<group logic="AND">
<age comparator="greater_than">18</age>
<income comparator="less_than">50000</income>
<group logic="OR">
<dob comparator="after">2000-01-01</dob>
<enrolment_status comparator="equals">full_time</enrolment_status>
</group>
</group>
</rules>
npm run build
npm run dev
npm run lint
npm test
GPL-3.0
FAQs
A TypeScript library for building and managing XML-based rule systems with React components
The npm package xml-rule-builder receives a total of 6 weekly downloads. As such, xml-rule-builder popularity was classified as not popular.
We found that xml-rule-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.