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

validate-rc

v1.0.0

Published

Validate config files following user-defined rules.

Downloads

2

Readme

Usage

NOTE: This library is intended to be used for config files, but if you really want to, you can use it like a 'runtime interface'.

Example

// dummy.ts

// import validateRc
import validateRc, { Optional, Choice } from 'validate-rc'

// the rules that the rc should follow
// rule to validate a custom package.json
const rules = {
    name: String,
    version: String,
    description: Optional(Object),
    scripts: Optional(Object),
    keywords: Array,
    dependencies: Optional(Object),
    devDependencies: Optional(Object),
    author: String,
    license: String,
    bin: Optional(Object),
    repository: Optional({
        type: String,
        url: String
    })
}

validateRc(rules, require('./package.js'))

validateRc(rules: object, rc: object): boolean | never

Takes two objects, rules and rc. rules is the object that holds the rules, rc is the object that needs to be validated.

Syntax

The rules' syntax is made of constructors and branches.

Example:

const rules = {
    // only matches numbers
    num: Number,
    // only matches strings
    str: String,
    // only matches booleans
    bool: Boolean
    // this is valid for all primitives, promises, buffers etc cannot be used
    branch: {
        // other constructors
    }
}

Optional

Optional is a function that accepts a constructor or branch. If in the config file the key associated with Optional isn't present, it does not throw. But, if that key is present and it violates the rule that is specified in Optional, throws.

Example

import validateRc, { Optional } from 'validate-rc'

const rules = { 
    age: Optional(Number),
    otherInfo: Optional({
        hobbies: Array,
        height: Number,
        name: String
    })
}

validateRc(rules, {})  // does not throw, because `age` and `otherInfo` are optional
validateRc(rules, { age: 24 }) // does not throw, because `otherInfo` is optional
validateRc(rules, {  // does not throw, because `otherInfo`'s rules are not violated
    otherInfo: {
        hobbies: [ 'Programming' ],
        height: 179,
        name: 'Michael'
    }
})
validateRc(rules, {  // throws, because `age` is a string
    age: 'string'
})
validateRc(rules, {  // throws, because `otherInfo`'s rules are violated
    otherInfo: {}
})
validateRc(rules, {  // throws, because otherInfo is a boolean and not an object
    otherInfo: false
})

Choice

Choice is a function that takes multiple arguments as choices, throws when the value in the config isn't present in Choice.

Example

import validateRc, { Optional, Choice } from 'validate-rc'

const rule = { esVersion: Optional(Choice('es5', 'es6', 'es2015', 'esnext')) }

validateRc(rule, {}) // does not throw, because its optional
validateRc(rule, { esVersion: 'es6' }) // does not throw, because 'es6' is in `Choice`
validateRc(rule, { esVersion: 'es2017' }) // throws, because 'es2017' isn't in `Choice`