json-rules-engine-angular-ita
v1.0.4
Published
expand json-rules-engine to work with arrays and angular FormGroup
Downloads
4
Maintainers
Readme
json-rules-engine-angular-ITA
Welcome to the "json-rules-engine-angular-ITA" repository!
Overview
This open-source project contain 3 thing
- simple Angular wrapper library for JSON-RULE-ENGINE.
- one file bundle for vanilla Javascript project
what we use under the hood ?
This open-source project is a small wrapper helper utils written in TYPESCRIPT that can accept @angular/form object of a JavaScript library json-rules-engine
originally licensed under the ISC License.
Features
- easy to create complex Form cross field validations
- SOC - separate of concern - remove your form logic out of your angular component
- non programer stuff member like Qa & project managers can read the Form cross validation logic
Getting Started
Follow these steps to get your project up and running quickly.
Installation
inside your angular project:
npm install json-rules-engine-angular-ITA
2 - import the base function 2,5 - add rule if you have 3 - run the base function 4 - add to the pipe
5- run the form observble
Usage
- define the Rule in a Json format or JS object
- inside your angular component initialize the Rules Engine ,pass the relevant rules for the specific form and call it whatever you want
- call the Engine
the basic Rule Engine is actually return promise , in our example we use ngZone but you usually you will not need it
Example
define the rule . the $ sign is the define main root object path
to learn more bout how to define rules read the Json rule Engine Docs.
import { RuleProperties} from 'json-rules-engine-angular-ITA
const emailMatch = {
conditions: {
"all": [
{
"value": {
"fact":"main",
"path": "$.email"
},
"fact": "main",
"path": "$.emailConfirm",
"operator": "notEqual"
}
]
},
event: {
type: 'emailNotMatch',
params: {
formPath: 'emailConfirm'
}
},
name: 'emailCheck',
priority: 1
}
export const emailRules: RuleProperties[] = [emailMatch];
import { Component, NgZone, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms'
import { Engine } from 'json-rules-engine-angular-ITA;
import { initEngine ,rulesEngineRunZone} from 'json-rules-engine-angular-ITA;
import {emailRules} from '../rules/email.rules'
@Component({
selector: 'app-sample-email',
templateUrl: './sample-email.component.html',
styleUrls: ['./sample-email.component.scss']
})
export class SampleEmailComponent implements OnInit {
engine: Engine
emailForm: FormGroup = new FormGroup({
email: new FormControl('', [Validators.required]),
emailConfirm: new FormControl('', [Validators.required]),
});
constructor(private ngZone: NgZone) { }
ngOnInit(): void {
this.engine = initEngine(emailRules, this.emailForm);
this.emailForm.valueChanges.subscribe(res => {
this.ngZone.run(() => {
rulesEngineRunZone(this.engine,this.emailForm)
})
})
}
}
<p>sample-email</p>
<section>
<div [formGroup]=emailForm class="form-group">
<div class="form-control">
<label for="">Email</label>
<input type="text" formControlName="email">
</div>
<div>error list: {{emailForm.controls.email.errors|json}}</div>
<div *ngIf="emailForm.controls.email.errors?.required" class="error">
חובה להכניס כתובת מייל
</div>
<div class="form-control">
<label for="">Confirm Email</label>
<input type="text" formControlName="emailConfirm">
</div>
<div>error list: {{emailForm.controls.emailConfirm.errors|json}} </div>
<div *ngIf="emailForm.controls.emailConfirm.errors?.emailNotMatch" class="error">
כתובת מייל לא תואמת
</div>
</div>
</section>
<hr>
{{emailForm.value|json}}
Documentation
see in documentations and example folder in ths repo
Acknowledgments
We would like to express our gratitude to the creators of the original library for their valuable work, which served as the foundation for our customized version.
Feel free to contribute, report issues, or share your feedback to help us improve "json-rules-engine-angular-ITA". We appreciate your support!
License
This project is licensed under the MIT License - see the ISC file for details.