ducktype
Flexible data validation using a duck type interface for JavaScript and Node.js.
As JavaScript is a loosely typed language, any variable can contain any type of data, and any type of data can be passed as arguments to any function. When dealing with data inputs coming from external sources, there is a need to validate the type and contents of the data. Ducktype offers an easy way to validate both basic data types as well as complex structured data types in a flexible way.
Replace this kind of type checking mess:
{ if contact && && contactid > 0 && && contactaddress && && // ... save contact else throw 'Invalid contact'; }
with this:
var contactType = ; { contactType; // ... save contact}
Install
npm
npm install ducktype
bower
bower install ducktype
Load
Node.js
var ducktype = ;
browser
Use
Basic types
// use built-in typesducktypenumber; // trueducktypenumber; // falseducktypenumber; // falseducktypedate; // trueducktypedate; // falseducktypestring; // true // create a ducktypevar type = ;type; // truetype; // falsetype; // false // create a ducktype with optionsvar nullableString = ;nullableString; // truenullableString; // truenullableString; // false
Combined types
// combination of typesvar combi = ;combi; // truecombi; // truecombi; // false
Structured objects
// structured objectvar person = ; person; // true person; // false
Structured Arrays
// structured arraysvar numberArray = ;numberArray; // truenumberArray; // false // structured object and arrayvar family = ; family; // true family; // false
Function arguments
var type = ; { type; return a + b;} var sum = ; // okvar sum = ; // will throw a TypeError
Alternatively, a ducktype wrapper can be created which validates the function arguments against the ducktype:
var add = ; var sum = ; // okvar sum = ; // will throw a TypeError
API
construction
A ducktype can be constructed as:
ducktype(type)
ducktype(type, options)
ducktype(type1, type2, ...)
ducktype(type1, type2, ..., options)
Where:
-
type
can be:- A basic type. Choose from
Array
,Boolean
,Date
,Function
,Number
,Object
,RegExp
,String
,null
,undefined
. - Another ducktype.
- An object. All properties of the object will be checked. Each property can be a basic type, ducktype, object, or array.
- An array.
An array can have zero, one or multiple elements which can be
a basic type, ducktype, object, or array.
Providing an array with zero elements will just return a
ducktype(Array)
. Providing an array with one element will return a ducktype which will test each of tested arrays elements against the given type, for exampleducktype([Number]).test(1, 2, 3)
. Providing an array with multiple elements will validate the length of the tested array, and validate each of the array elements one to one against the provided types. This can be used to test the number and type of function arguments. Example:ducktype([Number, String]).test(2, 'str')
.
- A basic type. Choose from
-
options
is an object which can contain properties:- A string
name
- A boolean
optional
- A boolean
nullable
- A boolean
integer
. Test whether a number has an integer value. Only applicable for Numbers. - A number
min
. Test whether a number is larger or equal to a minimum value. Only applicable for Numbers. - A number
max
. Test whether a number is smaller or equal to a maximum value. Only applicable for Numbers.
- A string
A created ducktype has functions:
test(object)
. A function which returns true when provided object matches the ducktype, and false otherwise.validate(object)
. A function which will throw a TypeError when the provided object does not match the ducktype.wrap(fn)
. Creates a wrapper function around the provided function, which validates the function arguments against the ducktype. Only applicable for ducktypes containing an array, as the ducktype is tested against an array with the function arguments.
Built-in types
Ducktype comes with a set of built-in types:
ducktype.array
ducktype.boolean
ducktype.date
ducktype.email
ducktype.integer
ducktype.function
ducktype.number
ducktype.object
ducktype.regexp
ducktype.string
ducktype.null
ducktype.undefined
ducktype.url
The built-in types can be used as:
ducktypenumber; // trueducktypestring; // false
Test
To execute tests for the library, run:
npm test
Roadmap
- Implement more options for specific types:
- Number: finite, odd, even, positive, negative, nan, ...
- String: lowercase, uppercase, alpha, alphanumeric, empty, ...
- Array: length, length.min, length.max, ...
- Implement a parser accepting a string describing a type in annotations.
- Implement support to define your own tests for custom types.
- Implement non-strict type checking: when an object can be converted to the desired type, it is ok. For example a string containing a numeric value can be considered a valid Number, or a string containing an ISO date can be considered a valid Date.
License
Copyright (C) 2013-2018 Jos de Jong wjosdejong@gmail.com
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
http://www.apache.org/licenses/LICENSE-2.0
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.