GithubHelp home page GithubHelp logo

cerberus's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.