
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
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.
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".
Let us get start with validation:
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:
We can verify complex object by schema like:
schema.assert(x).is({
name:String,
age:Number
});
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.
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
}]
})
Save schema as "type" to re-use them.
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);
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.
Defined new type by leverage existing type, I mean:
schema.type('Proposal',{
id: schema.Integer
title: String,
description: String,
resourceDemands: [schema.ResourceDemand]
});
'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.
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.
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.
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 :)
FAQs
Schema Validation System
We found that duck-type demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.