GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - klaby/grule: 🛡️ Simple engine for validating rules using Rete.

BranchesTags
Open more actions menu

Latest commit

 

History

51 Commits
51 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
.husky
.husky
 
 
__tests__
__tests__
 
 
assets
assets
 
 
lib
lib
 
 
samples
samples
 
 
src
src
 
 
.editorconfig
.editorconfig
 
 
.eslintignore
.eslintignore
 
 
.eslintrc
.eslintrc
 
 
.gitignore
.gitignore
 
 
.npmignore
.npmignore
 
 
.prettierignore
.prettierignore
 
 
.prettierrc
.prettierrc
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
commitlint.config.js
commitlint.config.js
 
 
index.ts
index.ts
 
 
jest.config.ts
jest.config.ts
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 
yarn.lock
yarn.lock
 
 

Repository files navigation

grule logo

A simple and powerful mechanism for validating rules in JSON.

Built with ❤︎ by Hiukky


GitHub stars GitHub issues GitHub forks
npm bundle size
NPM

About

Grule is a minimal mechanism for testing conditions against values using a JSON scheme. Its main objective is to work as a Rete mechanism and to solve all operations in a performatic and simple way.


Installation

Using NPM

npm i grule

Using Yarn

yarn add grule

Use

To set up your rules scheme, just follow the steps below.

Create an instance

To create a test instance, you will need a load of facts to power the engine.

// Import Engine
import { Grule } from 'grule'

// Create an type
type IUser = {
  id: number
  name: string
}

// Create metadata
const metadata: IUser = {
  id: 3,
  name: 'test',
}

// Create instance
const grule = new Grule<IUser>(metadata)

Subscribe rules

After creating an instance of the engine, pre-loading with facts, the next step is to register the rules. To register the rules, you must import the IRules <T> interface and create a set of rules for the attributes declared in the facts.

The rules follow the structure of the data declared in the facts. For each attribute a rule will be executed.

The rule creation function offers 2 arguments, the first being the attributes and the second the events.

You can use a logical expression for each test and perform an action you want, or you can use the when and then event to test and perform an action based on the result. This is a good option for dynamic testing.

After creating an instance and registering the rules, you can simply execute the run method by passing the rules defined as a parameter. At the end of the tests, if there is no error that interrupts the flow of the tests, a Boolean value will be returned.

  • true all conditions have passed.
  • false one or all of the conditions failed.

// ... Previous code

// Create Rules
const rules: IRules<IUser> = ({ id, name }, { when }) => ({
  id: when(id.diff(1)).then(() => {
    throw new Error('User not allowed.')
  }),
  name: name.in(['foo', 'test']),
})

// Enroll rules
grule.run(rules)

You can also simplify the previous flow in a cleaner way. (Example of final code)

// Import Engine
import { Grule } from 'grule'

// Create an type
type IUser = {
  id: number
  name: string
}

// Create instance
new Grule<IUser>({
  id: 3,
  name: 'test',
}).run(({ id, name }, { when }) => ({
  id: when(id.diff(1)).then(() => {
    throw new Error('User not allowed.')
  }),
  name: name.in(['foo', 'test']),
}))

Available methods

For Attributes

Each attribute has 9 methods available for the tests that are.

  • less
// Type: less(value: ILess): boolean
// Acceptable: ['number', 'bigint']
  • lessOrEqual
// Type: lessOrEqual(value: ILess): boolean
// Acceptable: ['number', 'bigint']
  • greater
// Type: greater(value: IGreater): boolean
// Acceptable: ['number', 'bigint']
  • greaterOrEqual
// Type: greaterOrEqual(value: IGreater): boolean
// Acceptable: ['number', 'bigint']
  • equal
// Type: equal(value: IEqual): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'date']
  • diff
// Type: diff(value: IEqual): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'date']
  • in
// Type: in(value: In): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'array']
  • notIn
// Type: notIn(value: In): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'array']
  • eval
// Type: eval(operator: IOperatorsList, arg1: A): boolean
// Acceptable: [Idle]

For Events

Grule currently has only one event than when. It returns a promise with a boolean result from the test performed.

  • when
// Type: when(test: boolean): Promise<boolean>
// Acceptable: ['boolean']

Contributing

Grule is in an initial version without many features yet, but you can feel free to send your suggestion or open a PR.


buy me a coffee

GitHub license

About

🛡️ Simple engine for validating rules using Rete.

Topics

grulenodejsreterulesrules-engine

Resources

Readme
MIT license
Activity
Custom properties

Stars

6 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.