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

cerberus

v1.0.4

Published

Object Validator with Typescript support

Downloads

68

Readme

Cerberus

The typescript object validator
Experimental, work in progress.

Build Status codecov dependencies Status devDependencies Status Greenkeeper badge

Here is a basic example.

// We can import the schema `types` to match the types of Typescript.
import { object, any, string, number } from "cerberus";
// Create our schema
const schema = object({
  a: string,
  b: number,
  c: any
});
// Get an object to test
const obj: any = { a: "foo", b: 42, c: "bar" };
// Validate the object against the schema
const result = schema.validate(obj);
if (result.valid) {
  // A valid result gives us
  // result.obj: { a: string, b: number, c: any }
}

Features

  • Validation against types and exact values
  • Correctly typed result object
  • Relevant constraints for each type, e.g. string.length
  • Optional and default values
  • Referencing values within objects
  • Asynchronously validate values with mapAsync

Installation

npm install cerberus

Examples

import { object, array, string, integer } from "cerberus";

const person = object({ name: string, id: integer.positive() });
const schema = object({
  ...person.schema,
  mother: person,
  father: person,
  children: array(person)
});
const result = schema.validate({
  name: "foo jr bar",
  id: 3,
  mother: { name: "baz buz bar", id: 1 },
  father: { name: "foo bar", id: 2 },
  children: [{ name: "bum bar", id: 4 }, { name: "baz bar", id: 5 }]
});

API Documentation

Temporary

Types

The below functions are exposed to create a validator.

  • boolean: Validator<boolean>
  • number: Validator<number>
  • string: Validator<string>
  • any: Validator<any>
  • integer: Validator<number> - not a decimal
  • nil: Validator<null>
  • required: Validator<any> - not undefined and not null
  • forbidden: Validator<undefined>
  • array(Validator<A>): Validator<A[]>
  • object(Schema<A>): Validator<A>

A schema is an object where each property is a validator or a function from A to a validator.

Runners

Below are the two methods of running a validator. Both of these also have an async version, called by appending Async.

validate(Validator<A>, value): Result<A>, validateAsync(Validator<A>, value): Result<A>

Result contains an info property, which is an object containing whether the result is valid or not, and if it is valid, it holds the validated value, else it holds an error.

info: { valid: true; object: A } | { valid: false; error: ValidationError }

assert(Validator<A>, value): A, assertAsync(Validator<A>, value): A

Returns the validated value, or throws an error if invalid.

Logical operators

The below functions are exposed for logical operations on validators. They are also methods on the class, if you prefer chaining.

and(Validator<A>, Validator<B>): Validator<A & B>

Short circuits if the first is invalid.

or(Validator<A>, Validator<B>): Validator<A | B>

Short circuits if the first is valid.

xor(Validator<A>, Validator<B>): Validator<A | B>

Is valid if only one is valid, invalid in all other cases.

Methods

#default(a: A): Validator<A>

Allows the value to be undefined, and returns a in that case.

#optional<A>(): Validator<A | undefined>

Allows the value to be undefined.

#not(a: A): Validator<A>

Ensures that the value is not strictly equal to a.

#map(A -> B): Validator<B>, mapAsync(A -> Promise<B>): Validator<B>

Maps the result through the function if the validator returns valid.

Contribute

Please submit an issue with outlining your idea. If small, a pull request can be submitted immediately.

License

This project is licensed under the MIT license.