rules-machine icon indicating copy to clipboard operation
rules-machine copied to clipboard

Create unified Rule types & runtime check helpers

Open justsml opened this issue 1 year ago • 0 comments

Create unified Rule types & runtime check helpers

export type Rule =
  | string
  | IfThenElseRule
  | AndRule
  | OrRule
  | ReturnRule
  | TryCatchRule
  | Rule[];

type RulesStrings = string | string[];
 
interface ReturnRule {
  return: RulesStrings
}

interface TryCatchRule { try: Rule, catch: Rule }

interface IfThenElseRule {
  if: AndRule | OrRule | string
  then: Rule
  else?: Rule
}
const validateRules = {
  isString: rule => typeof rule === 'string' && rule.length > 4,
  isStringOrStringList: rule => Array.isArray(rule) ? rule.every(validateRules.isString) : validateRules.isString(rule),
  isLogicalRule: rule => rule != null && ('and' in rule || 'or' in rule),
  isStructuralRule: rule => rule != null && ('if' in rule || 'and' in rule || 'or' in rule || 'map' in rule || 'reduce' in rule || 'filter' in rule || 'return' in rule),
  // TODO: add more combined rules to express higher order structures, constraints, etc
}

https://github.com/elite-libs/rules-machine/pull/46#discussion_r1012405866

https://github.com/elite-libs/rules-machine/pull/47#discussion_r1009072065

justsml avatar Nov 03 '22 23:11 justsml