Types

Primitive Types

You can use one of primitive types: *, array, string, undefined, boolean, function, nan, null, number, object, regexp

validate( true, "boolean" );
// or
validate( true, "Boolean" );
validate( null, "boolean" ); // ByContractError: expected boolean but got null

const fn = function(){ validate( arguments, [ "boolean", "*" ]); };
fn( null, "any" ); // ByContractError: Argument #0: expected boolean but got null

Union Types

validate( 100, "string|number|boolean" ); // ok
validate( "foo", "string|number|boolean" ); // ok
validate( true, "string|number|boolean" ); // ok
validate( [], "string|number|boolean" );
// ByContractError: expected string|number|boolean but failed on each:
// expected string but got array, expected number but got array, expected boolean but got array

Optional Parameters

Array Expression

Object Expression

Structure

Interface validation

You can validate if a supplied value is an instance of a declared interface:

When the interface is globally available you can set contract as a string:

Globally available interfaces can also be used in Array/Object expressions:

Nullable Type

Validation Exceptions

Last updated

Was this helpful?