Flexible data validation using a duck type interface


Data validation using a ducktype interface. For JavaScript and Node.js.

npm install ducktype
bower install ducktype
var ducktype = require('ducktype');
    <script src="ducktype.js" type="text/javascript"></script> 
    <script type="text/javascript">
        // use ducktype... 

Example usage:

// simple type 
var number = ducktype(Number);
number.test(2.3);   // true 
number.test('hi');  // false 
number.test(true);  // false 
// create a structured object 
var person = ducktype({
  name: String,
  age: Number,
  address: {
    city: String,
    street: String,
  email: ducktype(String, {optional: true})
  name: 'John',
  age: 32,
  address: {
    city: 'Sunnyvale, CA 95125',
    street: '701 First Ave.',
    country: 'United States'
  phone: '123-45678'
}); // true 
  name: 'Mary',
  age: 26
}); // false 

A ducktype can be constructed as:

ducktype(type, options)
ducktype(type1, type2, ..., options)


  • type is a type description. This can be another DuckType,
  • options is an object with properties
    • A string name (optional)
    • A boolean optional (optional)
    • A boolean nullable (optional)

A created ducktype has methods:

  • test(object). A method which returns true when provided object matches the ducktype, and false otherwise.
  • validate(object). A method which will throw a TypeError when the provided object does not match the ducktype.

To execute tests for the library, run:

npm test

Copyright (C) 2013 Jos de Jong

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.