validate( [ 1,1 ],"Array.<number>" ); // okvalidate( [ 1,"1" ],"Array.<number>" );// ByContractError: array element 1: expected number but got string// orvalidate( [ 1,1 ],"number[]" ); // okvalidate( [ 1,"1" ],"number[]" );// ByContractError: array element 1: expected number but got string
Object Expression
validate( { foo:"foo", bar:"bar" },"Object.<string, string>" ); // okvalidate( { foo:"foo", bar:100 },"Object.<string, string>" );// ByContractError: object property bar: expected string but got number
Structure
validate({ foo:"foo", bar:10}, { foo:"string", bar:"number"}); // okvalidate({ foo:"foo", bar: { quiz: [10] }}, { foo:"string", bar: { quiz:"number[]" }}); // okvalidate({ foo:"foo", bar:10}, { foo:"string", bar:"number"}); // ByContractError: property #bar expected number but got null
Interface validation
You can validate if a supplied value is an instance of a declared interface:
classMyClass {}constinstance=newMyClass();validate( instance, MyClass ); // ok
classMyClass {}classBar {}constinstance=newMyClass();validate( instance, Bar );// ByContractError: expected instance of Bar but got instance of MyClass
When the interface is globally available you can set contract as a string: