Git Product home page Git Product logo

fish-type-js's Introduction

Fish type JS

Data type validation in function calls on Runtime to be used in javascript projects.

dependencies Status license version build

We have the new version 1.1.0 ready!

In this version:

  • Improve input function type semantic.
  • Add typedef validation module.

Objective:

Every JS programmer knows how annoying it is to work with static validators of data types. That's why I created this library, to help us and make sure that our function is always executed only with the types of data that we define for it.

Type data validation:

This library uses joi https://github.com/hapijs/joi to handle the type validations, you can use custom primitives data as "string", "bool", "number" or more complex structure to validate if the parameters used to call a function match with the paramters that we define to allow it.

Usage:

Install:

Run this command to install the library from npm.

npm install fish-type-js

Library:

The library export two things, a decorate function that have to be use for decorate a single function and add a automatic call parameters validation, and a list of primitive data types.

Decorate function - format

const {decorate} = require('fish-type-js');

//I will get the function output.
const sumT = decorate({input},output)(sum);

Define input and output:

You can specify the types of the parameters that the function receive and the return data of the function.

const newFunction = decorate({input},output)(Function);

IN: paramters structure / OUT: type function return

Type parameters: You can use this type parameters in each parameter key.

  • undefined
  • string
  • null
  • boolean
  • number
  • object
  • promise

Or create more complex object schema and mix the function call with a joi schema validation, is important to remember include JOI module to use his data types schema.

const Joi = require('joi');

//Include primitives types.
const pointType =  Joi.object().keys({
  lat: Joi.number(),
  lng: Joi.number()
});

const findGeoT  = decorate({point:pointType})(findGeo);

findGeoT({lat:1.111,lng:3.01});

Examples:

Try this basic example of how to use the library.

//Include lib and types.
const {decorate} = require('fish-type-js');

//Basic function.
const sum = (a,b)=>{
  return a+b;
}

//Decorate the function.
const sumT  = decorate({a:'number',b:'number'},'number')(sum);

sumT(10,10);

If you want more examples, go and download the project and go to /samples folder and chek the examples.

Note:

This library is a work in progress project, I'm sure that will be very usefull for any JS developer, but take care that could be changes in the next versions. We are working in create our custom structure validation engine to avoid use JOI.

fish-type-js's People

Contributors

damiancipolat avatar dcipolattruenorth avatar

Watchers

 avatar  avatar  avatar  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.