Giter Site home page Giter Site logo

vlad's Introduction

vlad Build Status

A simple asynchronous JSON validator with a chainable syntax.

npm install vlad

Example

Object Validation

var validate = vlad.promise({
    email: vlad.string,
    location: vlad({
        long: vlad.number.required.within(-180, 180),
        lat: vlad.number.required.within(-90, 90)
    }),

    tags: vlad.array.of(vlad.string.within(3, 10))
});

validate(validObject).then(function(value) {
    /*{
        email: "[email protected]",
        location: {
            long: 70.235,
            lat: 60.234
        },
        tags: ['foo', 'bar']
    }*/
});


validate(invalidObject).catch(function(err) {
    /* GroupValidationError {
        message: "Invalid object.",
        fields: {
            email: FieldValidationError {message: '...'},
            location: GroupValidationError {
                message: 'Invalid object.',
                fields: {
                    long: FieldValidationError {message: '...'}
                }
            },
            tags: ArrayValidationError {
                message: 'Invalid array.',
                fields: [
                    undefined,
                    FieldValidationError {message: '...'},
                    undefined
                ]
            }
        }
    }*/
});

Express Middleware

router.post('/',
    vlad.middleware('body', {
        email: vlad.string.required.pattern(/.*@.*/)
    }),
    function(req, res) {
        res.send(200);
    }
);

router.use(function(err, req, res, next) {
    if (err instanceof vlad.ValidationError) {
        res.status(400).send(err.toJSON());
    } else {
        res.sendStatus(500);
    }
});

Subvalidators

var validate = vlad({
    field: vlad.string.required
});

validate({ field: 'hello world' }).then( /* handle */ );
validate.field('hello world').then( /* handle */ )

API Reference

vlad's People

Contributors

nickclaw avatar

Stargazers

Holden avatar Bryan Gula avatar Joseph Zhong avatar Egor Gumenyuk avatar  avatar Kostas Bariotis avatar hello there avatar Narendra Sisodiya avatar Morton Fox avatar bake avatar  avatar Yavuz Ege Özcan avatar  avatar Mudit avatar Andrée Hansson avatar Julien CROUZET avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Forkers

gulabry stegmanh

vlad's Issues

Write Tests

  • util function tests
  • chaining tests
  • jsonschema

Descriptive errors

Right now vlad uses the jsonschema node module to parse schema. Unfortunately the error message returned by it are not great. Possibly look into modifying jsonschema to return better error messages, writing my own limited JSON schema validator, or using another type of validation entirely.

vladidate function

What do we want to return from the vladidate function?

If we want to be synchronous the function would either have to return the validated value, or throw an error. Which I'd rather not do because it means lots of try-catch statements for nested vladidators.

With asynchronous operation we get a little better control flow with errors.

vladidate(obj, function(err, value) {
    if (err) return handle(err);
    success(value);
});

// or

vladidate(obj)
    .then(function(value) { success(value); })
    .catch(function(err) { handle(err); });

It would also let people do custom asynchronous validation which could also be potentially added to the chainable properties.

var checkServiceUrl = 'some/url';
var vladidate = vlad({
    a: vlad.string.test(checkServiceAsync.bind(null, checkServiceUrl)),
    b: function(value) {
        return checkWithServiceAsync(checkServiceUrl, value);
    })
});

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.