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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rokot-validate

v0.5.6

Published

Rocketmakers Rokot Platform - Validation

Readme

rokot-validate

Rokot - Rocketmakers TypeScript NodeJs Platform

Introduction

A typescript framework for Validation.

This library extends the validate.js npm with additional validators.

It provides abstract Validator<T> and ValidatorWithContext<T, TContext> class to create validator instances

The only difference between the 2 validators is that ValidatorWithContext provides a method to obtain context data that is needed within the creation of the ConstraintSpec

NOTE: When an object is validated it is returned 'clean' (e.g. attributes NOT specified on the validator will be removed)

Getting Started

Installation

Install via npm

npm i rokot-validate -S

Example:

import {ValidatorWithContext, Validator, ConstraintSpec} from "rokot-validate";

export interface IEntity {
  id: string;
  members?: number[];
  subEntities: ISubEntity[];
}
export interface ISubEntity {
  id: string
}

// Create the 'Validator Spec'
class SubEntityValidator extends Validator<ISubEntity> {
  constraints(): ConstraintSpec<ISubEntity> {
    return {
      id: this.mandatoryString()
    }
  }
}

export class EntityValidator extends ValidatorWithContext<IEntity, number>{
  getContext(entity, isRootValidator) {
    return Promise.resolve(1);
  }
  constraints(context: number) :ConstraintSpec<IEntity> {
    //console.log("context", context);
    return {
      id: this.mandatoryString(),
      deleteMe:{
        absence: true
      },
      subEntities: {
        array: true,
        presence: true,
        validator: new SubEntityValidator()
      },
      members: {
        array: {
          numericality: this.integerStrict({greaterThan:0})
        }
      }
    };
  }
}

const validator = new EntityValidator();
validator.validate({ id: "1",members:[1,2], subEntities:[{id:"2"}], extraneous:true })
  .then((entity: IEntity) => {
    // you now have a clean and verified entity
    // e.g. {id: "1",members:[1,2], subEntities:[{id:"2"}] }
  })
  .catch(error => {
    // the error might be a real error (if your custom functions failed)
    // or most likely it will be a ValidationError (implements IValidationErrors)
  })

Additional Validators

string

Ensure the value is a string

Example: name is a mandatory string

name: {
  presence: true,
  string: true
}

boolean

Ensure the value is a boolean

Example: active is a mandatory boolean

active: {
  presence: true,
  boolean: true
}

array

Ensure the value is an array

Example: tags is an array

tags: {
  array: true
}

Example: counts is a numeric array

counts: {
  array: {
    numericality: true
  }
}

absence

Ensure the property explicitly doesn't exist on the object

notAllowed: {
  absence: true
}

defined

Ensure the value is not null or undefined

must

Allows a custom function (or functions) to determine validity

Example: single function to check existence in database

roleId: {
  must: {
    func: this.mustValidator<string>(id => db.checkRoleId(id)),
    message: "does not exist"
  }
}

Example: multiple functions to check existence in database, and perform secondary check

roleId: {
  must: [{
    func: this.mustValidator<string>(id => db.checkRoleId(id)),
    message: "does not exist"
  },{
    func: this.mustValidator<string>(id => db.someOtherCheck(id)),
    message: "does not pass other check"
  }]
}

Example: single function to check existence in database (called for each item in array)

roleIds: {
  array: true,
  must: {
    func: this.mustValidator<string>(id => db.checkRoleId(id)),
    message: "does not exist"
  }
}

validator

Allows you to specify a child Validator for the property

Example: validate friend objects with a secondary validator

friends:{
  array:true,
  validator: new FriendValidator()
}

Consumed Libraries

validate.js

Declarative validating of javascript objects

Contributing

Getting started

Install node_modules via npm

npm i

Install typings

typings install

Build the project (using typescript compiler)

npm run build

Test the project (builds before testing)

npm test