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

rulya

v1.0.8

Published

Apply rules for semantic validation of YAML files

Downloads

3

Readme

Rulya

npm

Helps you define and apply semantic validation to a collection of YAML files. Typically used as pre-commit hook or CI/CD build step.

Usage

Create a NPM package in the directory containing the files to validate:

npm init

And set the test script to:

jasmine --require=rulya validate.js

Add Rulya as a development dependency:

npm install rulya --save-dev

Write your validation rules in validate.js:

// Traverse all YAML files in the test-data/articles directory
documents("test-data/articles/**/*.yaml", (doc, path) => {

    // Validate against a JSON schema (can be a URL as well)
    schema("test-data/articles/article.schema.json", doc);

    // Define a rule on the filename
    rule("filename", path, "must have a filename containing the string 'article'", () => {
        expect(path.split("/").slice(-1)[0]).toContain("article");
    });
});

// Traverse all YAML files in the test-data/books directory
documents("test-data/books/**/*.yaml", (doc) => {

    // Iterate through the chapters
    property("chapters", doc.chapters, chapter =>

        // Group rules applied to the chapter titles
        property("title", chapter.title, title => {

            // Define rules for the chapter titles

            rule("capitalized", title, "must start with a capital letter", () => {
                expect(title).toMatch("^[A-Z].*");
            });

            rule("letters-only", title, "must contains only letters", () => {
                expect(title).toMatch("^[A-Za-z ]*$");
            });
        }));

    // A rule can be defined standalone, the property name must be writen in the description though
    rule("page-number", doc.pages, "'pages' must be positive if present", pages => {
        expect(pages == null || pages > 0).toBeTrue();
    });
});

Run your rules with:

npm test

Breaking the Rules

If for some reason you need to disable some rule, use a skip comment with the rule names.

    - title: "a fantastic tale no 1" # skip(capitalized,letters-only)

For rules applying to the whole document, the skip comment must be on the first line:

# skip(schema,filename)
text: Hello

Expectations

Rulya uses Jasmine under the hood, you can use all its matchers in the rule definitions.

API

| | | |-|-| | documents(pattern,callback) | Traverses and parses YAML documents using a glob pattern. | | property(name,value,callback) | Steps down in a property to group multiple rules together. It creates a describe section at the Jasmine level. name and value typically corresponds to a YAML map entry. callback will be called with value as parameter or each item if value is an array. | | rule(name,value,description,callback) | Defines a rule. name identifies the rule if you want to skip it (see Breaking the Rules, above). value is the YAML element the rule applies to. This creates a single spec at the Jasmine level. | | schema(reference, doc) | Validates a document against a JSON Schema. reference is the filesystem path or an URL pointing to the schema. | ||

Customization

You can use Jasmine's describe and it functions if you need more flexibility than property and rule provide. Just note that rule manages the skip comment for you.