Giter Club home page Giter Club logo

tink_validation's Introduction

tink_validation

Build Status Gitter

Runtime type check and value extractor for dynamic objects

Background

Sometimes we just cannot make sure the data type during compile time, usually when the input data comes from external source. For example:

var obj:{myInt:Int} = Json.parse('{"myString":"hello, world"}');
trace(obj.myInt + 10); // fail

// for the best security, we need to check it manually
if(obj.myInt == null || !Std.is(obj.myInt, Int)) throw "Invalid data";

However, thanks to Haxe macros, we don't need to write these check codes manually. This library helps you generate the validation codes and also make sure the resulting object contains only the fields you need.

Usage

Extractor

The extractor will get the data out of the passed object, checking type, setting to null the missing ones and returning the result.

var source:Dynamic = {a:1, b:"2"};
var r:{a:Int} = Validation.extract(source); // r is {a:1}

var source:Dynamic = {a:1, b:"2"};
var r:{a:Int, ?c:Int} = Validation.extract(source); // r is {a:1, c:null}

var source: Dynamic = {a:"a", b: "2"};
var r:{a:Int, ?c:Int} = Validation.extract(source); // will throw UnexpectedType([a], Int, "a");

Validator

The validator will only check the existence of fields and their type. If everything is alright, it won't return anything.

var source:Dynamic = {a:1, b:"2"};
Validation.validate((source: {a:Int})); // OK

var source:Dynamic = {a:1, b:"2"};
Validation.validate((source: {a:Int, ?c:Int})); // OK

var source: Dynamic = {a:"a", b: "2"};
Validation.validate((source: {a:Int, c:Int})); // will throw MissingField([c]);

var source: Dynamic = {a:1, c: "2"};
Validation.validate((source: {a:Int, c:Int})); // will throw UnexpectedType([c], Int, "2");

var source: Dynamic = {a:1, c: {a:1, b:2}};
Validation.validate((source: {a:Int, c:{a:Int, b:String}})); // will throw UnexpectedType([c,b], String, 2);

Errors

2 types errors can be thrown:

MissingField(path: Array<String>);
UnexpectedType(path: Array<String>, expectedType: Class, actualValue: Dynamic);

The first parameter path is an array of each object level passed by the validator before reaching the error.

tink_validation's People

Contributors

9morello avatar back2dos avatar jbips avatar kevinresol avatar

Stargazers

 avatar

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.