npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

duck-type

v0.0.17

Published

Schema Validation System

Downloads

32

Readme

duck-type

Duck type is a schema and validator JavaScript library, which provide a natural way to define schema and validate your data structure in JavaScript. The purpose of this library is try to help you to build up 'Complicated But Robust JavaScript Program', especially when you have to teamwork with other peoples, or developing your code based on unstable API.

Getting Started

Currently, duck-type can support both NodeJS and browser:

   ## in node
   npm install duck-type

and, use it in your code, like:

  // in node 
  var schema = require('../duck-type').create();

Or

   ## in browser
   bower intall duck-type

and, use it in your code, like:

  // in browser, global variable  duckType
  var schema = duckType.create();

It also support "requirejs".

Validation:

Let us get start with validation:

Example 1

    schema.assert(1).is(String);   //throw Error
    schema(String).match(1);       //throw Error

    schema.assert('1').is(String); //passed, return true
    schema(String).match('1');     //passed, return true

We also can verify many parameters at once, like:

    schema.assert(x, y).are(String, Number);  
    schema(String, Number).match(x, y);  

schema.assert(...).is(...) and schema.assert(...).are(...) eaquals schema(...).match(...), it is just different coding style.

We will use schema.assert in flowing example:

Example 2

We can verify complex object by schema like:

  schema.assert(x).is({
    name:String, 
    age:Number
  });

Example 3

Even support "nest" schema like this:

  schema.assert(x).is({
    name : {
      first:String, 
      last:String
    },
    age: Number,
    sayHello: Function
    });

Here :

'sayHello': Function means target object which to verified must have a method named 'sayHello'.

'name', is a nest schema.

Example 4

For array, duck-type can support different pattern:

  schema.assert(x).is([]);        // x must be a array, element can by any type
	
  schema.assert(X).is([Number]);  //x must be a array, element must be a Number
	
  schema.assert(X).is([Number, String, Date]);
   /* 
    means x must be a array, 
    and the first element  must be a Number, 
    the second element must be a String....
    */

Of cause, we can combine definition of array and object, like;

  schema.assert(x).is({
    title: String,
    description: String,
      resourceDemands: [{
        resourceTypeId: Number,
        year: Number,
        month: Number,
        quantity: Number
    }]
  })

Define schema:

Save schema as "type" to re-use them.

Example 5

Define a type:

  schema.type('ResourceDemand',{	//now, we defined a type ResourceDemand
    resourceTypeId: Number,
    year: Number,
    month: Number,
    quantity: Number
  });

Re-use type.

	schema.assert(x).is(schema.ResourceDemand);

Example 6

We can define some basic type, even like java.lang.Integer

	schema.type('Integer',function(value){
		return schema.assert(value).is(Number) && value % 1 === 0 && value >= -2147483648 && value <= 2147483647;
	});

Here, by define the validate function we can decided what is 'Integer' in our program.

Example 7

Defined new type by leverage existing type, I mean:

  schema.type('Proposal',{
    id: schema.Integer
    title: String,
    description: String,
    resourceDemands: [schema.ResourceDemand]
});	

Other interesting features:

Example 8

Generate data.

'Generate' is another interesting feature provided by duck-type.

  schema.generate(schema.Proposal);  //it will return an object, which must compatible with type Proposal.

I mean,

  {
    id: 112,
    title: 'sdfasf adsf',
    description: 'sdfsdf sdf 234s sd',
    resourceDemands: [{
      resourceTypeId: 123,
      year: 2343,
      month: 234,
      quantity: 444
    }]
  }

The object like above might be return, of cause, most of value will be changed randomly.

Example 9

Optional property

The type can define optional property for an object by using function schema.optional.

  schema.type('Profile', {
    name: String,
    skill: schema.optional(String)
  });

Here, skill' is a optional property, it can be undefined, BUT, if it has value, the value must be a String.

Example 10

Operator: And, Or

Dynamic data type of arguments is common in JavaScript. which means we need operator 'Or',

  schema.assert(x).is(schema.or(String, Number));

Here, the value of parameter 'x' can be a String, or can be a Number.

Example 11

Implement Interfaces

In Java world, we often need make sure a Object must implement Interface A, Interface B... Similarly, operator 'And' can used for this purpose in JavaScript.

  schema.type('Config',{ //here is definition of type 'Config'
    orderBy:String
    layout: String
  });

 schema.type('Query',{ //here is definition of type 'Query'
    table: String,
    id: Number
  });

 schema.assert(x).is(schema.and(schema.Config, schema.Query)); 

Here, we want to make sure the value of 'x' must implement type 'Config', and type 'Query' at same time.

###End

The library duck-type is still developing continually, more interesting feature will be bring to you. We also except any of your comments.


More information can be get by accessing Wiki page

Thanks :)