GithubHelp home page GithubHelp logo

ag-types's Introduction

ag-types - arbitrary data structure validation for Javascript

Build Status NPM version Dependency Status Coverage Status

You've got incoming JSON data that you'd like to access using Javascript, but you can't be sure its structure matches what you expect. ag-types allows you to validate the structure of the input graph before you crash and burn on a TypeError: 'undefined' is not an object.

Installation

npm install ag-types

Usage

Examples in Coffeescript.

Describe your type

{ Object, String, Optional, Map, Any } = require 'ag-types'
RequestType = Object {
  url: String
  method: String
  params: Optional Map Any
}

Construct a type validator by simply mirroring the structure of what you want.

Validate input data against the type

input = {
  url: 'http://example.com'
}
RequestType(input)

Your validator is a function that returns a data.validation.

Continue off the validation result

RequestType(input).fold(
  (errors) -> console.log "This doesn't look like a valid request: ", errors
  (request) -> doRequest request
)

If an error occurs, the errors object will hold details of what went wrong. Otherwise you're good to go with the data.

Development

npm install
grunt test watch:test --force

ag-types's People

Contributors

ezku avatar juhazi avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ezku

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.