npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nhaancs/rule-engine

v0.0.6

Published

This module is responsible for validate and provide a set of common data validation rules.

Downloads

18

Readme

rules-engine

This module is responsible for validate and provide a set of common data validation rules.

Code structure

/src/lib/rules

All rules are specified in this folder.

Important interfaces/classes and their relationships:

/**
 * An interface that defines the required 
 * shape/structure for all rules. 
 */
interface IRuleComponent {
    execute(): RuleResult;
}
/**
 * New rules should extend [SimpleRule] or [CompositeRule],
 * these rule abstractions extend [RulePolicy].
 */
export class RulePolicy implements IRuleComponent {
    // implementation hidden
}
/**
 * Use this class as a base [extends] class for 
 * simple rules. A simple rule contains a single 
 * rule and target to evaluate.
 */
export class SimpleRule extends RulePolicy {
    // implementation hidden
}
/**
 * Use the [CompositeRule] as a base class 
 * for a complex rule - a rule that contains
 * other rules.
 */
export class CompositeRule extends RulePolicy {
    // implementation hidden
}

/src/lib/service

Important interfaces/classes and their relationships:

/**
 * Use this class to create a message for the 
 * current [ServiceContext].
 */
export class ServiceMessage {
    // implementation hidden
}
/**
 * Use this class to manage the context 
 * of a single service call.
 *  
 * This class will contain a list of 
 * any service messages added during 
 * the processing of a service request.
 */
export class ServiceContext {
    // implementation hidden
}

/src/lib/validation

Important interfaces/classes and their relationships:

/**
 * Use this interface class to define the structure 
 * of a Validation Context.
 */
export interface IValidationContext {
  /**
   * Use to indicate the status of the validation context. 
   * The value is [true] when all rules are evaluated 
   * without violations.
   */
  isValid: boolean;
  /**
   * Use to indicate the state of the validation context.
   */
  state: ValidationContextState;
  /**
   * A list of results for all rules evaluated.
   */
  results: RuleResult[];
  /**
   * A list of rules that will be evaluated.
   */
  rules: RulePolicy[];

  /**
   * Implement this method to indicate if the validation 
   * context contains any rule violations. Returns [true]
   * when there are one or more rule violations.
   */
  hasRuleViolations(): boolean;

  /**
   * Implement this method to render the rules contained in the validation context.
   */
  renderRules(): IValidationContext;
}
/**
 * Use this class to create a new Validation Context 
 * for your application. 
 * With this context, you can add rules and 
 * evaluate the rules.
 *
 * After the rules are evaluated, you can use the 
 * Validation Contextto determine if there are 
 * any rule violations.
 */
export class ValidationContext implements IValidationContext {
    // implementation hidden
}

Example usage:

// New ValidationContext
const validationContext = new ValidationContext();

// Add rules

validationContext.addRule(
    new IsTrue( // SimpleRule
        'LogWriterExists', 
        'The log writer is not configured.', 
        this.hasWriter
    )
);
validationContext.addRule(
    new StringIsNotNullEmptyRange( // CompositeRule
    'SourceIsRequired',
    'The entry source is not valid.',
    this.targetEntry.source,
    1,
    100
    )
);

// Start evaluate rules and
// Add rule evaluation messages to service context
validationContext
    .renderRules()
    .results
    .forEach(ruleResult => {
        const serviceMessage = new ServiceMessage(
            ruleResult.rulePolicy.name,
            ruleResult.rulePolicy.message,
            MessageType.Error
        );
        serviceMessage.DisplayToUser = ruleResult.rulePolicy.isDisplayable;
        serviceMessage.Source = this.actionName;
        this.serviceContext.Messages.push(serviceMessage);
    })

if (validationContext.hasRuleViolations()) {
    this.serviceContext.Messages.forEach(e => {
        if (e.MessageType === MessageType.Error) {
            this.loggingService.log(Severity.Error, e.toString());
        }
    });
}

Notes on tsconfig.json

...
"angularCompilerOptions": {
  // publishable library cannot build with ivy
  "enableIvy": false
},
...

This library was generated with Nx.

Running unit tests

Run nx test rules-engine to execute the unit tests.