Skip to content

matthispaita/js-contract-on-value

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-contract-on-value

Library for create contract on the type value of a data.

First class support about type union and iterator.

Example:

const a = new String('Hello world!');
const b = 'Hello world!';
const c = 15;
const ctString = contract('primitive', String);
// No error
{
	if ((contract.validataBool(a, ctString) === false) ||,
	(contract.validataBool(c, ctString) === false))
		throw new Error('a && b shall be a String!');
}
// Throw error
{
	contract.validate(c, ctString, new TypeError('c shall be a String!'));
}
// Create a contract who expect data has Symbol.iterator property and values are wheter Number || Boolean || String
{
	contract('iterator', [Number, Boolean, String], 'primitive');
}

Important objects:

Export of this library is contract function.
There are three type of contract called as assertion and stored at assertions object.
contract is used to create a new object representating the contract.
validate and validateBool validate a contract in function of a data.
All functions are pure.

Choose an assertion:

Primitive:

Used to match primitive data type.
Correct if data is an instance of model or an primitive of this.
Function composing the model shall be an identifier of object primitives

Instance:

Simply a test based on instanceof operator.

Iterator:

Ensure that data is an iterable object and each values corresponding to the model.
subType arg is required for the call to contract and model is complies to that.

Creating a contract:

Validating a contract:

Error:

Boolean:

Deeper:

See also:

About

Library for create contract on the value of a data.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published