0.0.5 • Published 9 years ago

model-validation v0.0.5

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

model-validation

Experimental, API breaks all the time.

Goal is to create an asynchronous model and data validation framework for immutable models. This means that validators must not mutate the model, or any other inputs they get.

Example with JavaScript

var V = require("model-validation");

// Validation definition
var userValidator = V.object({
    id : V.integer,
    name : V.required(V.string),
    email : V.string,
    address : V.object({
        city : V.required(V.string),
        street : V.string,
    })
});

// Validation is done like this
userValidator.validate({
    id : 5,
    name : "Jack Doe",
    email : "jack@example.com",
    address: {
        city : "Philly",
        street : "Homestreet 123"
    }
}).then(function(v) {
    // v is validated object
}).catch(function(errs) {
    // errs is object of errors {[name: string] : string[]}
});

Example with TypeScript and composition

/// <reference path="node_modules/model-validation/index.d.ts" />
import * as V from 'model-validation';

interface Address {
    city : string
    street : string
}

var addressValidator = V.object<Address>({
    city : V.required(V.string),
    street : V.string,
});

interface User {
    id: number
    name: string
    email: string
    address: Address
}

var userValidator = V.object<User>({
    id : V.integer,
    name : V.required(V.string),
    email : V.string,
    address : addressValidator
});

userValidator.validate({
    id : 5,
    name : "Jack Doe",
    email : "jack@example.com",
    address: {
        city : "Philly",
        street : "Homestreet 123"
    }
}).then((v) => {
    // v is validated object
}).catch((errs) => {
    // errs is object of errors {[name: string] : string[]}
});
0.0.5

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago