正規表現アクション実装

This commit is contained in:
wtl
2024-04-15 18:08:22 +09:00
parent 784cb7a473
commit da24972482
19 changed files with 696 additions and 69 deletions

View File

@@ -1,14 +1,12 @@
import { actionAddins } from ".";
import { IAction,IActionResult, IActionNode, IActionProperty, IField, IContext} from "../types/ActionTypes";
import { ConditionTree } from '../types/Conditions';
import { IAction,IActionResult, IActionNode, IActionProperty, IField} from "../types/ActionTypes";
/**
* アクションの属性定義
*/
interface IRegularCheckProps{
field:IField;
show:string;
condition:string;
message:string;
field:IField;//チェックするフィールドの対象
message:string;//エラーメッセージ
regExpression:string;//正規表現式
}
/**
* 正規表現チェックアクション
@@ -20,11 +18,10 @@ export class RegularCheckAction implements IAction{
constructor(){
this.name="正規表現チェック";
this.actionProps=[];
this.props={
this.props={
field:{code:''},
show:'',
condition:'',
message:''
message:'',
regExpression:''
}
//アクションを登録する
this.register();
@@ -35,7 +32,7 @@ export class RegularCheckAction implements IAction{
* @param event
* @returns
*/
async process(actionNode:IActionNode,event:any,context:IContext):Promise<IActionResult> {
async process(actionNode:IActionNode,event:any):Promise<IActionResult> {
let result={
canNext:true,
result:false
@@ -43,20 +40,21 @@ export class RegularCheckAction implements IAction{
try{
//属性設定を取得する
this.actionProps=actionNode.actionProps;
if (!('field' in actionNode.ActionValue) && !('show' in actionNode.ActionValue)) {
if (!('field' in actionNode.ActionValue) && !('message' in actionNode.ActionValue) && !('regExpression'in actionNode.ActionValue)) {
return result
}
this.props = actionNode.ActionValue as IRegularCheckProps;
//条件式の計算結果を取得
const conditionResult = this.getConditionResult(context);
if(conditionResult){
if(this.props.message!=='[\.\w\d\S]'){
event.error = this.props.message
}
}
result= {
canNext:true,
result:true
const record = event.record;
const value = record[this.props.field.code].value;
const regex = new RegExp(this.props.regExpression);
if(!regex.test(value)){
record[this.props.field.code].error=this.props.message;
}else{
result= {
canNext:true,
result:true
}
}
return result;
}catch(error){
@@ -65,43 +63,10 @@ export class RegularCheckAction implements IAction{
result.canNext=false;
return result;
}
}
/**
*
* @param context 条件式を実行する
* @returns
*/
getConditionResult(context:any):boolean{
const tree =this.getCondition(this.props.condition);
if(!tree){
//条件を設定されていません
return true;
}
return tree.evaluate(tree.root,context);
}
/**
* @param condition 条件式ツリーを取得する
* @returns
*/
getCondition(condition:string):ConditionTree|null{
try{
const tree = new ConditionTree();
tree.fromJson(condition);
if(tree.getConditions(tree.root).length>0){
return tree;
}else{
return null;
}
}catch(error){
return null;
}
}
};
register(): void {
actionAddins[this.name]=this;
}
}
new RegularCheckAction();
new RegularCheckAction();